簡體   English   中英

Java BufferedWriter另存為操作

[英]Java BufferedWriter Save As operation

我正在嘗試在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();
    }
}

什么是實現這一目標的好方法?

您可以使用JFileChooser 這將為您提供一個“簡單”的用戶界面,讓用戶選擇文件(或文件名)。 然后,將您的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";
    }
}
  • 使用JFileChooser或您選擇的任何UI來獲取要創建的目標文件的完整路徑。
  • save方法中添加參數以獲取此路徑,並使用它代替myData.dat
  • 將文件路徑存儲在Main.class的字段中
  • 添加一個不帶參數的save ,它使用Main.class中存儲的路徑調用save參數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM