try{
File file = new File("write.txt");
FileWriter writer = new FileWriter(file);
PrintWriter printWriter = new PrintWriter(writer);
printWriter.println("pqr");
printWriter.println("jkl");
printWriter.close();
PrintWriter printWriter = new PrintWriter(file);
printWriter.println("abc");
printWriter.println("xyz");
printWriter.close();
}
Run Code Online (Sandbox Code Playgroud)
我不明白这两种方式有什么区别.在哪种情况下我应该使用printWriter和fileWriter.
尽管这两者在内部都使用FileOutputStream,但主要区别在于PrintWriter提供了一些其他格式化方法,例如println和printf。
代码段:
public PrintWriter(File file) throws FileNotFoundException {
this(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file))),
false);
}
public FileWriter(File file) throws IOException {
super(new FileOutputStream(file));
}
Run Code Online (Sandbox Code Playgroud)
主要区别:
| 归档时间: |
|
| 查看次数: |
21507 次 |
| 最近记录: |