File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes I/O and Streams and the fly likes read file not include new line Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "read file not include new line" Watch "read file not include new line" New topic

read file not include new line

Sophia Choi
Ranch Hand

Joined: Mar 22, 2002
Posts: 106
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
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
subject: read file not include new line
It's not a secret anymore!