在java中使用BufferedWriter写入文件 [英] Write file using BufferedWriter in java
本文介绍了在java中使用BufferedWriter写入文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
BufferedWriter writer;
public void writeStats(int word,int numSent,int short,int longest,int average)
{
try
{
File file = new File(jefferson_stats.txt);
file.createNewFile();
writer = new BufferedWriter(new FileWriter(file));
writer.write(字数:+字);
writer.newLine();
writer.write(句数:+ numSent);
writer.newLine();
writer.write(最短句:+最短+字);
writer.newLine();
writer.write(最长句:+最长+字);
writer.newLine();
writer.write(Average sentence:+ average +words);
}
catch(FileNotFoundException e)
{
System.out.println(File Not Found);
System.exit(1);
}
catch(IOException e)
{
System.out.println(something messed up);
System.exit(1);
}
}
解决方案
你必须刷新和关闭你的作家
writer.flush();
writer.close();
I am doing a lab where we have to read in an external file, take some statistics on the data, and then create and write a new file with the stats. Everything in my program works except for writing the file, which I cannot understand why my method won't work.
BufferedWriter writer;
public void writeStats(int word,int numSent, int shortest, int longest, int average)
{
try
{
File file = new File("jefferson_stats.txt");
file.createNewFile();
writer = new BufferedWriter(new FileWriter(file));
writer.write("Number of words: " + word );
writer.newLine();
writer.write("Number of sentences: " + numSent );
writer.newLine();
writer.write("Shortest sentence: " + shortest + " words");
writer.newLine();
writer.write("Longest sentence: " + longest + " words");
writer.newLine();
writer.write("Average sentence: " + average + " words");
}
catch(FileNotFoundException e)
{
System.out.println("File Not Found");
System.exit( 1 );
}
catch(IOException e)
{
System.out.println("something messed up");
System.exit( 1 );
}
}
解决方案
You have to flush and close your Writer
writer.flush();
writer.close();
这篇关于在java中使用BufferedWriter写入文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文