如何将arraylist保存并打印到文件中?

pes8fvy9  于 2021-07-03  发布在  Java
关注(0)|答案(3)|浏览(327)
import java.util.*;
import java.io.*;

class Book implements Serializable {
private static final long serialVersionUID = 1L;
private String name;
private String author;

public String getAuthor() {
    return author;
}

public Book(String name, String author) {
    this.name = name;
    this.author = author;
}

public void disPlay() {
    System.out.print("Book name : " + name);
    System.out.println("\tAuthor name : " + author);
}
}

public class Print {
public static void main(String[] args) throws Exception {
    writeList();
    List<Book> list = readList();
    for (Book obj : list)
        System.out.println(obj);
}

public static List<Book> readList() throws Exception {
    ObjectInputStream in = new ObjectInputStream(new FileInputStream("object.dat"));
    @SuppressWarnings("unchecked")
    List<Book> readObject = (List<Book>) in.readObject();
    in.close();
    return readList();
}

public static void writeList() throws Exception {
    List<Book> list = new ArrayList<>();
    ObjectOutputStream out = null;
    try (Scanner scan = new Scanner(System.in)) {
        System.out.print("Enter the book, author name : ");
        String name = scan.next();
        String author = scan.next();
        list.add(new Book(name, author));
        System.out.print("If you want to save to the list -1, if you don't want, enter 1 : ");
        int choice = scan.nextInt();
        out = new ObjectOutputStream(new FileOutputStream("object.dat"));
        out.flush();
        out.close();
        System.out.print("Save the list to a file");
    }
}
}

我尝试获取存储在文件(object.dat)中的arraylist对象,将book对象保存在arraylist中,然后在程序结束之前将arraylist保存在book对象存储在文件中的位置。
我想要的结果是,[程序首次运行时的结果]

There are no saved values

Enter the book, author name : Harrypotter jkrowling
If you want to save to the list -1, if you don't want, enter 1 : -1
Save the list to a file

以及,[第二次及以后的结果]

---Outputs the value stored in the file name---
Book name : Harrypotter
Author name : jkrowling

Enter the book, author name : Twilight StephenieMeyer
If you want to save to the list -1, if you don't want, enter 1 : 1
Save the list to a file

我想这样打印出来,但我该怎么办?

iszxjhcz

iszxjhcz1#

为了简化代码并避免样板文件,我建议您使用特定的工具和lib来处理文件。
有一篇关于使用spring:spring资源加载和读取文件的好文章
要将内容写入文件,请参阅“apache commons io”库提供了有用的帮助程序方法,如:
fileutils.write(新文件(“file.txt”),“content text”,“utf-8”);
无论如何,研究这些工具,然后考虑在其中采用您的需求。

tag5nh1u

tag5nh1u2#

try {
      FileWriter writer = new FileWriter("Book.txt");
      for(int i = 0 ; i < arraylist.size(); i++){
              writer.write(arraylist.get(i).getName();
              writer.write(arraylist.get(i).getAuthor();
      }
      myWriter.close();
    } catch (IOException e) {
      e.printStackTrace();
    }
cngwdvgl

cngwdvgl3#

下面是用java编写文件的一般方法。

import java.io.FileWriter;   // Import the FileWriter class
try {
      FileWriter myWriter = new FileWriter("filename.txt");
      myWriter.write("Write into file here! ");
      myWriter.close();
      System.out.println("Successfully wrote to the file.");
    } catch (IOException e) {
      System.out.println("An error occurred.");
      e.printStackTrace();
    }

相关问题