Java 解析并保存文本文件中的行
我正在尝试扫描一个格式如下的文本文件:Java 解析并保存文本文件中的行,java,java.util.scanner,Java,Java.util.scanner,我正在尝试扫描一个格式如下的文本文件: reviewers: 0 open: Sunday 08:00 16:00,Monday 06:00 20:00,Tuesday 06:00 20:00,Wednesday 06:00 20:00,Thursday 06:00 20:00,Friday 06:00 20:00,Saturday 06:00 01:00 name: The Lyre of Orpheus city: San Francisco cost: $$ category: Greek
reviewers: 0
open: Sunday 08:00 16:00,Monday 06:00 20:00,Tuesday 06:00 20:00,Wednesday 06:00 20:00,Thursday 06:00 20:00,Friday 06:00 20:00,Saturday 06:00 01:00
name: The Lyre of Orpheus
city: San Francisco
cost: $$
category: Greek,Breakfast & Brunch,Seafood,Salad,Soup
rank: 0
并将每一行保存为字符串或双精度,但我的forloop中的sys.out.println一直为null,有什么可以更改的吗?我的想法是,我在我的try中重置变量太早了
public class Yulp {
//instance vars
ArrayList<Restaurant> resList = new ArrayList<Restaurant>();
public static void main(String args[]) {
Yulp yelp = new Yulp();
yelp.scan();
for(int i = 0; i < yelp.resList.size(); i++){
System.out.println(yelp.resList.get(i).getCity());
}
}
public void scan() {
try {
Restaurant tempRes = new Restaurant();
String name, city, category, cost;
double rank, reviewers;
Scanner scan = new Scanner(new File("randomizedList.txt"));
while (scan.hasNext()) {
//name = null; city = null; category = null; cost = null; rank = 0.0; reviewers = 0;
String line = scan.nextLine();
String rest = omitPrefix(line, "reviewers:");
if (rest != null) {
reviewers = Double.parseDouble(rest);
tempRes.setReviewers(reviewers);
}
rest = omitPrefix(line, "rank:");
if (rest != null) {
rank = Double.parseDouble(rest);
}
rest = omitPrefix(line, "name:");
if (rest != null) {
name = rest;
}
rest = omitPrefix(line, "city:");
if (rest != null) {
city = rest;
}
rest = omitPrefix(line, "category:");
if (rest != null) {
category = rest;
}
rest = omitPrefix(line, "cost:");
if (rest != null) {
cost = rest;
}
resList.add(tempRes);
}
scan.close();
} catch (FileNotFoundException e) {
System.out.println("File not found");
}
}
private String omitPrefix(String line, String prefix) {
if (line.startsWith(prefix))
return line.substring(prefix.length());
return null;
}
公共类Yulp{
//实例变量
ArrayList resList=新建ArrayList();
公共静态void main(字符串参数[]){
Yulp yelp=新的Yulp();
yelp.scan();
对于(int i=0;i
}这是我的
import java.io.InputStream;
import java.util.Scanner;
public class Snippet {
public void scan() throws Exception {
Scanner scan = new Scanner(new File("randomizedList.txt"));
while (scan.hasNext()) {
String line = scan.nextLine();
String rest = omitPrefix(line, "reviewers:");
if (rest != null) {
System.out.println(rest);
}
rest = omitPrefix(line, "name:");
if (rest != null) {
System.out.println(rest);
}
rest = omitPrefix(line, "city:");
if (rest != null) {
System.out.println(rest);
}
rest = omitPrefix(line, "category:");
if (rest != null) {
System.out.println(rest);
}
}
scan.close();
}
private String omitPrefix(String line, String prefix) {
if (line.startsWith(prefix))
return line.substring(prefix.length());
return null;
}
public static void main(String[] args) throws Exception {
new Snippet().scan();
}
}
要点:
- 而不是
您只需执行line=newscanner(scan.nextLine());temp=line.nextLine()
扫描.nextLine
- 为了获取行的其余部分(例如“name:”part后面的部分),我引入了一个名为
的助手方法,如果ompiprefix(line,prefix)
以line
开头,或者prefix
开头,则返回null
line
- 还有另一种方法
public class FileParser {
public void scan() {
try {
File list = new File("randomizedList.txt");
Scanner scan = new Scanner(list);
String temp;
Scanner line;
while (scan.hasNext()) {
line = new Scanner(scan.nextLine());
temp = line.nextLine();
if (temp.startsWith("reviewers:")) {
System.out.println(temp.split(":",2)[1]);
}
if (temp.startsWith("name:",1)) {
System.out.println(temp.split(":",2)[1]);
}
if (temp.startsWith("open:")) {
System.out.println(temp.split(":",2)[1]);
}
if (temp.startsWith("city:")) {
System.out.println(temp.split(":",2)[1]);
}
if (temp.startsWith("category:")) {
System.out.println(temp.split(":",2)[1]);
}
}
scan.close();
}
catch(FileNotFoundException e){
System.out.println("File's MISSIN!");
}
catch(NoSuchElementException e){
System.out.println("NoSuchElementException dangus.");
}
}
public static void main(String[] args) {
new FileParser().scan();
}
}
检查带有startsWith
的行以查找介绍性关键字
然后使用split
围绕:
的字符串,该字符串返回两个字符串的数组
返回数组中的第二个字符串由[1]
到达,这将产生上述输出
不过,可以对该代码进行进一步的改进
if语句应该作为一个单独的方法进行重构和提取
应提取字符串常量,并且每行只应检查一次
一旦匹配完成,您应该转到下一行。为什么不使用
BufferedReader
?它有一个名为readLine()
的方法,可以很好地解决您的问题顺便说一句,使用temp.startsWith(“reviewers:”)
而不是temp.equals(“reviewers:”)
Scanner类有一些方法可以用来测试下一步将读取什么类型的数据。这些名字以hasNext开头。。。