java 读取txt 分割,如何从Java中的文本文件中读取逗号分隔值?

I have got this text file with latitude and longitude values of different points on a map.

How can I split my string into latitudes and longitudes? What is the general way to do these type of things that is with other delimiters like space or tab etc.?

Sample file:

28.515046280572285,77.38258838653564

28.51430151808072,77.38336086273193

28.513566177802456,77.38413333892822

28.512830832397192,77.38490581512451

28.51208605426073,77.3856782913208

28.511341270865113,77.38645076751709

This is the code I am using to read from the file:

try(BufferedReader in = new BufferedReader(new FileReader("C:\\test.txt"))) {

String str;

while ((str = in.readLine()) != null) {

System.out.println(str);

}

}

catch (IOException e) {

System.out.println("File Read Error");

}

解决方案

You may use the String.split() method:

String[] tokens = str.split(",");

After that, use Double.parseDouble() method to parse the string value to a double.

double latitude = Double.parseDouble(tokens[0]);

double longitude = Double.parseDouble(tokens[1]);

Similar parse methods exist in the other wrapper classes as well - Integer, Boolean, etc.

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值