简体   繁体   English

使用FileReader读取文件

[英]Use the FileReader to read the file

Today, I read the Basic I/O in the Java tutorial and I find some problem: 今天,我阅读了Java教程中的Basic I / O,发现了一些问题:

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). 您也可以将输出文件重新命名为“ b.txt”,它可以像以前一样工作(直到您再次无意中将其锁定)。 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? 也许您应该尝试使用一项新功能,将其放入try-catch块中,从而自动处理您的资源?

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. 如果以这种方式编写代码,则不需要finally块,因为java会处理try块括号内的资源。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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