我很少使用分隔符,需要读取一个文本文件,其中存储了一些对象,这些对象的数据以逗号(",")分隔在单行中。然后将分隔的字符串用于创建一个新对象,该对象添加到一个ArrayList中。
Amadeus,Drama,160 Mins.,1984,14.83
As Good As It Gets,Drama,139 Mins.,1998,11.3
Batman,Action,126 Mins.,1989,10.15
Billy Elliot,Drama,111 Mins.,2001,10.23
Blade Runner,Science Fiction,117 Mins.,1982,11.98
Shadowlands,Drama,133 Mins.,1993,9.89
Shrek,Animation,93 Mins,2001,15.99
Snatch,Action,103 Mins,2001,20.67
The Lord of the Rings,Fantasy,178 Mins,2001,25.87
我正在使用Scanner读取文件,但是遇到了"没有找到行"的错误,并且整个文件都被存储为一个字符串:
Scanner read = new Scanner (new File("datafile.txt"));
read.useDelimiter(",");
String title, category, runningTime, year, price;
while (read.hasNext())
{
title = read.nextLine();
category = read.nextLine();
runningTime = read.nextLine();
year = read.nextLine();
price = read.nextLine();
System.out.println(title + " " + category + " " + runningTime + " " +
year + " " + price + "\n"); // just for debugging
}
read.close();
read.next()
УђїСИЇТў»nextLine()
сђѓ - Jeroen Vannevel