read/write a file and '\n' problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mickey0
    New Member
    • Jan 2008
    • 142

    read/write a file and '\n' problem

    Hi,
    I have one problem; I must read a html like this;
    Code:
    <html>
    <body>
    Hello
    </body>
    </html>
    I read this in a string (correct?) and after I have to do some changes to it and write it on disk but in this way:
    Code:
    <html>\n<body>\nhello\n</body>\n</body>
    There, there's the '\n' because in the original file <body> was in another line of <html>
    How can I do this? This my code for read and write:
    Code:
          File fin = new File(FILE_NAME);    
                      FileReader in = new FileReader(fin);           
                      istream =  new BufferedReader( in );                                               
                      String line = null;                 
                      while ( (line = istream.readLine()) != null  ) {
                          //System.out.println(line);
                          sb.append(line);
                      }
                                  // here do changes on the sb
                                  String text = sb.toString();
                      FileWriter fw = new FileWriter(new File ("out.txt") ); 
                      out = new PrintWriter(fw); 
                      out.println(text);
    Thanks
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    The readLine() method returns a String without the terminating end of line character
    sequence so you have to append that sequence yourself after you have appended
    the line String to your StringBuffer (or StringBuilder). Look at the system properties
    for the correct value of the end of line character sequence.

    kind regards,

    Jos

    Comment

    • mickey0
      New Member
      • Jan 2008
      • 142

      #3
      is there any other "read" method that don't get out '\n' ???

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by mickey0
        is there any other "read" method that don't get out '\n' ???
        Sure, you can read parts (or all) of the file directly into a char[] of CharBuffer if
        you want. Read the API documentation for details. Note though that an end of
        line character sequence need not be a single \n character so don't anticipate
        on that.

        kind regards,

        Jos

        Comment

        • mickey0
          New Member
          • Jan 2008
          • 142

          #5
          to be sincer, for me it's not important to read the file line by line; how can I put the entire file into a String in a hit?

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            Originally posted by mickey0
            to be sincer, for me it's not important to read the file line by line; how can I put the entire file into a String in a hit?
            Read the API documentation for the BufferedReader and Reader classes.
            You'll find the read() methods you need.

            kind regards,

            Jos

            Comment

            • mickey0
              New Member
              • Jan 2008
              • 142

              #7
              hello,
              read() is ok; but as you can see I have ither problem because I'm a newbie to java; doesn't exist any function like c++ seekg() to seek at the end of my file and retrive the number of characters it has?
              Code:
              char buf[] = new char[100];
              int num = istream.read(buf, 0, 100);
              It's my code but I can't have a fixed dimesion for buf (I mean, 100)....
              Can you suggest me more? thanks...

              Comment

              • drsmooth
                New Member
                • Oct 2007
                • 112

                #8
                if you use readLine(), you can assume that there is a '\n' character after each call, im not sure how that fits into your program, but if you had a loop like:

                Code:
                                  while ( (line = istream.readLine()) != null  ) {
                                      //System.out.println(line);
                                      sb.append(line);
                                  }
                you could use something like
                Code:
                 line+= '\n' ;
                prior to appending it to whatever you are doing.

                im not sure if that helps, but i hope it did

                good luck,
                ken

                Comment

                • Nepomuk
                  Recognized Expert Specialist
                  • Aug 2007
                  • 3111

                  #9
                  Originally posted by drsmooth
                  if you use readLine(), you can assume that there is a '\n' character after each call...
                  That is not 100% correct - there won't be a '\n' character after the last line. So you should use something like this:
                  Code:
                  read first line
                  while (there is a next line)
                  {
                     add '\n' to your String
                     read the next line and add it to the String
                  }
                  or alternatively:
                  Code:
                  while(something)
                  {
                     read next line
                     if there's a next line, add '\n'
                     else quit the loop
                  }
                  Greetings,
                  Nepomuk

                  Comment

                  • BigDaddyLH
                    Recognized Expert Top Contributor
                    • Dec 2007
                    • 1216

                    #10
                    Originally posted by mickey0
                    hello,
                    read() is ok; but as you can see I have ither problem because I'm a newbie to java; doesn't exist any function like c++ seekg() to seek at the end of my file and retrive the number of characters it has?
                    Don't forget that the number of bytes is not necessarily the number of chars -- for example, my favorite encoding for HTML, UTF-8.

                    Comment

                    Working...