-3

我想读取一个包含空格分隔值的文本文件。值是整数。我该如何阅读它?我想阅读每一行,然后转到下一个。

内容如示例:

“2012 年 12 月 11 日” “00.00.01” 0,100

“2012 年 12 月 11 日” “00.00.05” 0,140

“2012 年 12 月 11 日” “00.00.09” 0,240

“2012 年 12 月 11 日” “00.00.13” 0,247

第一列是日期,第二列是第二列,第三列是升。

我怎样才能用 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

这看起来像一个 CSV 文件。您可以使用CSV 工具来阅读它。

我知道 CSV 表示“逗号分隔值”,您的文件没有逗号,但 CSV 工具也可以读取以空格分隔的文件。

于 2013-03-22T09:35:24.920 回答
0

这也可以通过简单的谷歌搜索来完成。首先你会找到这样的代码:

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 回答
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 回答