如何让一个方法接受两种类型的列表作为参数?

时间:2017-05-02 05:46:17

标签: java file methods

我有两个类,Book和User,这两个arraylist实现List,如示例代码片段中所示,具有不同的数据类型,我有问题做一个读取文件和写入文件的通用方法。方法不起作用,数据未保存到文件中。我猜这个参数出了问题 "列出arrayname"

public static List<Book> books = new ArrayList<Book>();
public static List<User> users = new ArrayList<User>();
//Sample array list

public static void writeFile(String filename,List<Object> arrayname){

    File file = new File(filename+".ser");  

    try {
        FileOutputStream fos = new FileOutputStream(file);
        ObjectOutputStream oos = new ObjectOutputStream(fos);

        oos.writeObject(arrayname);

        oos.close();
        fos.close();

    }catch(IOException i) {
     System.out.println("The file " + file.getPath() + " was not found.");
    }

}

public static void readFile(String filename, List<?> arrayname){

    File file = new File(filename+".ser");   
    try {


       FileInputStream fis = new FileInputStream(file);
       ObjectInputStream ois = new ObjectInputStream(fis);

        arrayname = (List<?>)ois.readObject();  

       ois.close();
       fis.close();

    }catch(IOException i) {
        System.out.println("The file " + file.getPath() + " was not found.");
    } catch (Exception e) {
          e.printStackTrace();
      }
}

1 个答案:

答案 0 :(得分:0)

您发布的代码中没有错误。我试图运行它,它的工作原理。这是程序:

var paper = new joint.dia.Paper({
    background: {
       color: '#ff0000'
    }                                                                         
});

打印:

class Book implements Serializable {
    private String title;
    private String author;

    public Book() {
    }

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

    @Override
    public String toString() {
        return "Book [title=" + title + ", author=" + author + "]";
    }

}

public class Snippet {
    public static void main(String[] args) {
        List<Book> list = Arrays.asList(new Book("book1", "author1"), new Book("book2", "author2"),
                new Book("book3", "author3"));
        writeFile("C:\\books", list);
        List<Book> list2 = readFile("C:\\books", list);
        list2.forEach(System.out::println);
    }

    public static List<Book> readFile(String filename, List<?> arrayname) {

        File file = new File(filename + ".ser");
        try {

            FileInputStream fis = new FileInputStream(file);
            ObjectInputStream ois = new ObjectInputStream(fis);

            List<Book> books = (List<Book>) ois.readObject();

            ois.close();
            fis.close();

            return books;
        } catch (IOException i) {
            System.out.println("The file " + file.getPath() + " was not found.");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void writeFile(String filename, List<Book> arrayname) {

        File file = new File(filename + ".ser");

        try {
            FileOutputStream fos = new FileOutputStream(file);
            ObjectOutputStream oos = new ObjectOutputStream(fos);

            oos.writeObject(arrayname);

            oos.close();
            fos.close();

        } catch (IOException i) {
            System.out.println("The file " + file.getPath() + " was not found.");
        }

    }