1

InputFileStream および Scanner クラスを使用して、テキスト ファイルを正常に読み取ることができます。とても簡単ですが、それよりも複雑なことをする必要があります。最初に私のプロジェクトについて少し背景を説明します.センサーを備えたデバイスがあり、センサーからのデータを10秒ごとにテキストファイルに記録するロガーを使用しています。10 秒ごとに新しいデータ行になります。したがって、ファイルを読み取るときに、個別のセンサーデータを配列に取得する必要があります。例: 速度 高度 緯度 経度

22 250 46.123245 122.539283

25 252 46.123422 122.534223

したがって、高度データ (250、252) を配列 alt[]; に取得する必要があります。など vel[]、lat[]、long[]...

次に、テキスト ファイルの最後の行は、1 行だけで異なる情報になります。日付、移動距離、経過時間..

少し調査した後、InputStream、Reader、StreamTokenizer、および Scanner クラスに出会いました。私の質問は、私のケースにどれをお勧めしますか? 私の場合、私がする必要があることをすることは可能ですか?ファイルの最後の行が何であるかを確認して、日付、距離などを取得できるようになります..ありがとうございます!

4

3 に答える 3

1

リーダー + String.split()

String line;
String[] values;
BufferedReader reader = new BufferedReader(new FileReader(args[0]));
List<Integer> velocity = new ArrayList<Integer>();
List<Integer> altitude = new ArrayList<Integer>();
List<Float> latitude = new ArrayList<Float>();
List<Float> longitude = new ArrayList<Float>();

while (null != (line = reader.readLine())) {
    values = line.split(" ");
    if (4 == values.length) {
        velocity.add(Integer.parseInt(values[0]));
        altitude.add(Integer.parseInt(values[1]));
        latitude.add(Float.parseFloat(values[2]));
        longitude.add(Float.parseFloat(values[3]));
    } else {
        break;
    }
}

リストされていない配列が必要な場合:

velocity.toArray();

私が理解している限り、データ行には4つのアイテムがあり、最後の行には3つのアイテム(日付、距離、経過時間)があります

于 2011-01-17T22:13:08.940 に答える
0

スキャナーを使用します。こちらの例をご覧ください。BufferedReader を使用して行を読み取り、 parse メソッドを使用してその行を必要なトークンに解析する別のオプション。

また、このスレッドが役立つ場合があります。

上記のリンクの非常に迅速なコードベース。入力配列には、ファイル データ トークンがあります。

public static void main(String[] args) {
    BufferedReader in=null;
    List<Integer> velocityList = new ArrayList<Integer>(); 
    List<Integer> altitudeList = new ArrayList<Integer>();
    List<Double> latitudeList = new ArrayList<Double>();
    List<Double> longitudeList = new ArrayList<Double>(); 
    try {
        File file = new File("D:\\test.txt");
        FileReader reader = new FileReader(file);
        in = new BufferedReader(reader);
        String string;
        String [] inputs;
        while ((string = in.readLine()) != null) {
            inputs = string.split("\\s");
            //here is where we copy the data from the file to the data stucture
            if(inputs!=null && inputs.length==4){
                velocityList.add(Integer.parseInt(inputs[0]));
                altitudeList.add(Integer.parseInt(inputs[1]));
                latitudeList.add(Double.parseDouble(inputs[2]));
                longitudeList.add(Double.parseDouble(inputs[3]));
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally{
        try {
            if(in!=null){
                in.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //here are the arrays you want!!!
    Integer [] velocities = (Integer[]) velocityList.toArray();
    Integer [] altitiudes = (Integer[]) altitudeList.toArray();
    Double [] longitudes = (Double[]) longitudeList.toArray();
    Double [] latitudes = (Double[]) latitudeList.toArray();
}
于 2011-01-17T21:11:15.377 に答える
0

あなたのデータは比較的単純なのでBufferedReaderStringTokenizerうまくいくはずです。残りの行がなくなったことを検出するには、1 行ずつ先読みする必要があります。

あなたのコードは次のようになります

      BufferedReader reader = new BufferedReader( new FileReader( "your text file" ) );

      String line = null;
      String previousLine = null;

      while ( ( line = reader.readLine() ) != null ) {
          if ( previousLine != null ) {
             //tokenize and store elements of previousLine
          }
          previousLine = line;
      }
      // last line read will be in previousLine at this point so you can process it separately

しかし、ライン自体をどのように処理するかはScannerあなた次第です。

于 2011-01-17T21:11:34.353 に答える