java 如何从文本文件中的单词填充 ArrayList?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9962803/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to populate an ArrayList from words in a text file?
提问by Deepankar Bajpeyi
I have a text file containing words separated by newline , like the following format:
我有一个包含由换行符分隔的单词的文本文件,格式如下:
>hello
>world
>example
How do i create an ArrayList and store each word as an element?
如何创建一个 ArrayList 并将每个单词存储为一个元素?
回答by Jon Skeet
回答by amit
You can use apache commons FileUtils.readLines()
.
您可以使用 apache commons FileUtils.readLines()
。
I thinkthe List
it returns is already an ArrayList
, but you can use the constructor ArrayList(Collection)
to make sure you get one.
我觉得在List
它返回已经是一个ArrayList
,但你可以使用构造ArrayList(Collection)
,以确保你得到一个。
回答by daniel owens
public static void main(String[] args) throws IOException{ // TODO Auto-generated method stub
public static void main(String[] args) throws IOException{ // TODO 自动生成的方法存根
File file = new File("names.txt");
ArrayList<String> names = new ArrayList<String>();
Scanner in = new Scanner(file);
while (in.hasNextLine()){
names.add(in.nextLine());
}
Collections.sort(names);
for(int i=0; i<names.size(); ++i){
System.out.println(names.get(i));
}
回答by Tom
I put the file at "C:\file.txt"; if you run the following it fils an ArrayList with the words and prints them.
我把文件放在“C:\file.txt”;如果您运行以下命令,它将用单词填充一个 ArrayList 并打印它们。
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) throws Exception {
File file = new File("C:\file.txt");
BufferedReader br = new BufferedReader(new FileReader(file));
List<String> lines = new ArrayList<String>();
String line = br.readLine();
while(line != null) {
lines.add(line.replace(">", ""));
line = br.readLine();
}
for(String l : lines) {
System.out.println(l);
}
}
}
回答by Adam
I'm sure they're lots of libraries that do this with 1 line, but here's a "pure" Java implementation:
我确定他们有很多库都用 1 行代码来完成这项工作,但这是一个“纯”Java 实现:
Notice that we've "wrapped"/"decorated" etc. a standard FileReader (which only has read one byte at a time) with a BufferedReader which gives us a nicer readLine() method.
请注意,我们已经“包装”/“装饰”了一个标准 FileReader(一次只读取一个字节)和一个 BufferedReader,它为我们提供了一个更好的 readLine() 方法。
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(
new FileInputStream("test.txt"),
Charset.forName("ISO-8859-1")));
List<String> lines = new ArrayList<String>();
String line;
while ((line = reader.readLine()) != null) {
lines.add(line);
}
System.out.println(lines);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
reader.close();
}
}
回答by dexametason
You read the file line-by-line, create an ArrayList for Strings, and add line.substring(1) to the defined ArrayList if line.length>0.
您逐行读取文件,为字符串创建一个 ArrayList,如果 line.length>0,则将 line.substring(1) 添加到定义的 ArrayList。