From one text file to the second, you need to rewrite all the lines, inserting its length at the end of each line. Here is the listing

public static void main(String[] args) { try(FileWriter writer = new FileWriter("C:\\prg1\\Letter.txt", false)){ BufferedReader reader = new BufferedReader(new FileReader("C:\\prg1\\test2.txt")); String tmp; while( (tmp = reader.readLine())!= null ){ writer.write(tmp); writer.write(tmp.length()); writer.append( '\n');// делал также writer.write( '\n'); } writer.close(); reader.close(); catch(IOException ex){ System.out.println(ex.getMessage()); } } 

The problem is that in the resulting file "Letter.txt" transitions to a new line are missing, that is, writer.append ('\ n') or writer.write ('\ n') does not work.

  • If you are given an exhaustive answer, mark it as correct (a daw opposite the selected answer). - Nicolas Chabanovsky

2 answers 2

writer.write(System.lineSeparator());

or

writer.write(System.getProperty("line.separator"));

PS: since you are using the try-with-resource construct, use writer.close(); not necessary. You can also transfer the ad reader to try(...) :

 try( FileWriter writer = new FileWriter("C:\\prg1\\Letter.txt", false); FileReader fr = new FileReader("C:\\prg1\\test2.txt"); BufferedReader reader = new BufferedReader(fr) ) { ... // В блоке try теперь не нужно вызывать методы close(). } 

    In fact, a line break is two characters \r\n . But many text editors of any of them can be understood as a line feed. I suppose you open the final file with a notepad, it just doesn’t understand a single \n