import java.io.*;
public class InputName {
public static void main (String [] args)throws IOException{
System.out.println("请输入姓名(结束请敲两次回车键):");//提示语
InputStreamReader keyBoard = new InputStreamReader(System.in);//读入用户输入,并将其转换为字符流
BufferedReader br = new BufferedReader(keyBoard);//建立输入缓冲区
File f = new File("name.txt");//创建name.txt文件
f.createNewFile();//若该文件原来不存在,则新建文件
FileWriter writer = new FileWriter(f,true);//新建输出器
String s;//用作输出的临时字符串
while((s=br.readLine())!=""){
writer.write(s);
}
writer.close();
keyBoard.close();
br.close();
}
}
请问为什么没法输出到name.txt?
Java从命令行读取输入然后输出到文本文件的问题
答案:1 悬赏:80 手机版
解决时间 2021-03-11 01:29
- 提问者网友:沉默的哀伤
- 2021-03-10 09:38
最佳答案
- 五星知识达人网友:刀戟声无边
- 2021-03-10 10:22
import java.io.*;
public class InputName {
public static void main(String[] args) throws IOException {
System.out.println("请输入姓名(结束请敲两次回车键):");
InputStreamReader keyBoard = new InputStreamReader(System. in );
BufferedReader br = new BufferedReader(keyBoard);
FileWriter writer = new FileWriter("name.txt", true);
String s;
while ((s = br.readLine()) != null) {
writer.write(s);
}
writer.flush();
writer.close();
keyBoard.close();
br.close();
}
}
public class InputName {
public static void main(String[] args) throws IOException {
System.out.println("请输入姓名(结束请敲两次回车键):");
InputStreamReader keyBoard = new InputStreamReader(System. in );
BufferedReader br = new BufferedReader(keyBoard);
FileWriter writer = new FileWriter("name.txt", true);
String s;
while ((s = br.readLine()) != null) {
writer.write(s);
}
writer.flush();
writer.close();
keyBoard.close();
br.close();
}
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯