繁体   English   中英

PrintWriter不将文本打印到文件

[英]PrintWriter not printing text to file

我的任务是将一个文件的文本复制到另一个文件。 使用以下代码,我可以将文件的各行打印到控制台,但无法打印到输出文件。

      try {
        System.out.print("Enter the file name with extension : ");

        Scanner input = new Scanner(System.in);

        File inputFile = new File(input.nextLine());
        PrintWriter out = new PrintWriter("/Users/jonathanzier/Dropbox/IdeaProjects/CSE_205_Homework1/src/com/company/output.txt");


        input = new Scanner(inputFile);


        while (input.hasNextLine()) {
            out.println(input.nextLine());
           //System.out.println(input.nextLine());  - This will print 
           //correctly to the console
        }
        out.close();


    } catch (FileNotFoundException e) {
        System.out.println("File Not Found");
    }

打印作家使用缓冲作家。 它不会立即写入磁盘。 调用printlnprintfformat方法时,如果启用了自动刷新,它将写入磁盘。 否则,您必须调用flush方法才能写入磁盘。 通常,调用close方法时应将缓冲区中的内容写入文件,而无需使用flush方法。

https://docs.oracle.com/javase/7/docs/api/java/io/PrintWriter.html

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM