0

I'm currently writing a small parser for files of the size ~500000 a very simple syntax, but reading the file is taking ages although the file is only 10 MB big.
Currently I have

public static String read(File file) {
    BufferedReader reader = null;
    String currentLine = "";
    String text = "";
    try {
        reader = new BufferedReader(new FileReader(file));
        while ((currentLine = reader.readLine()) != null) {
            text += currentLine + "\n";
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            reader.close();
        } catch (Exception e) {
        }
    }
    return text;
}

As you might notice I'm inserting a line break on the end... This line break is needed for parsing...

What should I do in order to speed the process of reading up? I need the file in a single String...

RoiEX
  • 1,186
  • 1
  • 10
  • 37
  • 1
    Tried using a stringbuilder? – Rabbit Guy May 03 '16 at 21:11
  • 1
    tl;dr: use a `StringBuilder` or the whole thing will take O(n^2). – Louis Wasserman May 03 '16 at 21:12
  • No... I'll try that! Thanks – RoiEX May 03 '16 at 21:12
  • 1
    Not only that but its a huge memory waste as each time you increase the length of the string you're creating a NEW string, where as a StringBuilder is capable of increasing in size. – 2ARSJcdocuyVu7LfjUnB May 03 '16 at 21:13
  • Do you actually need the whole file in memory in order to parse it? If you could parse line by line instead, that would avoid having to use a StringBuilder at all... – Jon Skeet May 03 '16 at 21:15
  • @JonSkeet Sometimes I need to jump backwards in Code... To get the class name, or forward, in order to check if the rest of the line are just spaces and then jump back – RoiEX May 03 '16 at 21:20
  • Instead of reading line by line you can read a file with chunck of memory, lets say 4k. If you read your file with chunck of buffers, you can reduce a time to read it. – Vishwajit R. Shinde May 03 '16 at 21:22
  • Are you doing line-reading to control the line break character? If not, and you need the entire file in memory, use block-reading, not line-reading. That is, on top of using `StringBuilder`, which is the primary need. – Andreas May 03 '16 at 21:22
  • "Rest of the line" sounds like it would just be on the same line. Even if you need a buffer of a few lines, that could still be *much* more efficient than reading the whole file. – Jon Skeet May 03 '16 at 21:22
  • Good Point... I'll look into that later... – RoiEX May 03 '16 at 21:23
  • The fastest way to read a file in is not StringBuilder but NIO/Files: byte[] encoded = Files.readAllBytes(Paths.get(path)); return new String(encoded, encoding); – Srdjan Grubor May 03 '16 at 21:42

0 Answers0