0

我正在尝试在 Java 中模拟另存为功能。我想为它选择一个文件名,因为我之前所做的代码只保存到

myData.dat

这在我的 Main.Class 的菜单中使用,它将查找

else if (option.compareTo("8") == 0){
    manualLib.save();}


  public void save(){
    String content = "";
    for (int i = 0; i < library.size(); i++){
        for (int bar = 0; bar < library.get(i).size(); bar++){
            content += library.get(i).get(bar).getSerial() + "\n";
            content += library.get(i).get(bar).getTitle() + "\n";
            content += library.get(i).get(bar).getAuthor() + "\n";
            content += library.get(i).get(bar).onLoan() + "\n";
            content += library.get(i).get(bar).getBorrower() + "\n";
        }
    }

    Writer output;
    try {
        output = new BufferedWriter(new FileWriter("myData.dat"));
        try {
              output.write(content);
            }
        finally {
              output.close();
              System.out.println("Successfully saved to myData.dat file.");
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

实现这一目标的好方法是什么?

4

2 回答 2

1

您可以使用JFileChooser。这将为您提供一个“简单”的 UI,让用户选择文件(或文件名)。然后,您将用 .myData.dat返回的值替换您的值chooser.getSelectedFile().getName()

我还没有编译这个,但你的代码最终应该看起来像:

public void save(){
    String content = "";
    for (int i = 0; i < library.size(); i++){
        for (int bar = 0; bar < library.get(i).size(); bar++){
            content += library.get(i).get(bar).getSerial() + "\n";
            content += library.get(i).get(bar).getTitle() + "\n";
            content += library.get(i).get(bar).getAuthor() + "\n";
            content += library.get(i).get(bar).onLoan() + "\n";
            content += library.get(i).get(bar).getBorrower() + "\n";
        }
    }

    Writer output;

    JFileChooser chooser = new JFileChooser();
    DatFilter filter = new DatFilter();
    filter.addExtension("dat");
    filter.setDescription(".dat files");
    chooser.setFileFilter(filter);
    int returnVal = chooser.showOpenDialog(null);
    String fileName = new String();
    if(returnVal == JFileChooser.APPROVE_OPTION) {
    fileName=chooser.getSelectedFile().getName();
    }

    try {
        output = new BufferedWriter(new FileWriter(fileName));
        try {
              output.write(content);
            }
        finally {
              output.close();
              System.out.println("Successfully saved to "+fileName+" file.");
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

然后也上课

public class DatFilter extends FileFilter {

    //should accept only dirs and .dat files
    public boolean accept(File f) {
        if (f.isDirectory()) {
            return true;
        }

    String extension = null;
        String s = f.getName();
        int i = s.lastIndexOf('.');

        if (i > 0 &&  i < s.length() - 1) {
            extension = s.substring(i+1).toLowerCase();
        }

        if (extension != null) {
            if (extension.equals("dat"){
                    return true;
            } else {
                return false;
            }
        }

        return false;
    }

    //The description of this filter
    public String getDescription() {
        return ".dat Files";
    }
}
于 2013-01-18T17:30:33.267 回答
0
  • 使用 JFileChooser 或您选择的任何 UI 来获取要创建的目标文件的完整路径。
  • 向您的方法添加一个参数以save获取此路径,并使用它代替myData.dat
  • 将文件路径存储在 Main.class 的字段中
  • 添加一个不带参数的参数,它使用存储在 Main.class 中的路径save调用参数。save
于 2013-01-18T17:37:49.320 回答