2010-10-24 67 views
3

我有下面的测试文件:与价值观读多行文本由空格分隔

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

什么是通过这个文件的每一行解析的最佳方式,用(姓名,出生日期)创建对象?当然这只是一个示例,真正的文件有很多记录。

+0

你教过什么?通常在学校里,至少对我来说这是扫描仪课。这将有助于确定什么是最适合你的。 – Matt 2010-10-24 15:47:35

+0

我打算使用扫描仪,但如果使用StringTokenizer或StreamTokenizer更感兴趣。 – mastodon 2010-10-24 16:02:08

+1

你也可以使用标记器。基本上扫描该行,然后使用带有空格分隔符的标记器。 StringTokenizer st = new StringTokenizer(“this is a test”); (st.hasMoreTokens()){System.out.println(st.nextToken()); } – Matt 2010-10-24 16:44:53

回答

8
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()); 
    } 
} 
} 
+0

我会在一分钟内更新此代码以包含对象的创建。 – Orbit 2010-10-24 15:44:49

+2

@布兰登 - 这个问题显然有一个'家庭作业'的标签...我不认为你通过提供完整的解决方案使@乳香的生活变得更好。 – 2010-10-24 15:48:28

+0

eep,道歉放弃答案。 – Orbit 2010-10-24 15:53:27

0

请看BufferedReader班。它有readLine方法。然后你可能想要用空格分隔符来分隔每一行来构造获取每个单独的字段。

1

乍一看,我建议的StringTokenizer将是你的朋友在这里,但有一些经验,这样做真的,在商业应用中,你可能无法保证的是姓是一个单一的名称(即有人用双筒姓,不带连字符会导致你的问题。

如果你能保证数据的完整性,那么,你的代码会

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 
} 

如果你不能保证你的数据的完整性,那么你会需要找到第一个空格,然后选择之前的所有字符作为姓,然后找到最后一个空格和之后的所有字符作为DOB,并且其中的所有内容都是姓氏。

1

使用FileReader来读取文件中的字符,使用BufferedReader来缓冲这些字符,以便您可以将它们读取为行。然后你有一个选择..就个人而言,我会使用String.split()拆分空白给你一个很好的字符串数组,你也可以标记这个字符串。

当然,你必须考虑如果有人有中间名等会发生什么。

8
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(); 
     } 

    } 
} 

此:

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

也可以更改为

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

将要读空白。

你可以做

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

要做到自定义分隔符

在后的时间,现在你有三种不同的方法来做到这一点。在这里你只需要解析你需要的数据。你可以阅读这一行,然后逐一分开或阅读,所有的3将会有新的一行或一个新的人。