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); } } }
FileWorker? And what's inside?\ndoesn't work? - Alexey Shimansky