1

我正在测试使用以下代码写入文件:

package files;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;

public class FileTest1 
{
public static void main(String[] args)
{
    try
    {
        try
        {
            File f = new File("filetest1.txt");
            FileWriter fWrite = new FileWriter(f);
            BufferedWriter fileWrite = new BufferedWriter(fWrite);
            fileWrite.write("This is a test!");
        }
        catch(FileNotFoundException e)
        {
            System.out.print("A FileNotFoundException occurred!");
            e.printStackTrace();
        }
    }
    catch(IOException e)
    {
        System.out.println("An IOException occurred!:");
        e.printStackTrace();
    }
}

}

执行时没有任何反应。“这是一个测验!” 没有写,也没有 StackTrace 或“发生 A/An [异常]!”......我不知道是什么导致了问题。我在文件下面的包中有fileTest1.txt...

4

4 回答 4

4

ABufferedWriter就是这样做的,它在将输出写入目标之前对其进行缓冲。这可以使使用BufferedWriter速度更快,因为它不必立即写入慢速目标,如磁盘或套接字。

当内部缓冲区已满时,内容将被写入,flushWriterclose作者

记住,如果你打开它,你应该关闭它......

例如...

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class TestFileWriter {

    public static void main(String[] args) {
        try {
            BufferedWriter fileWrite = null;
            try {
                File f = new File("filetest1.txt");
                System.out.println("Writing to " + f.getCanonicalPath());
                FileWriter fWrite = new FileWriter(f);
                fileWrite = new BufferedWriter(fWrite);
                fileWrite.write("This is a test!");
                fileWrite.flush();
            } catch (FileNotFoundException e) {
                System.out.print("A FileNotFoundException occurred!");
                e.printStackTrace();
            } finally {
                try {
                    // Note, BufferedWriter#close will also close
                    // the parent Writer...
                    fileWrite.close();
                } catch (Exception exp) {
                }
            }
        } catch (IOException e) {
            System.out.println("An IOException occurred!:");
            e.printStackTrace();
        }
        try {
            BufferedReader br = null;
            try {
                File f = new File("filetest1.txt");
                System.out.println("Reading from " + f.getCanonicalPath());
                FileReader fReader = new FileReader(f);
                br = new BufferedReader(fReader);
                String text = null;
                while ((text = br.readLine()) != null) {
                    System.out.println(text);
                }
            } catch (FileNotFoundException e) {
                System.out.print("A FileNotFoundException occurred!");
                e.printStackTrace();
            } finally {
                try {
                    // Note, BufferedWriter#close will also close
                    // the parent Writer...
                    br.close();
                } catch (Exception exp) {
                }
            }
        } catch (IOException e) {
            System.out.println("An IOException occurred!:");
            e.printStackTrace();
        }
    }
}

如果您使用的是 Java 7,您可能想看看try-with-resources

于 2013-08-16T23:32:40.667 回答
1

fileWrite.write("This is a test!");

你必须对flush()作家。为了避免资源泄漏,您还应该close()编写器(它会自动刷新它)。

所以你需要添加:

fileWrite.close();
于 2013-08-16T23:32:50.813 回答
1

使用BufferedWriter.flush()BufferedWriter.close()。此处的附加信息http://docs.oracle.com/javase/7/docs/api/java/io/BufferedWriter.html

于 2013-08-16T23:33:08.917 回答
0

您必须调用close()或至少调用flush()writer 才能将缓冲区真正写入文件。

于 2013-08-16T23:33:12.857 回答