\n troubles

From: gajo (gajo_at_eunet.yu)
Date: 06/24/04


Date: Thu, 24 Jun 2004 23:28:24 +0200

OK, again I have troubles dealing with String. I have a field of the type
String which contains some text that is separated into lines at random
places, or in other words, the text contains \n\r (or \n in the case of
linux). I write this field into a file and there it looks ok, but when I
read it back I get the whole string in one line. So I figured I'd add a
separator at the end of each line. However, then I get an empty line after
each line! I'm using GZIP to pack the data, so perhaps that is causing this?
Btw. after each block of text I write an end-of-block string to know that
it's the end of that block.
Here's the explanation in code:

String notes = ""; // the result
String lin = ""; // used to read one line from the file
final String eob = "---$#-||-"; // end of block
lin = filein.readLine(); // I'm reading the 1st line

while (lin.compareTo(eob) != 0) {
    notes += lin + System.getProperty("line.separator");
    lin = filein.readLine();
    if (lin == null) { break; } // this will never occur, but just in case
}
return notes;

//-------------
// filein is a BufferedReader
// I can't change that, don't ask why, and
// because of that this is how I read a file
filein = new BufferedReader(
           new InputStreamReader(
           new BufferedInputStream(
           new GZIPInputStream(
           new FileInputStream("testFile.gz"))))));

I figured I could erase one empty line after the reading-in is done, but I'm
not sure if this will happen in every case, on every computer. I'm thinking
it could be an anomaly for Windows files.
Anyway, I'm waiting for some suggestions ;)

Gajo



Relevant Pages

  • Re: Learning the fretboard..
    ... Sight reading at a higher level entails recognition of musical ... is your default handling of two whole steps along a string, ... -Start with a major triad, in root position, with the root on the ... There is only one way to play this triad. ...
    (rec.music.classical.guitar)
  • Re: Program slow. Garbage Collection
    ... the larger string I only had to build one string and call ToLoweron ... The three different possible approaches discussed so far -- "read the whole file", "read a line at a time as separate strings", "read a line at a time into a pre-allocated buffer" -- all have the bulk of their cost in the same thing: ... Reading a line at a time into separate strings does wind up creating a large number of individual strings. ... Also, these objects may survive long enough to wind up in the older generations of the heap; on the one hand, that alleviates the cost those objects cause for GC operations, but on the other hand you know for sure those objects are still going to get destroyed soon, so it'd be better for them to not wind up in the older generation where they may remain and cause increased memory pressure for a longer time. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Cross domain iframe access
    ... and WebKit do not allow to read back the `location' string value ... Such that any attempt to read the - toString - property, or if it can be read then to execute its value, may be the point at which a security exception is thrown. ... Other forms of direct access to the - location - object certainly can stand as a demonstration that reading its value is not subject to any security restrictions. ...
    (comp.lang.javascript)
  • Re: A Modern Method for Guitar
    ... as part of my new practice schedule, i'm reading for 30 minutes every ... Open position is actually much harder to play in than the upper positions because you have to deal with open strings ringing out when they're not supposed to be. ... I generally try to sight read in one of those positions and simply walk down the low E string for any notes below the position's range or up the high E string for nay notes above the position's range. ...
    (rec.music.makers.guitar.jazz)
  • Re: Easy question but I cant seem to get it to work
    ... BufferedReader br = new BufferedReader; ... return returnText; ... Reading a whole file into a string is dangerous, what if the whole file is 2GB long? ... I always just loop through reading each line and appending to a StringBuffer until readLine is null. ...
    (comp.lang.java.help)