Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.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_Java 8_Bufferedreader - Fatal编程技术网

Java BufferedReader未读取整个文件且未退出循环

Java BufferedReader未读取整个文件且未退出循环,java,java-8,bufferedreader,Java,Java 8,Bufferedreader,我有一个ini文件要在我的应用程序中读取,但问题是它没有读取整个文件,并且它在while循环中阻塞 我的代码: FileReader fr = new FileReader(file); BufferedReader br = new BufferedReader(fr); String line = br.readLine(); Properties section = null; while(line!=null){ if(line.startsWith("[") &&

我有一个ini文件要在我的应用程序中读取,但问题是它没有读取整个文件,并且它在while循环中阻塞

我的代码:

FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);

String line = br.readLine();
Properties section = null;

while(line!=null){
     if(line.startsWith("[") && line.endsWith("]")){
         section = new Properties();
         this.config.put(line.substring(1, line.length() - 1), section);
     }else{
         String key = line.split("=")[0];
         String value = line.split("=")[1];
         section.setProperty(key, value);
     }

     line = br.readLine();
     System.out.println(line);

     // To continue reading newline. 
     //if i remove this, it will not continue reading the second header
     if(line.equals("")){ 
         line = br.readLine();
     }  
}

System.out.println("Done"); // Not printing this.
这是ini文件中的内容。新行被包括在内,因此我添加line.equals

输出:

[header]
key=value
[header2]
key1=value1
key2=value2
[header3]
key=value
//whitespace
//whitespace
更新:
我删除了ini文件中的所有换行符,但仍然没有读取整个文件。

除非这篇文章中没有包含某些内容,否则逻辑不会陷入循环。。。如果您正在使用的文件与您发布的文件完全相同,它将命中一个空行(因为您只跳过了一个空行)或一个开始的行,并获得ArrayIndexOutOfBoundsException,因为这些行不包含=。。。将while循环简化为该循环,ArrayIndexOutofBoundsException将不会出现,它将处理完整文件:

    Properties section = null;
    String line = null;
    while ((line = br.readLine()) != null) {
        if (line.startsWith("[") && line.endsWith("]")) {
            section = new Properties();
            this.config.put(line.substring(1, line.length() - 1), section);
        } else if (line.contains("=") && !line.startsWith("#")) {
            String[] keyValue = line.split("=");
            section.setProperty(keyValue[0], keyValue[1]);
        }
    }

请注意,我正在做一行.contains=以便跳过空行和起始行…

您只跳过一行空行,最好忽略所有空行。在循环开始时,如果!line.isEmpty{//执行正常处理}//读取新行。提示:当line=br.readLine!=null作为主循环,更易于处理,并且不在不同的位置读取。尝试了这两种方法,但仍然不读取整个文件。在第一个解决方案中,它读取换行符,直到第一个解决方案出现一些文本。在第二个例子中,没有换行符,但它只能读到header3。在调用堆栈的某个地方,可能有可怕的异常处理代码,抑制了代码抛出的异常。修复此问题,例如,通过移除任何try-catch块。谢谢!但是控制台中没有显示ArrayIndexOutOfBoundsCeptions。如果控制台中没有ArrayIndexOutOfBoundsCeptions,则可能是您在某处吞咽了它们,或者您的示例文件/代码不代表您正在使用的文件/代码。并且@user85421是正确的,这个解决方案并不完美,因为它将处理从包含=,开始的行,将else if中的条件更改为行。包含=&&!line.startsWith将阻止您处理包含=。
    Properties section = null;
    String line = null;
    while ((line = br.readLine()) != null) {
        if (line.startsWith("[") && line.endsWith("]")) {
            section = new Properties();
            this.config.put(line.substring(1, line.length() - 1), section);
        } else if (line.contains("=") && !line.startsWith("#")) {
            String[] keyValue = line.split("=");
            section.setProperty(keyValue[0], keyValue[1]);
        }
    }