Java - 从文件加载程序加载选项的最佳方法是什么?

时间:2016-08-10 00:11:05

标签: java xml javafx

我一直在教自己JavaFX,我正在开发一个简单的文件传输计算器。它可以计算文件大小,传输速度和传输时间。

我希望能够将当前选项从文件(例如,兆字节,千兆字节等)加载到几个JavaFX ChoiceBox。我需要:

  • 文件大小
  • 转移速度
  • 时间单位

我需要了解它们如何转换,因为用户可能想知道通过42 KB / s连接传输1 TB数据需要多少时间。

我想过使用一个文本文件,但是以一种容易被文件读取的方式格式化会很麻烦,并且很难自动化写入过程。我想过使用XML来做这件事,但我不知道如何将它用于此目的,或者它是否是一个好主意。 那么,加载选项和每个选项的最佳方法是什么?以及如何使用它?

package application;

import java.io.ObjectInputStream.GetField;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.RadioButton;
import javafx.scene.control.TextField;
import javafx.scene.control.Alert.AlertType;

public class MainInterfaceController implements Initializable {

    /*
     * Declarations of the interface interactive bits FS - File Size TS -
     * Transfer Speed TT - Transfer Time
     */
    @FXML
    private TextField FSTextField;

    @FXML
    private ChoiceBox<String> FSChoiceBox;

    @FXML
    private RadioButton FSRadioButton;

    @FXML
    private TextField TSTextField;

    @FXML
    private ChoiceBox<String> TSChoiceBox;

    @FXML
    private RadioButton TSRadioButton;

    @FXML
    private TextField TTTextField;

    @FXML
    private ChoiceBox<String> TTChoiceBox;

    @FXML
    private RadioButton TTRadioButton;

    @FXML
    private Button AboutButton;

    @FXML
    private Button CalculateButton;

    // Variables & Data Sets

    //These should be initialized in the init method so they can be populated with info from a file(?)
    private ObservableList<String> fileSizes = FXCollections.observableArrayList("KB", "MB", "GB", "TB", "PB");
    private ObservableList<String> transferSpeeds = FXCollections.observableArrayList("KB/s", "MB/s", "GB/s", "TB/s", "PB/s");
    private ObservableList<String> timeUnits = FXCollections.observableArrayList("Seconds", "Minutes", "Hours");


    @Override
    public void initialize(URL location, ResourceBundle resources) {
        populateChoiceBoxes();
        setRadioButtonsActions();
        CalculateButton.setOnAction(e -> calculateValues());
    }

    private void populateChoiceBoxes() {
        FSChoiceBox.setItems(fileSizes);
        TSChoiceBox.setItems(transferSpeeds);
        TTChoiceBox.setItems(timeUnits);
    }

    private void setRadioButtonsActions() {
        FSRadioButton.setOnAction(e -> clearRadioButtons(e));
        TSRadioButton.setOnAction(e -> clearRadioButtons(e));
        TTRadioButton.setOnAction(e -> clearRadioButtons(e));
    }

    private void clearRadioButtons(ActionEvent e) {
        if (e.getSource() == FSRadioButton) {
            TSRadioButton.setSelected(false);
            TTRadioButton.setSelected(false);
        }

        if (e.getSource() == TSRadioButton) {
            FSRadioButton.setSelected(false);
            TTRadioButton.setSelected(false);
        }

        if (e.getSource() == TTRadioButton) {
            FSRadioButton.setSelected(false);
            TSRadioButton.setSelected(false);
        }
    }

    private void calculateValues() {
        if (FSRadioButton.isSelected()) {
            try {
                String TSText = TSTextField.getText();
                double transferSpeed = Double.parseDouble(TSText);
                String TTText = TTTextField.getText();
                double transferTime = Double.parseDouble(TTText);

            } catch (NumberFormatException e) {
                Alert alert = new Alert(AlertType.ERROR, "The transfer speed/transfer time must be a number!");
                alert.showAndWait();
            }

        }
    }

    private void convertSpeed(double transferSpeed, ChoiceBox<String> speedUnit) {
    }
}

到目前为止,我有上面的代码。正如您在开始时所看到的,我声明了ObservableLists。这就是我想要自动化的东西以及最终的转换方法。这将使程序更灵活,更容易更新。

2 个答案:

答案 0 :(得分:3)

在Java中执行此操作的标准方法是使用.properties文件,该文件与类java.util.Properties

结合使用

有一种标准格式:

  • key=value
  • <entry key="key">value</entry>

Properties类具有标准方法,用于加载属性并从Properties对象访问它们。

引用J2SE Javadoc示例可能是:

Properties properties = new Properties();
try(InputStream is = this.getClass().getResourceAsStream("my.properties")) {
    properties.load(is);
}
String value = properties.getProperty("key");

答案 1 :(得分:0)

我建议将两种不同的库用于在java中存储数据。第一个是json-simple它简单易用,只需要很少的设置就可以找到它here还有很多教程。另一个选择是使用yaml。库蛇yaml非常适合这个,并且很容易设置,并且有很多在线教程。你可以找到它here

相关问题