[英]Reading a Multi-line text file with different data in each line
I'm attempting to read a text file with Scanner dataFile = new Scanner("FileName.txt");
我正在尝试使用
Scanner dataFile = new Scanner("FileName.txt");
读取文本文件Scanner dataFile = new Scanner("FileName.txt");
the text file has lines which are supposed to be read for info on them. 文本文件中的行应被读取以获取有关其信息。 I'm using:
我正在使用:
while(dataFile.hasNext()){
String line = dataFile.nextLine();
...
}
to loop through the lines. 遍历线路。 I need to extract a String at the start of the line, an Integer after the String, and a Double after the Integer.
我需要在行的开头提取一个String,在该字符串之后提取一个Integer,并在Integer之后提取一个Double。 They have spaces in-between each part I need to extract so I am willing to \\ make a substring search through the line to individually search for the parts in the line.
我需要提取的每个部分之间都有空格,因此我愿意\\在整个行中进行子字符串搜索,以分别搜索行中的部分。
I am wondering, Is there an easier and quicker method to doing this? 我想知道,是否有一种更简便快捷的方法?
Example contents of text file: 文本文件的示例内容:
Name 10 39.5
Hello 75 87.3
Coding 23 46.1
World 9 78.3
If they're all the same format, you can split on whitespace. 如果它们都是相同的格式,则可以在空白处分割。
String str = "Name 10 39.5";
String[] arr = str.split(" ");
String s = arr[0];
int i = Integer.valueOf(arr[1]);
double d = Double.valueOf(arr[2]);
substring search?
you may use 你可以用
String[] str=line.split(" ");
and then str[0] is the string you find
str[1] is the string of Integer ,you cast it
and the str[2] is the string of Double , cast
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.