-3

スペースで区切られた値を含むテキスト ファイルを読みたいです。値は整数です。どうすれば読めますか?私は各行を読んでから次へ行きたいです。

内容は例のとおりです。

"12/11/2012" "00.00.01" 0,100

"2012 年 12 月 11 日" "00.00.05" 0,140

"2012 年 12 月 11 日" "00.00.09" 0,240

"12/11/2012" "00.00.13" 0,247

最初の列は日付、2 番目は 2 番目、3 番目はリットルです。

Javaプログラムでどうすればできますか?

Scannerクラスを使用することを考えています。私はこのプログラムを作りました:

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class ScannerExample {

    public static void main(String args[]) throws FileNotFoundException {

        File text = new File("C:\Users\Desktop\test\test.txt");

        Scanner scnr = new Scanner(text);

        int lineNumber = 1;
        while(scnr.hasNextLine()){
            String line = scnr.nextLine();
            System.out.println("line " + lineNumber + " :" + line);
            lineNumber++;
        }       

    }   

}

しかし、私は望んでいた結果を持っていません。何か助けはありますか?

4

3 に答える 3

0

Java で最も簡単なのは、Apache Commons Libraries を使用することです。pom ファイルに次の依存関係を追加するだけです (maven を使用している場合)。

<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-io</artifactId>
<version>1.3.2</version>
</dependency>

それからあなたはすることができます

File file = new File("yourFile.txt");
List<String> lines = FileUtils.readLines(file);

リスト内のファイルの各行を取得します。次に、ファイルのコンテンツを取得するには:

for(String line : lines){
    String[] columns = line.split(" ") //because your columns seem to be separated by a white space
}

配列列にはデータが含まれます

DateFormat format = new SimpleDateFormat("dd/mm/yyyy");
//column 1
Date date = format.format(column[0]);
//column 2
//I'm not sure to get your second column, is it just second or can it be more?
//column 3
double litres = Double.parseDouble(column[2]);
于 2013-03-21T09:45:32.400 に答える
0

これは CSV ファイルのように見えます。CSV ツールを使用して読み取ることができます。

CSV は「カンマ区切り値」を意味し、ファイルにはカンマがありませんが、CSV ツールはスペースで区切られたファイルを読み取ることもできます。

于 2013-03-22T09:35:24.920 に答える
0

これは、単純な Google 検索でも実行できます。最初に、次のようなコードを見つけます。

import java.io.*;

class FileRead 
{
 public static void main(String args[])
  {
  try{
  // Open the file that is the first 
  // command line parameter
  FileInputStream fstream = new FileInputStream("textfile.txt");
  // Use DataInputStream to read binary NOT text.
  BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
  String strLine;
  //Read File Line By Line
  while ((strLine = br.readLine()) != null)   {
  // Print the content on the console
  System.out.println (strLine);
  }
  //Close the input stream
  in.close();
    }catch (Exception e){//Catch exception if any
  System.err.println("Error: " + e.getMessage());
  }
  }
}

System.out.println を、" から " までのすべてのコンテンツを取得するコードに置き換えます。

while ((strLine = br.readLine()) != null)   {

        int start = 1, pos = 0,lastpos= 1;
        String dates, times, values;

        if(strLine.indexOf("\"",lastpos) > 0){
            pos = strLine.indexOf("\"",lastpos);
            dates = strLine.substring(start,pos);
            lastpos = pos+3;//replace through search for \"

            pos = strLine.indexOf("\"",lastpos);
            times = strLine.substring(lastpos,pos);
            lastpos = pos+2;//replace through search for \"

            values = strLine.substring(lastpos,strLine.length());


            System.out.println(dates);//Convert to Date
            System.out.println(times);//Convert to time
            System.out.println(values);//Convert to Integer.valueOf/)

            System.out.println("next line");
        }

    }
于 2013-03-21T09:35:53.890 に答える