简体   繁体   English

Java BufferedReader无法读取文件内容

[英]Java BufferedReader not reading file contents

I am currently trying to create a program to read and modify .test files (UTF-8, looks similar to an xml file) in order to avoid manually editing each one. 我目前正在尝试创建一个程序来读取和修改.test文件(UTF-8,看起来类似于xml文件),以避免手动编辑每个文件。 I managed to get the program to successfully loop through the files in a folder, but my problem comes when trying to read the contents of the files. 我设法使程序成功遍历文件夹中的文件,但是尝试读取文件内容时出现了问题。 Currently my readFile function looks like this: 目前,我的readFile函数如下所示:

public static ArrayList<String> readFile(String fileName) {

    // This will reference one line at a time
    String line = null;

    try {
        // FileReader reads text files in the default encoding.
        File file = new File(fileName);

        BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF8"));

        // Create arrayList to return
        ArrayList<String> fileContents = new ArrayList<String>();

        // Read first line
        line = reader.readLine();

        // Loop through file, and write each line to arrayList
        while(line != null) {
            fileContents.add(line);
            line = reader.readLine();
        }   

        // Close reader
        reader.close();

        // Return file contents
        return fileContents;
    }
    catch(FileNotFoundException ex) {
        System.out.println(
            "Unable to open file '" + fileName + "'");                
    }
    catch(IOException ex) {
        System.out.println("Error reading file '" + fileName + "'");                  
    }
    return null;
}

Where fileName is a string with the path to the file (ex: c:\\Tests\\test1.test). 其中,fileName是带有文件路径的字符串(例如:c:\\ Tests \\ test1.test)。

I have tried using PrintReader, but that also did not work. 我已经尝试过使用PrintReader,但这也行不通。 The code is not throwing and exception, when I get to reading the first line (line = reader.readLine()) the reader return null, as if the file was empty. 代码没有抛出异常,当我开始阅读第一行(line = reader.readLine())时,阅读器将返回null,就像文件为空一样。 Am I using the reader incorrectly? 我使用的阅读器不正确吗?

Any help or insight would be greatly appreciated. 任何帮助或见识将不胜感激。 Thanks! 谢谢!

Getting null as a response is likely because your encoding is wrong. 由于您的编码错误,可能会得到null作为响应。 I notice you have "UTF8", shouldn't that be "UTF-8" or StandardCharsets.UTF_8.name()? 我注意到您有“ UTF8”,不是“ UTF-8”或StandardCharsets.UTF_8.name()吗?

I managed to figure out the issue. 我设法找出问题所在。 On an earlier run the program must have erased the first file, so when the code was trying to read it, it was empty. 在较早的运行中,程序必须擦除了第一个文件,因此在代码尝试读取该文件时,该文件为空。 This solution works, as well as @Sudhakar's solution as well. 此解决方案以及@Sudhakar的解决方案同样有效。

You can try the below code to read the file content. 您可以尝试以下代码来读取文件内容。

public static List<String> getFileContentAsList(String fileName) {
    File f = new File(fileName);
    try {
        return Files.readAllLines(f.toPath());
    } catch (IOException e) {
        e.printStackTrace();
    }

    return Collections.emptyList();
}   

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

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