2011-01-17 6 views
1

InputFileStreamクラスとScannerクラスを使用してテキストファイルを正常に読み取ることができます。それは非常に簡単ですが、私はそれより複雑な何かをする必要があります。私のプロジェクトについての少しの背景です。私はセンサー付きのデバイスを持っています。センサーからテキストファイルに10秒ごとのデータを記録するロガーを使用しています。 10秒ごとに新しいデータ行が表示されます。だから私が望むのは、ファイルを読み込むと、それぞれのセンサーデータを配列に集めることです。例えば: 速度高度緯度経度javaを使用したテキストファイルの解析、使用するための推奨事項

22 250 46.123245 122.539283

25 252 46.123422 122.534223

だから[]配列ALTに(250、252)標高データを取得する必要があります。 vel []、lat []、long [] ...

次に、テキストファイルの最後の行は異なる情報、単一行になります。

少しの研究をした後、私はInputStream、Reader、StreamTokenizer、Scannerクラスを見つけました。私の質問は、あなたが私の場合にお勧めするものですか?私の場合に必要なことをすることは可能ですか?ファイルの最後の行が何であるかを確認できるので、日付、距離などを取得できます。ありがとう!

答えて

0

私はスキャナを使用します。例hereを見てください。 BufferedReaderを使用して行を読み込み、その行を解析して必要なトークンに変換する別のオプションもあります。

また、threadが役立つことがあります。

上記のリンクには非常に簡単なコードベースがあります。 inputs配列にはファイルデータトークンがあります。

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(); 
} 
+0

ありがとうございます。しかし、私は必要なものにどのように適用しますか?提供されているリンクの最後の例では、次のデータ型(int、string)を探します。もし文字列、int、int、int、float、floatを持っていればどうなりますか? – t0x13

+0

Scannerクラスは、異なるデータ型。あなたのケースでは、データは常に数値ですか? – CoolBeans

+0

データはintまたはfloatのいずれかになります。私はすべてのデータを含んでいませんが、時間と時間が両方とも 'String'になります。 – t0x13

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を使用することができ、本当にあなた次第です。

+0

'StringTokenizer'の代わりに' String [] fields = previousLine.split( "") ' – Damien

+0

のようにString.split()を使うことができます。StringTokenizer ..すべての要素をStringに変換しますか?私は整数の配列が必要ですし、浮動.. – t0x13

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つのアイテムを持っているのundestandし、最後の行は3つの項目があります(日付、距離、経過時間)

+0

実際にarraylistsは私が必要なものに適用されます。私はこれを試していただきありがとうございます、まさに私が必要なもののように見えます.. – t0x13

関連する問題