Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

read file not include new line

 
Sophia Choi
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have tried to write data into a file.So, I read the file first using Buffered Reader and save them into a 2D array. Then, I use PrintWriter and write them into a file. In order to have a good looking, I use println.
But, when I read that file again, the newline is included and counted to be a line. How can I ignore it?
Thanks.
-----------
Code:
FileReader fr = new FileReader(path + fileName);
BufferedReader br = new BufferedReader(fr);
String Line = br.readLine();
while(Line != null )
{
//System.out.println(Line + "<BR>"); //output
if(row == 0) find_col_num(Line);
sb = sb.append( Line);
sb = sb.append("\n"); //new line for screen
if(row < max_rows || Line != "\n" ){
System.out.println(row+":"+col+">"+Line);
find_col_data(row,Line);
row = row + 1;
}

Line = br.readLine(); //read next

}
--------------------
FileWriter fw = new FileWriter(path + fileName);
BufferedWriter bw = new BufferedWriter(fw);
PrintWriter pw = new PrintWriter(bw);
pw.println(sb.toString());
//bw.newLine(); //next line ,counted 1 line
pw.println(input);
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic