Java往文件里写文字的问题
大家帮我看看这段代码能往file4中写东西么???我执行以后卡在了“正在存储中”,打开文件一看,什么都没有。BufferedReaderin=newBufferedRea...
大家帮我看看这段代码能往file4中写东西么???
我执行以后卡在了“正在存储中”,打开文件一看,什么都没有。
BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); System.out.println("请输入内容:\n"); try { FileWriter writer = new FileWriter(file4.toString()); while(!(in.readLine().equalsIgnoreCase("exit"))){ String happyString = in.readLine(); System.out.println("您输入的内容是:\""+happyString+"\",正在存储中"); writer.write(happyString+"\n"); } writer.flush(); writer.close(); } catch (IOException e) { e.printStackTrace(); }
file4是通过以下代码创建的TXT:
File file4 = new File("C:\\HappyNewYear\\HappyNewYear.txt"); 展开
我执行以后卡在了“正在存储中”,打开文件一看,什么都没有。
BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); System.out.println("请输入内容:\n"); try { FileWriter writer = new FileWriter(file4.toString()); while(!(in.readLine().equalsIgnoreCase("exit"))){ String happyString = in.readLine(); System.out.println("您输入的内容是:\""+happyString+"\",正在存储中"); writer.write(happyString+"\n"); } writer.flush(); writer.close(); } catch (IOException e) { e.printStackTrace(); }
file4是通过以下代码创建的TXT:
File file4 = new File("C:\\HappyNewYear\\HappyNewYear.txt"); 展开
2个回答
推荐于2016-02-01
展开全部
File file4 = new File("FileTextCopy2.java");
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.println("请输入内容:\n");
try {
String happyString ;
FileWriter writer = new FileWriter(file4);
while(!(happyString=in.readLine()).equalsIgnoreCase("exit")){
System.out.println("您输入的内容是:\""+happyString+"\",正在存储中");
writer.write(happyString+"\n");
}
writer.flush();
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
更多追问追答
追问
还是没有写入
追答
你肯定看错了。
注意 文件改名了。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询