Java将文件读入对象的arraylist并返回该arraylist

时间:2017-05-13 17:21:24

标签: java serialization arraylist

我需要编写一个包含两个静态方法的类:writeFilereadFile。但是,在我执行readFile()之后,它什么也没有返回。

class writereadFile {
    public static void writeFile(ArrayList<Object> list, File file){
        try {
            try (FileOutputStream fos = new FileOutputStream(file);ObjectOutputStream oos = new ObjectOutputStream(fos)) {
                oos.writeObject(list);
                oos.close();
            }
        }catch(IOException e){e.getMessage();}
    }

    public static ArrayList<Object> readFile(ArrayList<Object>list, File file){
        try {
            try (FileInputStream fis = new FileInputStream(file);ObjectInputStream ois = new ObjectInputStream(fis)) {
                Object o = ois.readObject();
                list = (ArrayList<Object>) o;
                ois.close();
            }
        }catch(IOException | ClassNotFoundException e){e.getMessage();}  
        System.out.println(list);
        return list;
    } 
}

修改 这是我的课程测试。如果您需要自定义对象只是注释,我的对象是自定义对象的arraylist。

class main {
    public static void main(String[] args) {
        Date date = new Date();
        Book b1 = new Book("abc", "Phi", true, date, null);
        Book b2 = new Book("cba", "Someone", true, date, null);
        Books booklist = new Books();
        booklist.add(b1);
        booklist.add(b2);

        File filetoDo = new File("book.txt");

        //write arraylist into file
        writereadFile.writeFile(booklist, filetoDo);

        //clear the arraylist
        booklist.clear();

        //read book from file
        writereadFile.readFile(booklist, filetoDo);
        System.out.println(booklist);
    }    
} 

3 个答案:

答案 0 :(得分:1)

您的测试应为:

bookList = writereadFile.readFile(booklist, filetoDo);

顺便说一下,你应该真正重构你的readFile方法:

public static ArrayList<Object> readFile(File file)

您无法像这样修改参数 reference ,因为Java总是 pass-by-value 调用语义。 (您可以修改函数内的list参数内容,但这不是您正在做的事情。)

答案 1 :(得分:0)

如果您使用的是Java 8,请尝试使用Streams:

public static readFile(String filePath) {
    List<Object> list = new ArrayList<>();

    try (Stream<String> stream = Files.lines(Paths.get(filePath))) {
        stream.forEach(list::add);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return list;
}

答案 2 :(得分:0)

我自己玩这个主题,所以下面你可以找到一些可能对你有帮助的代码片段。 示例非常简短,因此我希望您不要在代码中使用e.printStackTrace():)

public class ExternalIO {

private ExternalIO() {
}

public static ObjectOutputStream objectOutputStream(String basePath, String pathToFile) throws IOException {
    FileOutputStream fileOutputStream = new FileOutputStream(createFileIfDoesNotExist(absolutePath(basePath, pathToFile)));
    return new ObjectOutputStream(fileOutputStream);
}

public static ObjectInputStream objectInputStream(String basePath, String pathToFile) throws IOException {
    FileInputStream fileInputStream = new FileInputStream(absolutePath(basePath, pathToFile));
    return new ObjectInputStream(fileInputStream);
}

private static File createFileIfDoesNotExist(String absolutePath) throws IOException {
    File file = new File(absolutePath);
    if (file.exists()) {
        return file;
    }

    file.getParentFile().mkdirs();
    file.createNewFile();
    return file;
}

private static String absolutePath(String basePath, String pathToFile) {
    return Paths.get(basePath, pathToFile).toAbsolutePath().toString();
}

}

输出用法:

List<ItemType> input = null; //create your input list here
try (ObjectOutputStream objectOutputStream = ExternalIO.objectOutputStream(CONFIG, FILENAME)) {
    objectOutputStream.writeObject(input);
} catch (Exception e) {
    e.printStackTrace();
}

输入用法:

try (ObjectInputStream objectInputStream = ExternalIO.objectInputStream(CONFIG, FILENAME)) {
    return (List<ItemType>) objectInputStream.readObject();
} catch (Exception e) {
    e.printStackTrace();
}

希望有所帮助; )