Read multiline text with values separated by white

2019-02-10 14:45发布

I have a following test file :

Jon Smith 1980-01-01
Matt Walker 1990-05-12

What is the best way to parse through each line of this file, creating object with (name, surname, birthdate) ? Of course this is just a sample, the real file has many records.

5条回答
神经病院院长
2楼-- · 2019-02-10 15:27
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class ScannerReadFile {
    public static void main(String[] args) {
        //
        // Create an instance of File for data.txt file.
        //
        File file = new File("tsetfile.txt");

        try {
            //
            // Create a new Scanner object which will read the data from the 
            // file passed in. To check if there are more line to read from it
            // we check by calling the scanner.hasNextLine() method. We then
            // read line one by one till all line is read.
            //
            Scanner scanner = new Scanner(file);
            while (scanner.hasNextLine()) {
                String line = scanner.nextLine();
                System.out.println(line);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

    }
}

This:

            while (scanner.hasNextLine()) {
                String line = scanner.nextLine();

Could also be changed to

        while (scanner.hasNext()) {
            String line = scanner.next();

Which will read whitespace.

You could do

Scanner scanner = new Scanner(file).useDelimiter(",");

To do a custom delimiter

At the time of the post, now you have three different ways to do this. Here you just need to parse the data you need. You could read the the line, then split or read one by one and everything 3 would a new line or a new person.

查看更多
淡お忘
3楼-- · 2019-02-10 15:36
 import java.io.*;
 class Record
{
   String first;
   String last;
   String date;

  public Record(String first, String last, String date){
       this.first = first;
       this.last = last;
       this.date = date;
  }

  public static void main(String args[]){
   try{
    FileInputStream fstream = new FileInputStream("textfile.txt");
          DataInputStream in = new DataInputStream(fstream);
          BufferedReader br = new BufferedReader(new InputStreamReader(in));
          String strLine;
          while ((strLine = br.readLine()) != null)   {
   String[] tokens = str.split(" ");
   Record record = new Record(tokens[0],tokens[1],tokens[2]);//process record , etc

   }
   in.close();
   }catch (Exception e){
     System.err.println("Error: " + e.getMessage());
   }
 }
}
查看更多
孤傲高冷的网名
4楼-- · 2019-02-10 15:38

At first glance, I would suggest the StringTokenizer would be your friend here, but having some experience doing this for real, in business applications, what you probably cannot guarantee is that the Surname is a single name (i.e. someone with a double barrelled surname, not hyphenated would cause you problems.

If you can guarantee the integrity of the data then, you code would be

BufferedReader read = new BufferedReader(new FileReader("yourfile.txt"));
String line = null;
while( (line = read.readLine()) != null) {
   StringTokenizer tokens = new StringTokenizer(line);
   String firstname = tokens.nextToken();
   ...etc etc
}

If you cannot guarantee the integrity of your data, then you would need to find the first space, and choose all characters before that as the last name, find the last space and all characters after that as the DOB, and everything inbetween is the surname.

查看更多
闹够了就滚
5楼-- · 2019-02-10 15:39

Look at BufferedReader class. It has readLine method. Then you may want to split each line with space separators to construct get each individual field.

查看更多
一纸荒年 Trace。
6楼-- · 2019-02-10 15:45

Use a FileReader for reading characters from a file, use a BufferedReader for buffering these characters so you can read them as lines. Then you have a choice.. Personally I'd use String.split() to split on the whitespace giving you a nice String Array, you could also tokenize this string.

Of course you'd have to think about what would happen if someone has a middle name and such.

查看更多
登录 后发表回答