从Java中读取文本文件中的数据

时间:2021-08-13 09:46:58

i have a text file with contents:

我有一个内容的文本文件:

26/09/2013,16:04:40 2412 -928.0
25/09/2013,14:24:30 2412 914.0

The file above contains a date, time an integer and a double on each line

上面的文件包含日期,时间为整数,每行包含一个double

I have made a class to contain the data once it is read in:

读完后我创建了一个包含数据的类:

public class Entry{

    public String date, time;
    public int integerNumber;
    public double doubleNumber;

    public Entry(String d, String t, int n1, double n2){
        this.date=d;
        this.time=t;
        this.integerNumber=n1;
        this.doubleNumber=n2;
    }
}

What is the best way to read the above file into an Entry[] array where each element in the array is the data from each line?

将上述文件读入Entry []数组的最佳方法是什么,其中数组中的每个元素都是来自每一行的数据?

EDIT: My current attempt involves reading each line as a String and creating substrings for the various pieces of data eg String date=line.substring(0,10); This works fine for now but when i get to the integer for example, it wont necessarily be a 4 digit number. This leaves me stuck as i don't know how to read a number of arbitrary size.

编辑:我当前的尝试涉及将每一行作为字符串读取并为各种数据创建子字符串,例如String date = line.substring(0,10);这个现在工作正常,但是当我得到整数时,它不一定是4位数。这让我陷入困境,因为我不知道如何阅读任意大小的数字。

4 个解决方案

#1


2  

Well, if you can guarantee that all your lines in the file you're reading have the following format

好吧,如果你能保证你正在阅读的文件中的所有行都具有以下格式

<date>,<time> <integer> <double>

You could read it like this

你可以这样读

String foo = "25/09/2013,14:24:30 2412 914.0";
String delims = "[, ]+";
String[] tokens = foo.split(delims);

String d = tokens[0]; // d would contain the string '25/09/2013'
String t = tokens[1]; // t would contain the string '14:24:30'
int n1 = Integer.parseInt(tokens[2]); // n1 would contain the integer 2412
double n2 = Double.parseDouble(tokens[3]); // n2 would contain the double 914.0

#2


3  

You can use a regular expression to read the text file.

您可以使用正则表达式来读取文本文件。

For example,

String line = "001 John Smith";  
String regex = "(\\d)+ (\\w)+ (\\w)+";  
Pattern pattern = Pattern.compile(regex);  
Matcher matcher = pattern.matcher(line);  
while(matcher.find()){  

    String [] group = matcher.group().split("\\s");  
    System.out.println("First Name "+group[1]);  
    System.out.println("Last Name " +group[2]);  

}  

#3


0  

You can use Scanner for this.
It has constructor which takes File and also has next(),nextInt(),nextDouble() methods to read String,int,double respectively.

您可以使用Scanner进行此操作。它有构造函数,它接受File并且还有next(),nextInt(),nextDouble()方法分别读取String,int,double。

For sample code reference link : http://www.cs.utexas.edu/users/ndale/Scanner.html

有关示例代码参考链接:http://www.cs.utexas.edu/users/ndale/Scanner.html

#4


0  

Could be done by using Use Apache Commons FileUtils

可以使用Use Apache Commons FileUtils来完成

File file = new File("/yourfile.txt");
List<String> lines = FileUtils.readLines(file, null);
List<Entry> entries = new ArrayList<Entry>();
for (String line : lines) {
  //line == 26/09/2013,16:04:40 2412 -928.0
  Entry entry = createEntry(line); // parse string and create a Entry...

  entries.add(entry);
}

// Warning, not safe and not pretty...
private static Entry createEntry(String line) {
   String[] parts = line.split(" ");        
   String[] datePart = parts[0].split(",");

   return new Entry(datePart[0], 
             datePart[1], 
             Integer.valueOf(parts[1]),
             Double.valueOf(parts[2])
   );
}

#1


2  

Well, if you can guarantee that all your lines in the file you're reading have the following format

好吧,如果你能保证你正在阅读的文件中的所有行都具有以下格式

<date>,<time> <integer> <double>

You could read it like this

你可以这样读

String foo = "25/09/2013,14:24:30 2412 914.0";
String delims = "[, ]+";
String[] tokens = foo.split(delims);

String d = tokens[0]; // d would contain the string '25/09/2013'
String t = tokens[1]; // t would contain the string '14:24:30'
int n1 = Integer.parseInt(tokens[2]); // n1 would contain the integer 2412
double n2 = Double.parseDouble(tokens[3]); // n2 would contain the double 914.0

#2


3  

You can use a regular expression to read the text file.

您可以使用正则表达式来读取文本文件。

For example,

String line = "001 John Smith";  
String regex = "(\\d)+ (\\w)+ (\\w)+";  
Pattern pattern = Pattern.compile(regex);  
Matcher matcher = pattern.matcher(line);  
while(matcher.find()){  

    String [] group = matcher.group().split("\\s");  
    System.out.println("First Name "+group[1]);  
    System.out.println("Last Name " +group[2]);  

}  

#3


0  

You can use Scanner for this.
It has constructor which takes File and also has next(),nextInt(),nextDouble() methods to read String,int,double respectively.

您可以使用Scanner进行此操作。它有构造函数,它接受File并且还有next(),nextInt(),nextDouble()方法分别读取String,int,double。

For sample code reference link : http://www.cs.utexas.edu/users/ndale/Scanner.html

有关示例代码参考链接:http://www.cs.utexas.edu/users/ndale/Scanner.html

#4


0  

Could be done by using Use Apache Commons FileUtils

可以使用Use Apache Commons FileUtils来完成

File file = new File("/yourfile.txt");
List<String> lines = FileUtils.readLines(file, null);
List<Entry> entries = new ArrayList<Entry>();
for (String line : lines) {
  //line == 26/09/2013,16:04:40 2412 -928.0
  Entry entry = createEntry(line); // parse string and create a Entry...

  entries.add(entry);
}

// Warning, not safe and not pretty...
private static Entry createEntry(String line) {
   String[] parts = line.split(" ");        
   String[] datePart = parts[0].split(",");

   return new Entry(datePart[0], 
             datePart[1], 
             Integer.valueOf(parts[1]),
             Double.valueOf(parts[2])
   );
}