PrintWriter仅写入部分文本 [英] PrintWriter writes only partial text

查看:94
本文介绍了PrintWriter仅写入部分文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于某种原因,我的String部分由PrintWriter编写。结果我在我的文件中得到了部分文本。这是方法:

For some reason my String is written partially by PrintWriter. As a result I am getting partial text in my file. Here's the method:

    public void new_file_with_text(String text, String fname) {
        File f = null;
        try {
            f = new File(fname);
            f.createNewFile();
            System.out.println(text);           
            PrintWriter out = new PrintWriter(f, "UTF-8");
            out.print(text);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

我将文本打印到控制台,我可以看到那些数据都存在,没有任何东西丢失,但是当PrintWriter完成它的工作时,显然部分文本会丢失......我很无能......

Where I print text to a console, I can see that the data is all there, nothing is lost, but apparently part of text is lost when PrintWriter does its job... I am clueless..

推荐答案

在丢弃已打开的流之前,您应始终 Writer #close 您的流。这将释放JVM在文件系统上打开文件时必须要求的一些相当昂贵的系统资源。如果您不想关闭流,可以使用 Writer #flush 。这将使您的更改在文件系统上可见,而不关闭流。关闭流时,所有数据都会被隐式刷新。

You should always Writer#close your streams before you discard your opened streams. This will free some rather expensive system resources that your JVM must quire when opening a file on the file system. If you do not want to close your stream, you can use Writer#flush. This will make your changes visible on the file system without closing the stream. When closing the stream, all data is flushed implicitly.

Streams总是缓冲数据,以便在有足够的数据写入时只写入文件系统。当流以某种方式考虑数据值得写时,流会不时地自动刷新其数据。写入文件系统是一项昂贵的操作(它需要时间和系统资源),因此只有在确实需要时才应该这样做。因此,如果您希望立即写入,则需要手动刷新流的缓存。

Streams always buffer data in order to only write to the file system when there is enough data to be written. The stream flushes its data automatically every now and then when it in some way considers the data worth writing. Writing to the file system is an expensive operation (it costs time and system resources) and should therefore only be done if it really is necessary. Therefore, you need to flush your stream's cache manually, if you desire an immediate write.

一般情况下,确保始终关闭流因为他们使用了相当多的系统资源。 Java有一些关闭垃圾收集流的机制,但这些机制应该只被视为最后的手段,因为流可以在实际垃圾收集之前存活很长时间。因此,请始终使用 try {} finally {} 以确保流关闭,即使在打开流后出现异常也是如此。如果你不注意这一点,你最终会得到一个 IOException ,表示你打开了太多文件。

In general, make sure that you always close streams since they use quite some system resources. Java has some mechanisms for closing streams on garbage collection but these mechanisms should only be seen as a last resort since streams can live for quite some time before they are actually garbage collected. Therefore, always use try {} finally {} to assure that streams get closed, even on exceptions after the opening of a stream. If you do not pay attention to this, you will end up with an IOException signaling that you have opened too many files.

你想改变你的代码:

public void new_file_with_text(String text, String fname) {
    File f = null;
    try {
        f = new File(fname);
        f.createNewFile();
        System.out.println(text);           
        PrintWriter out = new PrintWriter(f, "UTF-8");
        try {
            out.print(text);
        } finally {
            out.close();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

这篇关于PrintWriter仅写入部分文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆