[英]Java - How to read a text file with Int's and Strings in the same line
If a text file contains如果文本文件包含
1 Apple
2 Banana
3 Orange
4 Carrot
5 Lemon
6 Lime
7 Mango
How can the file be read to store the contents of for example, line 4 carrot into a variable and line 6 Lime into a separate variable?如何读取文件以将第 4 行 carrot 的内容存储到一个变量中,将第 6 行 Lime 的内容存储到一个单独的变量中?
//Get data from file
String fruit = "";
String greenFruit = "";
FileReader file = new FileReader("my/file/path.txt");
BufferedReader reader = new BufferedReader(file);
System.out.println(fruit);
System.out.println(greenFruit);
For the end result to be something like最终结果是这样的
"Fruit number 5 is a Lemon and Fruit Number 6 is a Lime"
Try something like this:尝试这样的事情:
String line = "";
String[] tokens;
int number;
String name;
while((line = reader.readLine()) != null)
{
tokens = line.split(" ");
// Use the following if you would rather split on whitespace for tab separated data
// tokens = line.split("\\s+");
number = Integer.parseInt(tokens[0]);
name = tokens[1];
System.out.println("Fruit number " + number + " is a " + name + "."
}
Read up on the String.split()
method.阅读String.split()
方法。
We can make use java NIO api to read all lines from the file,我们可以使用 java NIO api 从文件中读取所有行,
private static final Pattern SPACE = Pattern.compile(" ");
public static void main(final String[] args) throws URISyntaxException, IOException {
final String output =
Files.readAllLines(Paths.get(FileParser.class.getClassLoader().getResource("input.txt").toURI()))
.stream()
.map(SPACE::split)
.map(it -> new Fruit(it[0], it[1]))
.map(Fruit::toString)
.collect(Collectors.joining(" and "));
System.out.println(output);
}
}
class Fruit {
private final String number;
private final String name;
Fruit(final String number, final String name) {
this.number = number;
this.name = name;
}
@Override
public String toString() {
return "Fruit number ".concat(String.valueOf(number)).concat(" is a ").concat(name);
}
}
Output will be like below, Output 将如下所示,
Fruit number 1 is a Apple and Fruit number 2 is a Banana and Fruit number 3 is a Orange and Fruit number 4 is a Carrot and Fruit number 5 is a Lemon and Fruit number 6 is a Lime and Fruit number 7 is a Mango
You can read using Scanner
and check if there is more using hasNext()
您可以使用Scanner
阅读并检查是否有更多使用hasNext()
FileReader file = new FileReader("file");
Scanner scanner = new Scanner(file);
while (scanner.hasNext()) {
int number = scanner.nextInt();
String name = scanner.next();
}
scanner.close();
Java 8 style: Java 8款:
// normal solution
try (Stream<String> stream = Files.lines(Paths.get(fileName))) {
stream.foreach(line -> {
tokens = line.split(" ");
number = Integer.parseInt(tokens[0]);
name = tokens[1];
System.out.println("Fruit number " + number + " is a " + name + "."
});
} catch (IOException e) {
e.printStackTrace();
}
// more OOP style
public class Fruit {
private int number;
private String name;
public Fruit(String line) {
String[] line = line.split(" ");
this.number = Integer.parseInt(line[0]);
this.name = line[1];
}
}
List<Fruit> list = new ArrayList;
try (Stream<String> stream = Files.lines(Paths.get(fileName))) {
//1. convert all content to a object
//2. convert it into a List
list = stream
.map(Fruit::new)
.collect(Collectors.toList());
} catch (IOException e) {
e.printStackTrace();
}
// then you can loop the list get what you want
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.