简体   繁体   English

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

[英]Reading data from text file in Java

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 上面的文件在每个行上包含日期,时间,整数和双精度

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); 编辑 :我目前的尝试涉及将每一行作为一个字符串读取,并为各种数据创建子字符串,例如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. 这现在可以正常工作,但是例如当我到达整数时,它不一定是4位数字。 This leaves me stuck as i don't know how to read a number of arbitrary size. 这使我陷入困境,因为我不知道如何读取任意大小的数字。

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]);  

}  

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

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. 它具有构造函数,该构造函数采用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

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])
   );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM