Question

and thanks in advance for help. I am pretty new to Java and haven't had any formal Java education. I am making a Minecraft Bukkit server for practice and am logging sign locations to a file when created. I am trying to make a boolean that returns true if the location is already in the file and false if it isn't. I can't use a while loop or else it freezes the server so I am using a for loop. Anyways, all of the lines that the BufferedReader returns are null and I don't know why. I have an integer that returns the numbers of lines in a file using BufferedReader that works fine. I don't see why it is returning null with the boolean. Here is the boolean:

code Block :

    private boolean isDuplicate(String in) throws IOException
    {
        File f = file;
        try {
            fr = new FileReader(f);
            br = new BufferedReader(fr);
        } catch (FileNotFoundException e1) {
            log(Level.WARNING, "File not found.");
        }
        String temp;
        in = in.substring(0,in.length() - 1);
        for(int i = 0; i < (countLines(f)); i++)
        {
            if((temp = br.readLine()) != null)
            {
                String s = temp;
                log(temp);
            }
        }
        return false;
    }

Here is the integer:

    private int countLines(File f){
        try {
            fr = new FileReader(f);
            br = new BufferedReader(fr);
        } catch (FileNotFoundException e1) {
            log(Level.WARNING, "File not found!");
        }

        for(int i = 0; i < 100000; i++)
        {
            try
            {
                if(br.readLine() == null)
                {
                    return i;
                }
            }
            catch (NullPointerException e)
            {
                return i;
            }
            catch (IOException e)
            {

            }
        }
        return 0;
    }
Was it helpful?

Solution

The 'apparent reason' is that you have reached the end of the file. But this code is nonsense. You don't need to count the lines in advance at all, and you certainly don't need to count them every time around the loop, which is what your current code does. At present your processing is O(N2), which is disastrous. Just call readLine() until it returns null:

while ((line = br.readLine()) != null)
{
    // ...
    log(line);
}

NB There is no way that your isDuplicate() method does anything even slightly resembling its name.

OTHER TIPS

There are quite a few issues with that code (most glaring: when you catch the 'FileNotFound' you should return and log a 'fatal' - no point in continuing). I'm also assuming that your actual code is not the one you show (it wouldn't compile).

Anyways, here's how you read a file in Java:

public boolean isDuplicate(String lookup, String filename) {
    try {
        BufferedReader reader = new BufferedReader(new FileReader(filename));
        String line;
        int lineNo = 0;
        while ((line = reader.readLine()) != null) {
            lineNo++;
            // best would be to use a regex to match against the line
            // however, the naive approach would be
            if (line.indexOf(lookup) != -1) {
                log.info(String.format"Lookup term %s found on line %d", lookup, lineNo);
                return true;
            }
        }
        log.info(String.format("Lookup term %s not found in %s", lookup, filename);
        return false;
    } catch (IOException e) {
        // log a fatal error and bail
        log.error(String.format("Could not read from %s [%s]", filename, e.getLocalizedMessage()));
        // here best practice would be to throw your own custom application ex
        throw new MyApplicationException(e);
    }
}

The while and the for both behave essentially the same way; the reason why your while never "returned" is probably due to your "exhausting" the stream in the countlines() method.

Firstly, you dont need the countLines function or the for loop thats using it. You can replace the if statment with

while((temp = br.readLine()) != null)

Secondly, the countLines is using the global bufferedReader and has probably finished reading the entire file. Any new br.readLine() will return null henceforth. Hence when you return from the countlines function and try to do br.readLine(), you get a null value.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top