By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
461,955 Members | 730 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 461,955 IT Pros & Developers. It's quick & easy.

read/write a file and '\n' problem

mickey0
100+
P: 142
Hi,
I have one problem; I must read a html like this;
Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <body>
  3. Hello
  4. </body>
  5. </html>
  6.  
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:
Expand|Select|Wrap|Line Numbers
  1. <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:
Expand|Select|Wrap|Line Numbers
  1.       File fin = new File(FILE_NAME);    
  2.                   FileReader in = new FileReader(fin);           
  3.                   istream =  new BufferedReader( in );                                               
  4.                   String line = null;                 
  5.                   while ( (line = istream.readLine()) != null  ) {
  6.                       //System.out.println(line);
  7.                       sb.append(line);
  8.                   }
  9.                               // here do changes on the sb
  10.                               String text = sb.toString();
  11.                   FileWriter fw = new FileWriter(new File ("out.txt") ); 
  12.                   out = new PrintWriter(fw); 
  13.                   out.println(text); 
  14.  
Thanks
Jun 22 '08 #1
Share this Question
Share on Google+
9 Replies

Expert 10K+
P: 11,448
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
Jun 22 '08 #2

mickey0
100+
P: 142
is there any other "read" method that don't get out '\n' ???
Jun 22 '08 #3

Expert 10K+
P: 11,448
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
Jun 22 '08 #4

mickey0
100+
P: 142
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?
Jun 22 '08 #5

Expert 10K+
P: 11,448
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
Jun 22 '08 #6

mickey0
100+
P: 142
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?
Expand|Select|Wrap|Line Numbers
  1. char buf[] = new char[100];
  2. int num = istream.read(buf, 0, 100);
  3.  
It's my code but I can't have a fixed dimesion for buf (I mean, 100)....
Can you suggest me more? thanks...
Jun 22 '08 #7

100+
P: 112
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:

Expand|Select|Wrap|Line Numbers
  1.                   while ( (line = istream.readLine()) != null  ) {
  2.                       //System.out.println(line);
  3.                       sb.append(line);
  4.                   }
you could use something like
Expand|Select|Wrap|Line Numbers
  1.  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
Jun 22 '08 #8

Nepomuk
Expert 2.5K+
P: 3,112
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:
Expand|Select|Wrap|Line Numbers
  1. read first line
  2. while (there is a next line)
  3. {
  4.    add '\n' to your String
  5.    read the next line and add it to the String
  6. }
or alternatively:
Expand|Select|Wrap|Line Numbers
  1. while(something)
  2. {
  3.    read next line
  4.    if there's a next line, add '\n'
  5.    else quit the loop
  6. }
Greetings,
Nepomuk
Jun 22 '08 #9

BigDaddyLH
Expert 100+
P: 1,216
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.
Jun 23 '08 #10

Post your reply

Sign in to post your reply or Sign up for a free account.