简体   繁体   中英

While loop with buffered reader readline only loops once and goes into infinite loop

Am new to java and i have a program that's supposed to read through a file using a bufferred read and do some stuffs with the result. The program is that it only reads the first and line and goes into an infinite loop. I have tried different files but am getting the same result.

private void readingFile(){
    DataType type;
    String line;
    Log log = new Log(writeFile);
    try(BufferedReader reader = Files.newBufferedReader(Paths.get(readFile))){
        while ((line = reader.readLine()) != null){
            if (line.charAt(0) == '#' || line.charAt(0) == '/') {
                continue;
            }
            String[] lineArray = line.split(" - ", 2);
            String one = lineArray[0];
            String two = lineArray[1];
            type = new DataType(one, two);
            queue.put(type);
            log.readMessage(one, two);
        }
    }
    catch (Exception ex){
        System.out.println(ex.getClass().getSimpleName() + "-" +
                ex.getMessage());
    }
}

What does the statement:

log.readMessage(one, two);

do? Does it return in a finite amount of time?

Try and run your program again while removing the following lines:

String[] lineArray = line.split(" - ", 2);
String one = lineArray[0];
String two = lineArray[1];
type = new DataType(one, two);
queue.put(type);
log.readMessage(one, two); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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