• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Read text file using delimter "\r\n" only

 
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In my application, I want to read the text file line by line. For this, I used FileReader#readLine() method. But this method uses default delimiter as \n, \r or \r\n.
In the text file, I want to treat the string "\r\n" as new line so that line number is calculated correctly. Individul characters like '\n' or '\r', are not valid line separators in my text file.
Can anybody tell me if I can override the default delimeters so that readLine() method will return the line if it ends with "\r\n" combination only.
If not, is there any other java API which can help me to implement the required functionality?
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By FileReader#readLine, do you mean BufferedReader#readLine? That does not appear to allow a different delimiter. Have you tried the Scanner class, which does allow different delimiters? Is that any use?
Or, use an unbuffered FileReader, put the last character read into a "previousCharacter" local variable and try a while loop
while(!(previousCharacter == '\r' && presentCharacter == '\n')){ . . .}

See whether either of those solutions works.
 
Aji Ozkan
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Scanner is present in JDK 5.0 where as I am using JDK 1.4.2
The second approach to read one character at a time is not efficient.
Can you suggest any other approach?
 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Use Campbell's algorithm but on a BufferedReader. Efficient and effective.
 
Aji Ozkan
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In worst case, I have to read 60MB of data. I think it will take hell lot of time if I read character by character.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic