将FileWriter作为参数传递给方法 [英] Passing FileWriter as a parameter to a method
问题描述
我敢肯定,这个问题的答案很简单,所以我们开始吧.
I'm sure there is a fairly simple answer to this question, so here we go.
我正在尝试使用FileWriter将文本写入文件.我的程序从用户指定的现有文件中读取文本,然后询问是将文本打印到控制台还是新文件(也要由用户命名).
I'm trying to use a FileWriter to write text to a file. My program reads text in from an already existing file, specified by the user and then asks whether to print the text to the console or to a new file, also to be named by the user.
我相信我的问题是将FileWriter传递给"FileOrConsole"方法.我没有在"FileOrConsole"方法中正确传递或声明FileWriter吗?始终会创建该文件,但不会写入任何文件.
I believe my problem is with passing the FileWriter to the "FileOrConsole" method. Am I not passing or declaring the FileWriter in the "FileOrConsole" method correctly? The file is always created but nothing is written to it.
这是代码:
import java.io.*;
import java.util.*;
public class Reader {
public static void main(String[] args) throws IOException {
Scanner s = null, input = new Scanner(System.in);
BufferedWriter out = null;
try {
System.out.println("Would you like to read from a file?");
String answer = input.nextLine();
while (answer.startsWith("y")) {
System.out.println("What file would you like to read from?");
String file = input.nextLine();
s = new Scanner(new BufferedReader(new FileReader(file)));
System.out
.println("Would you like to print file output to console or file?");
FileOrConsole(input.nextLine(), s, input, out);
System.out
.println("\nWould you like to read from the file again?");
answer = input.nextLine();
}
if (!answer.equalsIgnoreCase("yes")) {
System.out.println("Goodbye!");
}
} catch (IOException e) {
System.out.println("ERROR! File not found!");
// e.printStackTrace();
} finally {
if (s != null) {
s.close();
}
if (out != null) {
out.close();
}
}
}
public static void FileOrConsole(String response, Scanner s, Scanner input,
BufferedWriter out) {
if (response.equalsIgnoreCase("console")) {
while (s.hasNext()) {
System.out.println(s.nextLine());
}
} else if (response.equalsIgnoreCase("file")) {
System.out.println("Name of output file?");
response = input.nextLine();
try {
out = new BufferedWriter(new FileWriter(response));
} catch (IOException e) {
e.printStackTrace();
}
while (s.hasNext()) {
try {
out.write(s.nextLine());
out.newLine();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
} else {
System.out.println("Sorry, invalid response. File or console?");
response = input.nextLine();
FileOrConsole(response, s, input, out);
}
}
}
推荐答案
您会遇到经典错误,忘记了在Java中按值传递的参数是引用的值.事情是你的任务
you make classic error forgetting that parameters passed by value in case of java it is a value of the reference. The thing is that your assignment
out = new BufferedWriter(new FileWriter(response));
实际上不会更改main()中声明的变量,而是保持为空
actually does not change the variable declared in main() it stays null
BufferedWriter out = null; 然后最后它会通过if(out == null)跳过close() 因为它是缓冲的,所以您不执行刷新操作,不会将任何内容写入文件. 您要做的是out.close();在FileOrConsole方法调用旁边
BufferedWriter out = null; and then in finally it skips the close() by the if(out==null) and as it is Buffered and you do no flush nothing is written to file. what you got to do is out.close(); in side the FileOrConsole method call
OR
执行out = new BufferedWriter(new FileWriter(response)); 在它外面.您选择:-)
do the out = new BufferedWriter(new FileWriter(response)); outside of it. You choose :-)
这篇关于将FileWriter作为参数传递给方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!