There is a part of the code that adds each element of the collection to a text file, but it is necessary that each element be from a new line, how to do this?

String prev_text = ""; for (ToyBlocks toyblocks : blocks){ String text = prev_text+"Назва товара: "+toyblocks.name+ ", кол-во кубиков в наборе: " + toyblocks.amount + ", цена: "+toyblocks.uah + "дол." +toyblocks.kopeck+"центов"+"\n"; prev_text = text; String fileName = "G://Результати введения и сортировки данних.txt"; FileWorker.write(fileName, text); } 

FileWorker code:

 import java.io.File; import java.io.IOException; import java.io.PrintWriter; public class FileWorker { public static void write(String fileName, String text) { File file = new File(fileName); try { if(!file.exists()){ file.createNewFile(); } PrintWriter out = new PrintWriter(file.getAbsoluteFile()); try { out.print(text); } finally { out.close(); } } catch(IOException e) { throw new RuntimeException(e); } } } 
  • at least tell me what's wrong? - Muscled Boy
  • And who is FileWorker ? And what's inside? \n doesn't work? - Alexey Shimansky
  • doesn't work \ n - Muscled Boy
  • Need String Builder? - Muscled Boy
  • @ Alexey Shimansky, or is this not the case? - Muscled Boy

1 answer 1

@ Alexey Shimansky, you excuse my ignorance, but nothing comes of it. All day I read something, reread and nothing comes out.

 import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; public class FileWorker { public static void write(String fileName, String text) { // параметри путь и текст try { FileWriter writer = new FileWriter(fileName, false); // создаем райтер с параметрами путь и значаение false, означающее. что файл будет перезаписываться BufferedWriter bufferWriter = new BufferedWriter(writer); // передаем потоку записи райтер? bufferWriter.write(text); // записываем текст bufferWriter.write("\n"); // переход на новую строку bufferWriter.close();// закрываем поток } catch (IOException e) { System.out.println(e); } } } 

I understand that you already zadolbal. Apparently the topic of this self-study was too confusing for me.

  • Those who do nothing and want to do everything for them)) This is not your case) ..... Well, in the end is the answer?)) It is better to do a new line like this: bufferWriter.newLine(); then most likely in Unix will be replaced with \n and on Windows with \r\n ... probably)) - Aleksey Shimansky
  • @ Alexey Shimansky, sincerely grateful to you for working with me. But the problem did not disappear ( - Muscled Boy
  • @ Alexey Shimansky, I don’t even guess what the problem is - Muscled Boy
  • @ Alexey Shimansky, read on the Internet about the newLine () method that you proposed, everything seems to be written correctly, but not everything ( - Muscled Boy
  • one
    Most likely because adding takes place in a loop, including creating a stream and closing it .... I’d personally like the method to determine which input would accept a sheet of lines and add them to the loop, but open and close the stream only once ........ here is an example: ideone.com/XiwsQ9 is quite working .... where the write method should of course be in the class FileWorker - Alexey Shimansky