Search code examples
javabufferedreader

Java buffreader with close in middle


I have a method that receives a string with a filepath, and i have created all the code for reading the file, with the try a catch blocks, somthing very simple like this:

private static String readLineFormFile(String filename){

    File filepath=  new File(filename);
    BufferedReader reader = null;
    String line =null;      

    try{

    reader = new BufferedReader(new FileReader(filepath));

    line=reader.readLine();
    }
    catch (FileNotFoundException fe1){
        System.out.println(filename+" file Not Found");
    }
    catch (IOException ie1) {
        System.out.println("Error Reading File "+filename);
    }   
    finally{
        try{
        reader.close();


    }catch(IOException ie2){
        System.out.println("Error closing file "+filename);
    }
 }
    return line;
}

Now if I call this method on a file 2 times, will the buffereader still know the line I was after I close it for the first time?


Solution

  • will the buffereader still [k]now the line i was after i close it for the first time?

    No. You will be reading from the beginning of the file each time.