Tôi đã sử dụng mã bên dưới để ghi bản ghi vào một Tệp. Kỷ lục có thể được viết trong file, nhưng được nối thêm vào một Line, mỗi khi tôi gọi phương thức ví dụ này:Java FileWriter cách viết vào dòng tiếp theo
Xin chào worldhello worldhello worldhello Thế giới
Làm thế nào tôi có thể sửa đổi các mã để đầu ra nhìn như dưới đây, để đọc văn bản tôi có thể sử dụng line.hasNextLine() để kiểm tra?
Hello World
Hello World
Hello World
Hello World
// Create file
FileWriter fstream = new FileWriter(fileName, true);
BufferedWriter out = new BufferedWriter(fstream);
out.write(c.toString());
//Close the output stream
out.close();
// Code I used to read record I am using | as a seperator name and id
String fileName = folderPath + "listCatalogue.txt";
String line = "";
Scanner scanner;
String name, id;
scanner = new Scanner(fileName);
System.out.println(scanner.hasNextLine());
while (scanner.hasNextLine()) {
line = scanner.nextLine();
System.out.println(line);
StringTokenizer st = new StringTokenizer(line, "|");
name = st.nextToken();
id = st.nextToken();
catalogues.add(new Catalogue(name, id));
}
Vui lòng không sử dụng '\ n' bộ tách dòng sẽ khác nhau trên nền tảng – Kris
Cảm ơn nó hoạt động. – user236501