java分隔符读取文件_java – 在读取文件时使用分隔符

我几乎没有使用分隔符的经验,我需要读取一个文本文件,该文件存储多个对象,其数据以逗号(“,”)分隔的单行存储.然后使用单独的字符串来创建添加到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();

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值