我有类似的问题。不幸的是,我无法编辑任何内容。
en_GB United States 2015-07-10 2015-08-30 mountains 5,400.20 USD
这是文本文件中的一行。我正在尝试为它准备扫描仪并基于它创建对象。在这里我做了什么,我可以编辑什么。
String lang;
String country;
Date[] dates; //contains two dates - arrival and departure
String place;
Double price;
String curr;
public TravelData(String lang, String countryIn, Date[] dateIn, String placeIn, Double priceIn, String currIn)
这就是我的构造函数的样子。我试图使用scanner和next()方法,但看起来它并没有使用日期。在较小的部分上剪切这一行文字的最佳方法是什么?
我不工作的扫描仪:)。
public void ReadOffers(File dataDir) throws FileNotFoundException{
TravelData travel;
for (final File fileEntry : dataDir.listFiles()) {
scan = new Scanner(fileEntry);
while(scan.hasNextLine()){
String l = scan.next();
String c = scan.next();
Date[] d = [Date.parse(scan.next()); Date.parse(scan.next())];
String pl = scan.next();
Double pr = Double.parseDouble(scan.next());
String cu = scan.next();
travel = new TravelData(l, c, d, pl, pr, cu);
travelList.add(travel);
}
}
}
答案 0 :(得分:3)
您可以使用BufferedReader来读取该行,然后使用split()并将其放入一个字符串数组中,如下所示:
new UnityContainer().Resolve<A>()
答案 1 :(得分:2)
将其加载为整个字符串
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(fileEntry)));
String line;
while(line = br.readNext() != null)
{
String[] words = line.split(" ");//assuming you want to split based on the whitespace char
//do what you want to do with the words
}//reapeat as long as there is a line to the file
使用split(正则表达式)
通过正则表达式分隔String fileContent = new String(readAllBytes(get("test.txt"));
编辑:我读到那些是标签:split(“\ t *”)。 EDIT2:关于日期,请检查should support Parquet