2

我相信这个问题有一个相当简单的答案,所以我们开始吧。

我正在尝试使用 FileWriter 将文本写入文件。我的程序从用户指定的现有文件中读取文本,然后询问是将文本打印到控制台还是新文件,也由用户命名。

我相信我的问题是将 FileWriter 传递给“FileOrConsole”方法。我没有在“FileOrConsole”方法中正确传递或声明 FileWriter 吗?该文件始终被创建,但没有写入任何内容。

这是代码:

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);
    }
  }
}
4

2 回答 2

4

你犯了经典错误,忘记了在java的情况下按值传递的参数是引用的值。问题是你的任务

out = new BufferedWriter(new FileWriter(response));

实际上并没有改变 main() 中声明的变量,它保持为空

BufferedWriter out = null; 然后最后它通过 if(out==null) 跳过 close() 并且因为它是缓冲的并且你没有刷新任何内容都不会写入文件。你要做的是 out.close(); 在 FileOrConsole 方法调用中

或者

执行 out = new BufferedWriter(new FileWriter(response)); 在它之外。你选 :-)

于 2011-07-20T23:29:28.097 回答
0

尝试刷新您的流,但更重要的是,请记住关闭它。

这是处理流的推荐做法的代码示例。相同的方法也可用于输入流以及数据库代码之类的东西,在这些代码中,始终清理自己以获得预期结果很重要。

BufferedWriter out = null;
try {
    out = // ... create your writer

    // ... use your writer
} catch(IOException ex) {

    // maybe there was a problem creating or using the writer

} finally {
   if (null != out) {
       out.flush();
       out.close();
       out = null;
   }
}
于 2011-07-20T23:29:30.337 回答