2

How I can read the content from a text file line by line? When I try to output the content, the newline character seems to be ignored from reading.

public class ReadFile { public static void main(String[] args) throws IOException { // TODO Auto-generated method stub String str= ""; //Reading content from file Scanner in = new Scanner(new FileReader("text.txt")); while(in.hasNextLine()){ str = str + in.nextLine(); str.concat("\n"); //Not working!!!!!!!!!!! } in.close(); //Writing content to another file PrintWriter out = new PrintWriter(new FileWriter("output.txt")); out.println(str); out.close(); } } 
9
  • Scanner.nextLine will strip the linebreak. PrintWriter.println will add it back. Where's the problem? Commented Apr 17, 2016 at 7:01
  • 2
    String is immutable. String.concat is conventionally written as + - it's both shorter and more readable. str += "\n". "\n" is a UNIX linebreak - this won't work on Windows. Commented Apr 17, 2016 at 7:02
  • Are you on windows ? you should try appending \r\n instead of only \n. Commented Apr 17, 2016 at 7:03
  • @11thdimension OP is not appending anything. Commented Apr 17, 2016 at 7:03
  • 2
    Instead of loading the whole file in memory in a huge string, using STring concatenation at each step which is slower and slower due to the copies of the bigger and bigger strings that need to be made, why don't you simply println() each read line to the PrintWriter: less memory, much faster, and simpler. Commented Apr 17, 2016 at 7:11

3 Answers 3

2

You are making mistake in the following line:

str.concat("\n"); 

Update it like below:

str = str.concat("\n"); 

I am giving updated program.

public class ReadFile { public static void main(String[] args) throws IOException { try (PrintWriter out = new PrintWriter(new FileWriter("output.txt")); Scanner in = new Scanner(new FileReader("text.txt"));) { while (in.hasNextLine()) { out.println(in.nextLine()); } } } } 
Sign up to request clarification or add additional context in comments.

Comments

1

You need to set the string to the new value after your concat operation.

str = str.concat("\n"); // or \r\n for Windows. 

Comments

-2

You are concatenating string which is not very efficient; use StringBuilder, also use platform independent line terminator.

Try this instead :

public class ReadFile { public static void main(String[] args) throws IOException { String str= ""; //Reading content from file Scanner in = new Scanner(new FileReader("text.txt")); StringBuilder str = new StringBuilder(); String newLine = System.getProperty("line.separator"); while(in.hasNextLine()){ str.append(in.nextLine()).append(newLine); } in.close(); //Writing content to another file PrintWriter out = new PrintWriter(new FileWriter("output.txt")); out.println(str.toString()); out.close(); } } 

3 Comments

You advise to use a platform-independent line terminator, but your code doesn't. It also doesn't compile.
Still doesn't compile.
And you haven't identified the actual problem.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.