我有以下创建和写入该文件的方法。
// Create the file and the PrintWriter that will write to the file
private static PrintWriter createFile(String fileName){
try{
// Creates a File object that allows you to work with files on the hardrive
File listOfNames = new File(fileName);
PrintWriter infoToWrite = new PrintWriter(new BufferedWriter(
new FileWriter(listOfNames);
return infoToWrite;
}
// You have to catch this when you call FileWriter
catch(IOException e){
System.out.println("An I/O Error Occurred");
// Closes the program
System.exit(0);
}
return null;
}
该程序运行良好,即使我没有bufferedWriter and FileWriter
像下面这样。他们两个对象如何帮助改善写作过程?在这种情况下,我可以避免创建两个对象。
PrintWriter infoToWrite = new PrintWriter((listOfNames);