我遇到了类似的问题。不幸的是,我无法编辑任何内容。
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)
这就是我的构造函数的样子。我试图使用扫描仪和 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);
}
}
}
最佳答案
您可以使用 BufferedReader 来读取该行,然后 split() 并将其放入字符串数组中,如下所示:
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
关于java - 如何拆分包含两个日期的文本行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37641047/