Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java BufferedReader不读取文件内容_Java_Xml - Fatal编程技术网

Java BufferedReader不读取文件内容

Java BufferedReader不读取文件内容,java,xml,Java,Xml,我目前正在尝试创建一个程序来读取和修改.test文件(UTF-8,看起来类似于xml文件),以避免手动编辑每个文件。我设法让程序成功地循环遍历文件夹中的文件,但我的问题出现在尝试读取文件内容时。当前我的readFile函数如下所示: public static ArrayList<String> readFile(String fileName) { // This will reference one line at a time String line = nu

我目前正在尝试创建一个程序来读取和修改.test文件(UTF-8,看起来类似于xml文件),以避免手动编辑每个文件。我设法让程序成功地循环遍历文件夹中的文件,但我的问题出现在尝试读取文件内容时。当前我的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;
}
publicstaticarraylistreadfile(字符串文件名){
//这将一次引用一行
字符串行=null;
试一试{
//FileReader以默认编码读取文本文件。
文件=新文件(文件名);
BufferedReader=新的BufferedReader(新的InputStreamReader(新文件InputStream(文件),“UTF8”));
//创建要返回的arrayList
ArrayList fileContents=新的ArrayList();
//读第一行
line=reader.readLine();
//循环遍历文件,并将每一行写入arrayList
while(行!=null){
fileContents.add(行);
line=reader.readLine();
}   
//近距离阅读
reader.close();
//返回文件内容
返回文件内容;
}
捕获(FileNotFoundException ex){
System.out.println(
无法打开文件“+”文件名“+””;
}
捕获(IOEX异常){
System.out.println(“读取文件“+”文件名“+”时出错”);
}
返回null;
}
其中fileName是一个带有文件路径的字符串(例如:c:\Tests\test1.test)

我尝试过使用PrintReader,但也不起作用。代码没有抛出异常,当我开始读取第一行(line=reader.readLine())时,读取器返回null,就好像文件是空的一样。我是不是用错了阅读器


任何帮助或见解都将不胜感激。谢谢

您可以尝试以下代码来读取文件内容

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();
}   
公共静态列表getFileContentAsList(字符串文件名){
文件f=新文件(文件名);
试一试{
返回文件.readAllLines(f.toPath());
}捕获(IOE异常){
e、 printStackTrace();
}
返回集合。emptyList();
}   

很可能由于编码错误而将null作为响应。我注意到你有“UTF8”,那不应该是“UTF-8”或StandardCharsets.UTF_8.name()?

我设法解决了这个问题。在以前的运行中,程序必须擦除第一个文件,因此当代码试图读取它时,它是空的。此解决方案与@Sudhakar的解决方案一样有效。

能否验证
文件是否存在?是否尝试过?@JacobG。使用“调试”菜单,我可以验证文件是否存在。@我添加了连字符,但并没有改变结果。对于我来说,代码返回了一个空列表,类似于上面的内容。我发现无论是否使用连字符,函数都是一样的