\n troubles

Discussion in 'Java' started by gajo, Jun 24, 2004.

  1. gajo

    gajo Guest

    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
     
    gajo, Jun 24, 2004
    #1
    1. Advertising

  2. gajo

    Jim Sculley Guest

    gajo wrote:
    > 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.


    How do you know the String is '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?


    Erm, how do you know the data in the file 'looks ok' if it has been
    compressed? zcat I suppose?

    > 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


    The usual idiom for this is:

    String nextLine = null;
    while ((nextLine = filein.readLine()) != null) {
    //do stuff with nextLine
    }

    Since you are looking for a particular end of block String, you would
    modify it to:

    String nextLine = null;
    while (!(nextLine = filein.readLine()).equals(eob) {
    //do stuff with nextLine
    }
    >
    > 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;


    Just what are you doing with 'notes' after this point? Wouldn't it be
    far better to store your Strings in a Collection of some sort instead of
    concatenating them all together? Clearly you don't *want* them to be
    all one String, but the code above *makes* them all one String.

    Whatever code you are using to write the data to the File is appending
    the newline character for you, which is why you get extra empty lines
    between your text.

    Here's some code that writes an array of Strings to a GZipped file, one
    String per line:

    ====================

    File testFile = new File("/home/jim/test.txt.gz");
    //sample data
    String[] data = new String[] {
    "One",
    "Two",
    "Three",
    };
    //write the GZIPed file
    FileOutputStream fos = new FileOutputStream(testFile);
    GZIPOutputStream gzos = new GZIPOutputStream(fos);
    PrintWriter pw = new PrintWriter(gzos, true);
    for (int i = 0; i < data.length; i++) {
    pw.println(data);
    }
    pw.close();
    gzos.close();
    fos.close();

    ======================

    Here's the code that reads it back, one line at a time and stores them
    in an ArrayList:

    ======================
    //read the file
    FileInputStream fis = new FileInputStream(testFile);
    GZIPInputStream gzis = new GZIPInputStream(fis);
    BufferedReader br = new BufferedReader(new InputStreamReader(gzis));
    String nextLine = null;
    ArrayList al = new ArrayList();
    while ((nextLine = br.readLine()) != null) {
    //store each line in a Collection
    al.add(nextLine);
    }
    //print the contents of the Collection
    for (Iterator i = al.iterator(); i.hasNext();) {
    System.out.println(i.next());
    }


    Jim S.
    --
    Remove my extraneous mandibular appendages to reply via email.
     
    Jim Sculley, Jun 25, 2004
    #2
    1. Advertising

  3. gajo

    ak Guest

    > String line = "this is\na multi-line\ntext";
    >
    > Contents of testFile.gz (after being extracted by WinZip):
    > this is
    > a multi-line
    > text
    >
    > When I read it back, and write
    > System.out.println(line); I get:
    > this is a multi-line text
    >
    > When I add separators at the end of each line, I get
    > this is
    >
    > a multi-line
    >
    > text
    >


    please post complete compilable example.

    --
    http://uio.dev.java.net Unified I/O for Java
    http://reader.imagero.com Java image reader
     
    ak, Jun 26, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ober
    Replies:
    6
    Views:
    533
    Frank Mamone
    Sep 13, 2004
  2. Victor
    Replies:
    10
    Views:
    3,447
    Victor van Hillo
    Nov 21, 2004
  3. Owen

    cookie troubles

    Owen, Jan 7, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    314
    Exception
    Jan 7, 2005
  4. Bob [BVP]
    Replies:
    2
    Views:
    410
    Bob [BVP]
    Jan 21, 2005
  5. Replies:
    1
    Views:
    365
    bchandley
    May 23, 2005
Loading...

Share This Page