简体   繁体   中英

Use the FileReader to read the file

Today, I read the Basic I/O in the Java tutorial and I find some problem:

public class CopyCharacters {
    public static void main(String[] args) throws IOException {
        FileReader inputStream = null;
        FileWriter outputStream = null;
        try {
            inputStream = new FileReader("/workspaces/test/a.txt");
            outputStream = new FileWriter("/workspaces/test/b.txt");
            int c;
            while ((c = inputStream.read()) != -1) {
                outputStream.write(c);
            }
        } finally {
            if (inputStream != null) {
                inputStream.close();
            }
            if (outputStream != null) {
                outputStream.close();
            }
        }
    }
}

But when I run the demo, it failed. In the Console:

Exception in thread "main" java.io.FileNotFoundException: /workspaces/test/b.txt (Access is denied)
    at java.io.FileOutputStream.open(Native Method)
    at java.io.FileOutputStream.<init>(FileOutputStream.java:221)
    at java.io.FileOutputStream.<init>(FileOutputStream.java:110)
    at java.io.FileWriter.<init>(FileWriter.java:63)
    at Demo.CopyCharacters.main(CopyCharacters.java:13)

How can I do that?

File might have a lock and forbid you to open it for writing (ie your application is still on a break point in debug mode and you forgot to stop it or you killed the app and the process is still running in memory). You can check by doing this:

        inputStream = new FileReader("/workspaces/test/a.txt");
        File outFile = new File("/workspaces/test/b.txt");
        if (!outFile.canWrite()) {
            System.err.println("Cannot write into file: " + outFile.getAbsolutePath());
        }
        outputStream = new FileWriter(outFile);

You can also renname your out file "b.txt" for something else and it will work as before (until you locked it again by accident). An other way to do this is to use a temporary file:

    public static void main(String[] args) throws Exception {
    FileReader inputStream = null;
    FileWriter outputStream = null;
    try {
        inputStream = new FileReader("/workspaces/test/a.txt");
        File file = File.createTempFile("test", null);
        outputStream = new FileWriter(file);
        System.out.println(file.getAbsolutePath());
        int c;
        while ((c = inputStream.read()) != -1) {
            outputStream.write(c);
        }
    } finally {
        if (inputStream != null) {
            inputStream.close();
        }
        if (outputStream != null) {
            outputStream.close();
        }
    }

}

Good for coding (and debugging). It ensures that it will be deleted by the OS after.

Maybe you should try to use a new feature that takes care of your resources automatically by putting them inside the try-catch block?

public static void main(String[] args) throws IOException {
    try (
            FileReader inputStream = new FileReader("/workspaces/test/a.txt");
            FileWriter outputStream = new FileWriter("/workspaces/test/b.txt");
        )
        {
        int c;
        while ((c = inputStream.read()) != -1) {
            outputStream.write(c);
        }
    }
}

If you write your code in this manner, you don't need the finally block, because java will take care of the resources that are inside the try block brackets.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM