0

我尝试检查是否已到达文件末尾,但空字符串的条件不起作用

public void readEquation(String fileName) {  
        File aFile = new File(fileName);
        String line = null;
        int cnt = 0;
        try {
            FileReader reader = new FileReader(aFile);
            BufferedReader bufferedReader = new BufferedReader(reader);
            while ((line = bufferedReader.readLine()) != null) {      
                //but line gets null
                intoMap(line, cnt);
                cnt++;
            }
        } catch (IOException e1) {
            e1.printStackTrace();
        }
    }
4

5 回答 5

1

您错误地阅读两行而不是一次。循环的条件自己读取一行,然后在循环内读取另一行。我建议这个改变:

public void readEquation(String fileName) {  
        File aFile = new File(fileName);
        String line = null;
        int cnt = 0;
        try {
            FileReader reader = new FileReader(aFile);
            BufferedReader bufferedReader = new BufferedReader(reader);
            line = bufferedReader.readLine();
            while (line != null) {                  
                intoMap(line, cnt);
                cnt++;
                line = bufferedReader.readLine();
            }
        } catch (IOException e1) {
            e1.printStackTrace();
        }
    }
于 2012-06-10T15:08:25.427 回答
0

您正在连续阅读两行:循环条件中的一行,然后是循环主体中的另一行。您在条件中读取的内容将被忽略且从未使用过。没有任何东西会检查第二行的值。

通常人们这样写这个循环:

while ((line = bufferedReader.readLine()) != null) {      
    intoMap(line, cnt);
    cnt++;
}

这样,您总是在使用前检查线路。

于 2012-06-10T15:06:04.570 回答
0
while ((bufferedReader.readLine()) != null) {      
                line = bufferedReader.readLine();    //but line gets null

你在这里读了两行。

为避免这种情况,请使用 tmp String 之类的

String line=null;
while ((line=bufferedReader.readLine()) != null) {  
    //now line stores line from file or null if file doesn't have new line

编辑 - 示例

我刚刚测试了这段代码

    BufferedReader bufferedReader=null;
    try {
        String line = null;
        bufferedReader = new BufferedReader(new FileReader("d:\\ranks.txt"));
        while ((line=bufferedReader.readLine()) != null) {      
            System.out.println(line);
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally{
        try {
            bufferedReader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

它奏效了。

于 2012-06-10T15:06:07.447 回答
0

您的代码的问题是,当您在 while 条件下执行 bufferedReader.readLine() 时,您已经在读取该行。

你应该做这个:

public void readEquation(String fileName) {  
    File aFile = new File(fileName);
    String line = null;
    int cnt = 0;
    try {
        FileReader reader = new FileReader(aFile);
        BufferedReader bufferedReader = new BufferedReader(reader);
        while ((line = bufferedReader.readLine()) != null) {       // only reads once
            intoMap(line, cnt);
            cnt++;
        }
    } catch (IOException e1) {
        e1.printStackTrace();
    }
}
于 2012-06-10T15:07:31.123 回答
0

这对你有用。

 while ((line = bufferedReader.readLine()) != null) {      

                    intoMap(line, cnt);
                    cnt++;
                }
于 2012-06-10T15:10:01.700 回答