Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BufferedReader not reading entire text file

                String str = "";
            try {

                BufferedReader br = new BufferedReader(new FileReader(file.getAbsolutePath()));
                while (br.readLine() != null) {
                    str += br.readLine();
                }
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            String replace = str.replace("HTTP Request: ", "")
                    .replace("Resource URL: ","")
                    .replace("Attribute\t\tDescription", "| Attribute | Type | Description |<P>|----|----|<P>")
                    .replace("Data Type | Max Length | Requirement |", "")
                    .replace("N/A", "Object")
                    .replace("String", "| String")
                    .replace("255 |", "")
                    .replace("Required", "**Required**")
                    .replace("Optional", "**Optional**")
                    .replace("Request Example <P>", "")
                    .replace("Response Example <P>", "Nothing");

            PrintWriter pw = null;

The BufferedReader ignores the first 3 lines and reads/converts the rest. Not sure as to what the issue is. I have tried other StackOverflow solutions but none of them seem to work!

like image 664
Suhail Prasathong Avatar asked Jan 06 '23 21:01

Suhail Prasathong


1 Answers

The problem is here:

while (br.readLine() != null)

The time that you check if the br.readLine() is not null you have already read the line.To fix this you can try the following:

String line = br.readLine();
while (line != null){
    str +=line;
    line = br.readLine();
}
like image 91
theVoid Avatar answered Jan 08 '23 09:01

theVoid