JavaFX:从文本文件中读取数据并将其粘贴到正确的位置

JavaFX: Read data from text file and paste it to the right place

我在学校有 javafx 作业

enter image description here

我能够按要求保存数据

enter image description here

我的问题是如何从文本文件中读取数据,以便所有数据都回到正确的位置

enter image description here

Integer cb_years_v;
            String cb_manuf_v,smb_type_v,tg_v,cb_extras_v,lv_size_v,tf_vend_user_v,tf_vend_name_v,pf_pass_v,ta_other_v,price_value_v;
            
            cb_manuf_v = cb_manuf.getValue(); //ComboBox
            smb_type_v=ch_type.getText(); //SplitMenuButton
            tg_v = tg.getSelectedToggle().toString(); //ToggleGroup
            cb_extras_v = cb_extras.getValue(); //ComboBox
            cb_years_v = cb_years.getValue(); //ComboBox
            lv_size_v = lv_size.getSelectionModel().getSelectedItem().toString(); //ListView
            tf_vend_name_v = tf_vend_name.getText(); //TextField
            tf_vend_user_v = tf_vend_user.getText(); //TextField
            pf_pass_v = pf_pass.getText(); //PasswordField
            ta_other_v = ta_other.getText(); //TextArea
            price_value_v = price_value.getText(); //Label
            
            FileChooser chooser = new FileChooser();
            chooser.setTitle("Choose location To Save Report");;
            File selectedFile = null;
            while(selectedFile== null){
                selectedFile = chooser.showSaveDialog(null);
            }
            File file2 = null;
            file2 = selectedFile;
            PrintWriter outFile = null;
            try {
                outFile = new PrintWriter(file2);
            } catch (FileNotFoundException e2) {
                e2.printStackTrace();
            }
            outFile.println("Manufacturer = " + cb_manuf_v);
            outFile.println("Type = "+smb_type_v);  
            outFile.println("Color = " +tg_v);    
            outFile.println("Extras = " + cb_extras_v);
            outFile.println("Year = " + cb_years_v);
            outFile.println("Size = " + lv_size_v);
            outFile.println("Vendor Name = " + tf_vend_name_v);
            outFile.println("Vendor User = " + tf_vend_user_v);
            outFile.println("Password = " + pf_pass_v);
            outFile.println("Other = " + ta_other_v);
            outFile.println("Price = " + price_value_v);
            
            outFile.close();

感谢您的帮助!

您可以使用 Java 扫描器 class 读取文件

 Scanner sc = new Scanner(new File(yourfilepath));
    while(sc.hasNextLine){
String line = sc.nextLine() //scanner can read line by line, you can concat all linesin a string to get text in the file
     }

这里有更多读取 txt 文件的选项:

Reading a plain text file in Java

您可以使用 Sir Lopez answer 获取每一行的字符串,对于每个字符串,您必须:

  • 检测涉及到哪个 javaFX 对象,最好是在有等号的地方切掉字符串,例如从“Color = blue”这一行你会得到两个字符串“Color”和“blue”。作业我就不给你做了,你可以看看split的方法
  • 用第二个值填充 javaFX 对象:tf_vend_name.setText(ValueFromFile);

我的解决方案,希望对其他人有所帮助。

感谢您的帮助!

 load.addEventHandler(ActionEvent.ACTION, (e) -> {

        FileChooser fileChooser = new FileChooser();
        fileChooser.setTitle("Open Resource File");
        fileChooser.getExtensionFilters().addAll(new ExtensionFilter("Text Files", "*.txt"));
        File selectedFile = fileChooser.showOpenDialog(primaryStage);
        Scanner scan = null;

        try {
            scan = new Scanner(selectedFile);

            scan.next();
            String line = scan.nextLine();
            String[] lineArray = line.split(" = ");
            cb_manuf.setValue(lineArray[1]);

            scan.next();
            String line2 = scan.nextLine();
            String[] lineArray2 = line2.split(" = ");
            smb_type.setText(lineArray2[1]);

            scan.next();
            String line3 = scan.nextLine();
            String[] lineArray3 = line3.split(" = ");
            // tg.selectedToggleProperty(lineArray3[1]);

            scan.next();
            String line4 = scan.nextLine();
            String[] lineArray4 = line4.split(" = ");
            cb_extras.setValue(lineArray4[1]);

            scan.next();
            String line5 = scan.nextLine();
            String[] lineArray5 = line5.split(" = ");
            cb_years.setValue(Integer.parseInt(lineArray5[1]));

            scan.next();
            String line6 = scan.nextLine();
            String[] lineArray6 = line6.split(" = ");
            // lineArray6[1] = lv_size.getSelectionModel().getSelectedItem();

            scan.next();
            String line7 = scan.nextLine();
            String[] lineArray7 = line7.split(" = ");
            tf_vend_name.setText(lineArray7[1]);

            scan.next();
            String line8 = scan.nextLine();
            String[] lineArray8 = line8.split(" = ");
            tf_vend_user.setText(lineArray8[1]);

            scan.next();
            String line9 = scan.nextLine();
            String[] lineArray9 = line9.split(" = ");
            pf_pass.setText(lineArray9[1]);

            scan.next();
            String line10 = scan.nextLine();
            String[] lineArray10 = line10.split(" = ");
            ta_other.setText(lineArray10[1]);

            scan.next();
            String line11 = scan.nextLine();
            String[] lineArray11 = line11.split(" = ");
            price_value.setText(lineArray11[1]);

        } catch (FileNotFoundException e1) {
            e1.printStackTrace();
        } finally {
            scan.close();
        }
    }

    );

这是您可以使用的另一种方法。您可以使用 Files.readAllLines 而不是 Scanner。从那里,您可以使用行索引作为 Switch-Statement 中的控件,或者如果行包含您需要使用 If-Statement 提取的信息。此示例不显示 Switch-Statement 版本。它只显示 If-Statement 版本。我没有测试此代码,因此可能存在错误。

try {
    File selectedFile = new File("");
    List < String > lines = Files.readAllLines(selectedFile.toPath());
    for (String line: lines) {
        if (line.contains("Manufacturer")) {
            cb_manuf.setValue(line.split("=")[1].strip());
        } else if (line.contains("Type")) {
            smb_type.setText(line.split("=")[1].strip());
        } else if (line.contains("Color")) {
            ...
        } else if (line.contains("Extras")) {
            ...
        } else if (line.contains("Year")) {
            ...
        } else if (line.contains("Size")) {
            ...
        } else if (line.contains("Vendor Name")) {
            ...
        } else if (line.contains("Vendor User")) {
            ...
        } else if (line.contains("Password")) {
            ...
        } else if (line.contains("Other")) {

        } else if (line.contains("Price")) {
            ...
        }
    }
} catch (IOException ex) {
    ex.printStackTrace();
}