I have a problem with ArrayList - there is a Library class, it has a list of books that are in a text file and need to be driven into ArrayList. It is necessary that ArrayList read data from a file in the format Name, Year, Author.
For entering into the file, I registered a method that basically writes text information from the console to a text file. But everything would be fine if I just had to write text into a .txt file. But there is nothing simple in our business, and I need to specify the formatted data entry into a text file. Here is the method I use to write.
if (sc==6){ System.out.println("ΠΠ²Π΅Π΄ΠΈΡΠ΅ Π½ΠΎΠ²ΡΡ ΠΊΠ½ΠΈΠ³Ρ:"); String filePath = "Lib.txt"; Scanner console = new Scanner(System.in); try {FileWriter out = new FileWriter(filePath, true); BufferedWriter bufferWriter = new BufferedWriter(out); bufferWriter.newLine(); bufferWriter.write(console.next()); bufferWriter.close(); } catch (IOException e) { System.err.println(e); } } Here is an example of the method I use to read.
public static void main(String[] args) throws IOException { Bks books = new Bks (); String line = null; ArrayList<Bk> list = new ArrayList<>(); try { BufferedReader reader = new BufferedReader(new InputStreamReader( new FileInputStream("Lib.txt"), "Cp1251")); while ((line = reader.readLine()) != null) { String[] items = line.split(","); list.add(new Bk(items[0], items[1], Integer.parseInt(items[2]))); } reader.close(); } catch (Exception ex) { ex.printStackTrace(); } books.setArr(list); Advise what I need to add here, and what to change?