5

I am using a buffered reader to read in a file filled with lines of information. Some of the longer lines of text extend to be more than one line so the buffered views them as a new line. Each line ends with ';' symbol. So I was wondering if there was a way to make the buffered reader read a line until it reaches the ';' then return the whole line as a string. Here a how I am using the buffered reader so far.

  String currentLine;
        while((currentLine = reader.readLine()) != null) {
            // trim newline when comparing with lineToRemove
            String[] line = currentLine.split(" ");
            String fir = line[1];
            String las = line[2];
            for(int c = 0; c < players.size(); c++){
                if(players.get(c).getFirst().equals(fir) && players.get(c).getLast().equals(las) ){
                    System.out.println(fir + " " + las);
                    String text2 = currentLine.replaceAll("[.*?]", ".150");
                    writer.write(text2 + System.getProperty("line.separator"));
                }
            }
        }
Mureinik
  • 297,002
  • 52
  • 306
  • 350
Waggoner_Keith
  • 590
  • 2
  • 9
  • 37
  • 1
    "Some of the longer lines of text extend to be more than one line so the buffered views them as a new line." That's not a line. That's multiple lines. If you've got a line break (any of "\r\n", "\n" or "\r"), you've got a new line - if you want to define "line" to be anything else, don't use `BufferedReader.readLine` – Jon Skeet Jun 14 '15 at 16:59
  • Right. I want the buffered reader to separate lines only when it reaches a ";" instead of a "\n". What is a better option would a scanner be able to do this more efficiently? – Waggoner_Keith Jun 14 '15 at 17:01
  • 2
    Use a `Scanner` and set the delimiter to `";"`. I'm sure there are some tutorials for that "out there". – Tom Jun 14 '15 at 17:03

2 Answers2

11

It would be much easier to do with a Scanner, where you can just set the delimiter:

Scanner scan = new Scanner(new File("/path/to/file.txt"));
scan.useDelimiter(Pattern.compile(";"));
while (scan.hasNext()) {
    String logicalLine = scan.next();
    // rest of your logic
}
Erick G. Hagstrom
  • 4,873
  • 1
  • 24
  • 38
Mureinik
  • 297,002
  • 52
  • 306
  • 350
3

To answer your question directly, it is not possible. Buffered Reader cannot scan stream in advance to find this character and then return everything before target character.

When you read from stream with Buffered Reader you are consuming characters and you cannot really know character without reading.

You could use inherited method read() to read only single character and then stop when you detect desired character. Granted, this is not good thing to do because it contradicts the purpose of BufferedReader.

John
  • 5,189
  • 2
  • 38
  • 62
  • Thank you. Your right after it is much easier to use a scanner due to the face that the buffered reader cannot get a target character as easy. – Waggoner_Keith Jun 14 '15 at 17:18