簡體   English   中英

Java-文件保存不起作用

[英]Java - File save does not work

我想將ArrayList的內容保存到文件中(用戶應選擇.txt的位置),但是由於該代碼無法正常工作,因此我不確定該怎么做。

你有什么想法嗎?

package vizsgaquiz;

import java.io.File;
import java.util.ArrayList;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.FileChooser;
import javafx.stage.Stage;


public class VizsgaQuiz extends Application {

    ArrayList<String> kerdeslista = new ArrayList<String>();
    String a ="a";


    @Override
    public void start(Stage stage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("Foablak.fxml"));

        Scene scene = new Scene(root);

        stage.setScene(scene);
        stage.setTitle("Quiz Játék");
        stage.show();
        save();
    }

    public void save(){
                kerdeslista.add(a);

                FileChooser fileChooser = new FileChooser();

              //Set extension filter
              FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter("TXT files (*.txt)", "*.txt");
              fileChooser.getExtensionFilters().add(extFilter);

              //Show save file dialog
              File file = fileChooser.showSaveDialog(stage);

              if(file != null){
                  SaveFile(kerdeslista, file);
              }
    }



    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }

}

有幾個問題可能導致此問題。 對於初學者,此代碼無法編譯。 這是因為您在方法save中調用變量階段 ,而方法中的“死”是在start中 要調用save 階段 ,您需要傳遞它以保存或將其另存為字段。 第二個問題是方法SaveFile似乎不存在。 SaveFile的示例可能類似於下面包含的代碼。 請注意,我已更新方法save進入舞台,並且將方法SaveFile的名稱更改為saveFile以匹配Java命名約定。 另外,下面的代碼將arraylist的每個值打印在新行上,而您可能不需要。

    package vizsgaquiz;

    import java.io.File;
    import java.io.PrintWriter;
    import java.util.ArrayList;
    import javafx.application.Application;
    import javafx.fxml.FXMLLoader;
    import javafx.scene.Parent;
    import javafx.scene.Scene;
    import javafx.stage.FileChooser;
    import javafx.stage.Stage;


    public class VizsgaQuiz extends Application {

      ArrayList<String> kerdeslista = new ArrayList<String>();
      String a ="a";


      @Override
      public void start(Stage stage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("Foablak.fxml"));

        Scene scene = new Scene(root);

        stage.setScene(scene);
        stage.setTitle("Quiz Játék");
        stage.show();
        save(stage);
      }

      public void save(Stage stage){
        kerdeslista.add(a);

        FileChooser fileChooser = new FileChooser();

        //Set extension filter
        FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter("TXT files (*.txt)", "*.txt");
        fileChooser.getExtensionFilters().add(extFilter);

        //Show save file dialog
        File file = fileChooser.showSaveDialog(stage);

        if(file != null){
          saveFile(kerdeslista, file);
        }
      }


      /**
       * @param args the command line arguments
       */
      public static void main(String[] args) {
        launch(args);
      }

      public static void saveFile(ArrayList<String> list, File file) {
        try {
          PrintWriter out = new PrintWriter(file);
          for (String val : list)
            out.println(val + "\n");
          out.close();
        } catch (Exception e) {
          e.printStackTrace();
        }
      }

    }

這是一個如何將新文件保存到FileDialog的指定目錄和文件名的示例,FileDialog是從Strings的向量中提取的。

 public static void SaveFileTo(Vector<String> myLines) {
            FileOutputStream f = null;
            DataOutputStream h = null;
            FileDialog d = new FileDialog(new JFrame(), "Save", FileDialog.SAVE);
            d.setVisible(true);
            String dir;
            dir = d.getDirectory();
            File oneFile = new File(dir + d.getFile());
            try {
                oneFile.createNewFile();
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            try {
                f = new FileOutputStream(oneFile);
                h = new DataOutputStream(f);
                for (String de : myLines) {
                    h.writeBytes(de);               
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }finally {
                try {
                    h.close();
                    f.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

        }

暫無
暫無

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

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