Search code examples
javabufferedreader

BufferedReader not reading complete .txt file


I'm trying to read a moderately sized txt file (65,00 words) into a String, then a string array. The bufferedreader throws the "could not read file" catch. When I clear it, a small part of the contents of the text file is shown in the system.out. I do not get the error with smaller text files. I'm a beginner and I'm having a lot of trouble trying to narrow down the issue.

Why isn't the BufferedReader ingesting the entire file? And why is the "could not read file" error being thrown?

public class Main {

public static void main(String[] args) {

    final Guid Main = new Guid();  //creates instance of Guid

    Main.mergeButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) 
        {
        merge();

        }
    });

}
    static void merge()
    {

        //read file and turn into string
        String pathone = open(Guid.PathOne);
        System.out.print(pathone);

        //parse and format 
        //String[] oneArray = pathone.replace("\n"," ").split(" "); 


        //get pathtwo text
        //String pathtwo = open(Guid.PathTwo);

        //parse and format
        //load into array
        //compare array entries
        //add new array entry
        //sort array
        //write array to paththree file


        //for(int i=0; i<oneArray.length;i++)
        //{
            //System.out.println(oneArray[i]);
        //}

    }


    public static String open(JTextArea Path)
    {
        String record = null;

        FileReader frFile = null;
        try {
            frFile = new FileReader(Path.getText());//gets file from Path

            BufferedReader brFile = new BufferedReader(frFile);//creates buffered reader

            record = brFile.readLine() + "\n"; //gets contents of file and puts it into a string
            brFile.mark(0);

            while (brFile.read() != -1) //loop to read the rest of the text file
                {
                    brFile.reset();
                    record = record + brFile.readLine() + "\n";
                    brFile.mark(0);
                }

        } 
        catch (FileNotFoundException e) //catch path is in error
            {
                JFrame frame = null;

                JOptionPane.showMessageDialog(frame, "Could not find file.");
            } 
        catch (IOException e) //catch if file cannot be read
            {
                JFrame frame = null;

                JOptionPane.showMessageDialog(frame, "Could not read file.");
            }

        try {  //closes file
            frFile.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return record;
    }


}

Solution

  • Do in this way. Remove reset() and mark() methods if you want to read the entire file.

    StringBuilder record = new StringBuilder();
    BufferedReader brFile = new BufferedReader(frFile);
    String line = null;
    while ((line = brFile.readLine()) != null) {
        record.append(line).append("\n");
    }
    

    Note:

    • Don't forget to close the stream.
    • Use finally block to close the stream
    • Use StringBuilder or StringBuffer to append the string.
    • Use System.getProperty("line.separator") to get the system specific line separator

    Have a look at Java7 try-with-resources Statement advantage