通过GUI输出/输入Java文件

时间:2010-12-10 11:26:03

标签: java file-io

我有三个类libraryDB,libraryItems和libraryGUI。 libraryDB()本质上是一个哈希映射,其键为book barcodes / ISBN,其值为libraryItems,它包含两个String参数,因此需要两个String参数:Title和Author。

我在GUI中设置了一个JFileChooser来保存,但是我的save()和open()方法都给出了问题。我想要它设置,以便在保存时每个libraryDB对象都有自己的3行(分别为Barcode,Title,Author各一行)。我尝试通过阅读每一行来加载它们,这是我为此写的代码:

//Suppose to construct a LibraryDB by reading one from a previously-saved file.
    public LibraryDB (File file) throws IOException {
        Scanner readFile = new Scanner(file);
        int barcode;
        String title;
        String author;

        while (readFile.hasNext()){
            barcode = Integer.parseInt(readFile.nextLine());
            title   = readFile.nextLine();
            author  = readFile.nextLine();
            LibraryItem authorTitleValues = new LibraryItem(title,author);
            this.libraryItems.put(barcode, authorTitleValues);
            }
    }
//Trying to save to text file, where for each object n there are 3n lines.
    public void save(File file) throws IOException {
    PrintStream writer = new PrintStream(file);
    for (Iterator<Integer> localIterator = libraryItems.keySet().iterator(); 
    localIterator.hasNext();){ 
        int barcode = ((Integer)localIterator.next()).intValue();
        writer.println(barcode);
        writer.println((libraryItems.get(Integer.valueOf(barcode))).getTitle());
        writer.println((libraryItems.get(Integer.valueOf(barcode))).getAuthor());
        }
    }

您可以提供的有助于我成功保存/打开的任何帮助或见解将非常感谢!谢谢!

更明确,每当我将libraryDB保存到文件时,我都无法稍后再回来打开文件?

3 个答案:

答案 0 :(得分:0)

在结束flush()功能之前,您应该close()PrintStream save。我不确定这是不是问题,因为你的描述不太准确,但无论如何都要这样做。

答案 1 :(得分:0)

文件流和编写器必须在写入结束时显式关闭 - 否则它们将锁定文件。

public void save(File file) throws IOException {
    PrintStream writer = new PrintStream(file);
    try {
        for (Iterator<Integer> localIterator = libraryItems.keySet().iterator(); localIterator.hasNext();) {
            int barcode = ((Integer) localIterator.next()).intValue();
            writer.println(barcode);
            writer.println((libraryItems.get(Integer.valueOf(barcode))).getTitle());
            writer.println((libraryItems.get(Integer.valueOf(barcode))).getAuthor());
        }
        writer.flush();
    } finally {
        writer.close();
    }
}

答案 2 :(得分:0)

所以,我只是忘了重新发布libraryDB!? Grrr ...大声笑我不认为编译器会抱怨,因为它已经被声明了。但是,从文件中读取的信息只是遗忘或者其他东西,因为没有任何对象可以放入。至少,这就是我认为正在发生的事情。谢谢你的帮助。这是我的解决方案:

public LibraryDB (File file) throws IOException {
//this next line was what I was missing...sigh.
    this.libraryItems = new HashMap<Integer, LibraryItem>();
        Scanner readFile = new Scanner(file);
        int barcode;
        String title;
        String author;
        while (readFile.hasNext()){
            barcode = Integer.parseInt(readFile.nextLine());
            title   = readFile.nextLine();
            author  = readFile.nextLine();
            LibraryItem authorTitleValues = new LibraryItem(title, author);
            libraryItems.put(Integer.valueOf(barcode), authorTitleValues);
            }
        readFile.close();
    }