Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

println Vs newLine method

 
Ramakrishnan Ponmudi
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am copy a file from one location to another. if i am using PrintWriter's
println() its working fine. when i am using newLine() all lines are printed in the same line itself.
why it is not working properly?

BufferedReader in = new BufferedReader(new FileReader(filePath + "\\" + fileName));
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(pathName+ "\\" + fileName)));
String strLine = null;
while( (strLine=in.readLine())!= null)
out.println(strLine);

BufferedReader in = new BufferedReader(new FileReader(filePath + "\\" + fileName));
BufferedWriter out = new BufferedWriter(new FileWriter(pathName+ "\\" + fileName));
String strLine = null;
while( (strLine=in.readLine())!= null) {
out.write(strLine);
out.newLine();
}
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[RT]: i am copy a file from one location to another.

I would recommend you forget about Readers and Writers here, and use a FileInputstream and FileOutputStream instead. Or use a FileChannel. Oversimplfied examples are here and here. Using Readers and Writers can result in complications if you don't know the details of the character encoding to be used with a file, and possible complications with line separators when going from Unix to Windows or vice-versa. If you simply want to make an identical copy of a file, ignore these issues and copy the raw bytes - that's what FileInputStream, FileOutputStream, and FileChannel allow you to do.
[ November 30, 2005: Message edited by: Jim Yingst ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic