Java:访问和写入文件时出现问题

问题描述:

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

I was testing out writing to files with this code:

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 [异常]! ...
我不知道是什么原因引起的。我的文件包正下方是文件Test ... txt。

Nothing happens when it is executed. "This is a test!" is not written, nor the StackTrace or the "A/An [exception] occurred!"... I don't know what's causing the problem. I have fileTest1.txt in the package right under the file...

A BufferedWriter 就是这样做的,它将缓冲输出,然后再将其写入目标。这样可以使 BufferedWriter 更快地使用,因为它不必立即写入磁盘或套接字等慢速目标。

A BufferedWriter does just that, it buffers the output before it is written to the destination. This can make the BufferedWriter faster to use as it doesn't have to write to a slow destination, like a disk or socket, straight away.

当内部缓冲区已满时,将写入内容,您 flush Writer 关闭作家

The contents will be written when the internal buffer is to full, you flush the Writer or close the writer

请记住,如果打开它,则应将其关闭...

Remember, if you open it, you should close it...

例如...

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

If you are using Java 7, you may like to take a look at try-with-resources