【发布时间】:2023-03-07 01:32:01
【问题描述】:
我有一个文本文件,我想用 java.util.Scanner 读取并解析内容。
文件如下所示:
对象数:5
描述 0 一些描述
:描述 1 一些描述
: 对象 1 的更多描述
: 对象 1 的更多描述
: 对象 1 的更多描述
描述 2 对象 2 的一些描述
: 对象 2 的更多描述
描述 3 对象 3 的一些描述
: 对象 3 的更多描述
描述 4 对象 4 的一些描述
: 对象 4 的更多描述
到目前为止我已经有了这个
String pattern = "description";
String pattern2 = ":";
int romNummer = 0;
fil = new Scanner(new File(filnavn));
do{
String input;
input = fil.next();
romNummer = fil.nextInt();
String description = fil.nextLine();
if(fil.hasNext(":")){
input = fil.next();
String description2 = fil.nextLine();
description += description2;
}
if(fil.hasNext(":")){
input = fil.next();
String description2 = fil.nextLine();
description2 += description2;
}
if(fil.hasNext(":")){
input = fil.next();
String description2 = fil.nextLine();
description += description2;
}
}while (fil.hasNext(pattern)||fil.hasNext(pattern2));
我想要一个对象的所有描述作为一个字符串。使用扫描仪类是否有更优雅的方法?
【问题讨论】: