Hi,

I have a problem while reading a jsp file. I have a jsp file called scan.jsp. It is a very big file having 9000 lines. It has java script codes, some imported files etc. Unable to read all lines of data when I try the following code. 



public static final void startReadProcess(String sourcFile){
                LineNumberReader lnr = null;
                try {
                    lnr = new LineNumberReader (new FileReader (sourcFile));
                    while ((lnr.readLine ()) != null) {
                            System.out.println(lnr.readLine ());
                        }
                } catch (Exception e) {
                    e.printStackTrace();
                    PrintErrorLog.printError("Error while readingSoureFile");
                }
                finally{
                    closeReaderObj(lnr);           
                }

            }



Can any one please let me know why Data loss happing while reading big files? 

Dani AI

Generated

Short diagnosis: the loop is calling readLine() twice per iteration (once in the loop condition and again in the print), so every other line is consumed and effectively skipped. already identified that logic error and 's suggestion to use buffered/streamed reading is appropriate — the problem is a code-path bug, not the file size (9,000 lines is trivial for buffered I/O).

A concise, modern fix is to read each line once per iteration or use a streaming API. Example (Java 8+), which avoids loading the entire file and lets the platform close the stream automatically:

import java.nio.file.*;
import java.nio.charset.StandardCharsets;
import java.util.stream.Stream;

Path p = Paths.get("scan.jsp");
try (Stream<String> lines = Files.lines(p, StandardCharsets.UTF_8)) {
    lines.forEach(System.out::println);
}

Notes and troubleshooting tips: if line numbers are needed, either use a LineNumberReader correctly (call getLineNumber() after reading) or decorate the stream with an AtomicInteger counter. Avoid FileReader alone because it uses the platform default charset; prefer Files.newBufferedReader(Path, Charset) or Files.lines(Path, Charset) with an explicit Charset to prevent character-decoding issues. For extremely large files, streaming (not readAllLines) prevents OOM. If missing lines persist after fixing the double-read bug, check for concurrent writers modifying the file during the read and add simple diagnostics (print a counter or the line number alongside content) to locate where the gap occurs.

Recommended Answers

All 3 Replies

Perhaps you should use BufferedReader.

 try (BufferedReader reader = Files.newBufferedReader(sourceFile, charset)) {
        String line = null;
        while ((line = reader.readLine()) != null) {
           System.out.println(line);
        }
    } catch (IOException x) {
        System.err.format("IOException: %s%n", x);
    }

Can any one please let me know why Data loss happing while reading big files?

Because your readLine call actually reads in a line and increments the file pointer. In your current code, you are basically discarding every other line. You need to assign the result of readLine to a variable (as shown above) and print it out for getting expected behaviour.

commented: Excellent focused and correct answer. +6

Thanks for your replies. I Got an idea.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.