首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Java: PrintWriter

Java: PrintWriter
EN

Stack Overflow用户
提问于 2011-07-18 13:59:58
回答 1查看 1.9K关注 0票数 0

我正在尝试使用PrintWriter.java,但我遇到了一个相当奇怪的问题,我无法弄清楚我在这里错过了什么。

MyPrintWriter.java

代码语言:javascript
复制
public class MyPrintWriter {

    public static void main(String[] args) {

        File myFile = new File("myFileDirectory/myFileName.txt");
        try {

            FileWriter fw = new FileWriter(myFile);
            PrintWriter pw = new PrintWriter(fw);
            pw.println("Hello World!");

            pw.close();

        }   catch (FileNotFoundException e) {
                System.err.println("File not found: " + myFile);
        }   catch (Exception e) {
                e.printStackTrace();
        }       
    }

}

MyFileWriter.java

代码语言:javascript
复制
public class MyFileWriter {
    public static void main(String[] args) {

        File myFile = new File("myFileDirectory/myFileName.txt");
        try {
            InputStreamReader isr = new InputStreamReader(System.in);
            BufferedReader br = new BufferedReader(isr);

            FileWriter fw = new FileWriter(myFile);
            PrintWriter pw = new PrintWriter(fw);

            String input;
            input = br.readLine();
            while(input != null) {
                pw.println(input);
                input = br.readLine();
            }           
            br.close();
            pw.close();

        }   catch (FileNotFoundException e) {
                System.err.println("File not found: " + myFile);
        }   catch (Exception e) {
                e.printStackTrace();
        }       
    }

}

MyPrintWriter.java很高兴地将其写入myFileName.txt文件,但MyFileWrite.java不能。

有人能帮我理解我在这里错过了什么吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-07-18 14:02:10

你可能需要同花顺你的印刷作家。

参数创建一个autoFlush设置为off的PrintWriter

pw.flush()之前调用pw.close();应该能做到这一点

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6734077

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档