读取多行文本,其值之间用空格隔开 [英] Read multiline text with values separated by whitespaces
本文介绍了读取多行文本,其值之间用空格隔开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下测试文件:
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.
推荐答案
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());
}
}
}
这篇关于读取多行文本,其值之间用空格隔开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文