Java的:如何读取文本文件 [英] Java: How to read a text file

查看:175
本文介绍了Java的:如何读取文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想读取包含空格分隔值的文本文件。价值观是整数。
我怎么能读它,把它放在一个数组列表?

下面是该文本文件的内容的一个示例:

  1 62 4 55 5 6 77

我想有它在一个ArrayList的 [1,62,4,55,5,6,77] 。我怎样才能做到这一点在Java中?


解决方案

您可以使用<一个href=\"http://docs.oracle.com/javase/8/docs/api/java/nio/file/Files.html#readAllLines-java.nio.file.Path-\"><$c$c>Files#readAllLines()得到一个文本文件中的所有行成列表与LT;弦乐方式&gt;

 为(串线:Files.readAllLines(Paths.get(/路径/要/ file.txt的))){
    // ...
}

教程:基本I / O>文件I / O>阅读,写作和创建文本文件


您可以使用<一个href=\"http://docs.oracle.com/javase/8/docs/api/java/lang/String.html#split%28java.lang.String%29\"><$c$c>String#split()基于一个普通的前pression零件拆分字符串

 为(字符串部分:line.split(\\\\ S +)){
    // ...
}

教程:数字和字符串>字符串>在字符串


您可以使用<一个href=\"http://docs.oracle.com/javase/8/docs/api/java/lang/Integer.html#valueOf%28java.lang.String%29\"><$c$c>Integer#valueOf()转换一个字符串整数

 整数i = Integer.valueOf(部分);

教程:数字和字符串>字符串>数字和字符串


您可以使用<一个href=\"http://docs.oracle.com/javase/8/docs/api/java/util/List.html#add%28E%29\"><$c$c>List#add()将元素添加到列表

  numbers.add(I)

教程:接口> List接口


所以,简而言之(假设文件没有空行,也没有拖尾/前导空格)。

 列表&LT;整数GT;数=新的ArrayList&LT;&GT;();
对(串线:Files.readAllLines(Paths.get(/路径/要/ file.txt的))){
    对于(字符串部分:line.split(\\\\ S +)){
        整数i = Integer.valueOf(部分);
        numbers.add(ⅰ);
    }
}


如果你恰巧是在Java的8不已,那么你甚至可以使用的流API ,从<一个href=\"http://docs.oracle.com/javase/8/docs/api/java/nio/file/Files.html#lines-java.nio.file.Path-\"><$c$c>Files#lines().

 列表&LT;整数GT;数= Files.lines(Paths.get(/路径/要/的test.txt))
    .MAP(线 - &GT; line.split(\\\\ S +))。flatMap(数组::流)
    .MAP(整数::的valueOf)
    .collect(Collectors.toList());

教程:使用Java 8流

处理数据>

I want to read a text file containing space separated values. Values are integers. How can I read it and put it in an array list?

Here is an example of contents of the text file:

1 62 4 55 5 6 77

I want to have it in an arraylist as [1, 62, 4, 55, 5, 6, 77]. How can I do it in Java?

解决方案

You can use Files#readAllLines() to get all lines of a text file into a List<String>.

for (String line : Files.readAllLines(Paths.get("/path/to/file.txt"))) {
    // ...
}

Tutorial: Basic I/O > File I/O > Reading, Writing and Creating text files


You can use String#split() to split a String in parts based on a regular expression.

for (String part : line.split("\\s+")) {
    // ...
}

Tutorial: Numbers and Strings > Strings > Manipulating Characters in a String


You can use Integer#valueOf() to convert a String into an Integer.

Integer i = Integer.valueOf(part);

Tutorial: Numbers and Strings > Strings > Converting between Numbers and Strings


You can use List#add() to add an element to a List.

numbers.add(i);

Tutorial: Interfaces > The List Interface


So, in a nutshell (assuming that the file doesn't have empty lines nor trailing/leading whitespace).

List<Integer> numbers = new ArrayList<>();
for (String line : Files.readAllLines(Paths.get("/path/to/file.txt"))) {
    for (String part : line.split("\\s+")) {
        Integer i = Integer.valueOf(part);
        numbers.add(i);
    }
}


If you happen to be at Java 8 already, then you can even use Stream API for this, starting with Files#lines().

List<Integer> numbers = Files.lines(Paths.get("/path/to/test.txt"))
    .map(line -> line.split("\\s+")).flatMap(Arrays::stream)
    .map(Integer::valueOf)
    .collect(Collectors.toList());

Tutorial: Processing data with Java 8 streams

这篇关于Java的:如何读取文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆