There is such a code.

File f = new File("aa.txt"); File f1 = new File("bb.txt"); OutputStream out = new FileOutputStream(f1); int a; BufferedReader br = new BufferedReader(new FileReader(f)); String str; ArrayList<String> arr = new ArrayList<String>(); while((str=br.readLine())!=null){ arr.add(str); } str=""; for(String s : arr){ str+=s+"\n"; } System.out.println(str); byte[] b = str.getBytes(); out.write(b); out.close(); 

The contents of the file "aa" - a few lines. Accordingly, I would like to transfer the text (preformatted it) to the file "bb". The main question is how to write information with line breaks to a file. I tried to create a string with control characters, but apparently they do not read the stream.

  • will help something from here? ru.stackoverflow.com/a/533079/191482 - Alexey Shimansky
  • OS what? Under Linux, for example, your code works without problems, "\n" are written and do not squeak. - PinkTux
  • one
    @PinkTux Instead of \n it is better to use Environment.NewLine - Bulson
  • @Bulson, "Under Linux, for example . " - PinkTux
  • 2
    @ Sergey sorry brother, I am not from your world of java, but accidentally got here from the world of dotnet ... It’s better still to point in tags on which language the question is, not just the files and filestream - Bulson

1 answer 1

better to use StringBuilder . System.lineSeparator() or System.getProperty("line.separator") determines the character you need depending on the OS.

  StringBuilder sb = new StringBuilder(); for(String s : arr){ sb.append(s).append(System.lineSeparator()); } System.out.println(sb.toString()); byte[] b = sb.toString().getBytes(); out.write(b); out.close(); 

UPD

Look at what character you do not see. The following code will let you know the character code and how it looks

  byte[] bytes = FileUtils.readFileToByteArray(file); for(int i =0; i< bytes.length ; i++){ System.out.println("As the bytem value: "+ bytes[i]);//as the numeric byte value System.out.println("Converted as char to printiong to the screen: "+ String.valueOf((char)bytes[i])); } 
  • It did not work, the same symbol - Sergey
  • @ Sergey updated the answer. Try and say what character you don’t drive - Senior Pomidor
  • As the bytem value: 13 Converted as the byte value: 10 Converted as the byte value: 10 for the screen: these are the escape sequences for the line feed (10) and the carriage return (13). I googled. It just doesn’t work out a mini-issue there, while reading the first two characters in the lines, writes it as “Eya”, but then overwrites it normally. - Sergey
  • @ Sergey, that is, there is no problem, but there is a question why "ei"? >> writes the first two characters as "eya" where does it write? how do you look? - Senior Pomidor
  • Problem still exists. But on top of that, I don’t understand the mechanism of the formation of the first two bytes) there it writes to the line at the very beginning (yu), but writes it to the file afterwards without these bytes. Perhaps this is interpreted as a specific control character, I do not know) Wrong, writes to the beginning (yu) - Sergey