Java : Problems accessing and writing to file

大兔子大兔子 提交于 2019-12-11 09:26:50

问题


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();
    }
}

}

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...


回答1:


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.

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...

For example...

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();
        }
    }
}

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




回答2:


After

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

you have to flush() the writer. To avoid leaking of resources you should also close() the writer (which automatically flushes it).

So you need to add:

fileWrite.close();



回答3:


Use BufferedWriter.flush() and BufferedWriter.close(). Additional info here http://docs.oracle.com/javase/7/docs/api/java/io/BufferedWriter.html




回答4:


You must call close() or at least flush() on the writer in order for the buffer to be really written to the file.



来源:https://stackoverflow.com/questions/18283513/java-problems-accessing-and-writing-to-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!