2016-08-10 3 views
1

私はJavaFXを教えてきました。私は単純なファイル転送計算機を開発しています。ファイルサイズ、転送速度、転送時間を計算します。Java - プログラムがファイルからオプションをロードする最も良い方法は何ですか?

現在のオプションをファイル(たとえばメガバイト、ギガバイトなど)からいくつかのJavaFX ChoiceBoxに読み込みたいと思っています。私が必要とする:

  • ファイルは
  • 転送が
  • 時間単位

を高速化し、ユーザーはそれが秒単位でどのくらいの時間を知りたいかもしれないので、私は、彼らが変換方法についての情報を持っている必要がありますがサイズ1 TBのデータを42 KB /秒の接続で転送することになります。

私はテキストファイルを使うと思っていましたが、ファイルで読みやすいほど書式づけが難しく、書込みプロセスを自動化するのは難しいでしょう。私はそうするために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を宣言します。それが私が自動化したいものであり、最終的な変換方法です。これにより、プログラムはより柔軟で簡単に更新可能になります。

+0

iniファイル? – sMaN

+0

http://stackoverflow.com/questions/190629/what-is-the-easiest-way-to-parse-an-ini-file-in-java – sMaN

+0

@sMaNあなたはそれがどのように機能するかについてより具体的に説明できますか? – Elegea

答えて

3

Javaでこれを行うための標準的な方法は、標準的な形式があり、クラスjava.util.Properties

にアップ結婚.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"); 
+0

私がプロパティファイルを読んだところでは、1つのキーと1つの値しか使用できませんでした。例:MB - 1024 KB さらに多くのMBペアを必要とする場合はどうすればよいですか? Like MB - 1024 KB - 1/1024 GB? – Elegea

+0

ここでhttps://www.mkyong.com/java/java-properties-file-examples/には複数のプロパティの例がたくさんあります – sMaN

+0

多くのキーですが、各キーは1回だけです。各キーはユニークです。あるキーに多くの値が必要な場合は、 'key = v1、v2、v3'のようにコンマ区切りのリストで指定し、別々に値を解析します。 – Stewart

0

javaにデータを格納するために2つの異なるライブラリが推奨されています。最初のjson-simpleはシンプルで使いやすく、少しでもセットアップが必要な場合は、それを見つけることができますhereチュートリアルもたくさんあります。別のオプションはyamlを使うことです。ライブラリヘビyamlはこれに本当に良いですし、セットアップも簡単ですし、オンラインで多くのチュートリアルがあります。あなたはそれを見つけることができますhere

関連する問題