简体   繁体   中英

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?

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.

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.

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

Could be done by using 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])
   );
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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