Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing 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?
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" ){
row = row + 1;

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

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