java 将文本文件逐行读入字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34208962/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 22:36:37  来源:igfitidea点击:

Read a text file line by line into strings

javaarraysstringline

提问by Cardinal - Reinstate Monica

How do I read the contents of a text file line by line into Stringwithout using a BufferedReader?

如何在String不使用BufferedReader? 的情况下逐行读取文本文件的内容?

For example, I have a text file that looks like this inside:

例如,我有一个文本文件,里面看起来像这样:

Purlplemonkeys
greenGorilla

I would want to create two strings, then use something like this

我想创建两个strings,然后使用这样的东西

File file = new File(System.getProperty("user.dir") + "\Textfile.txt");
String str = new String(file.nextLine());
String str2 = new String(file.nextLine());

That way it assigns strthe value of "Purlplemonkeys", and str2the value of "greenGorilla".

这样它就分配str了 的值"Purlplemonkeys"str2的值"greenGorilla"

回答by Rustam

You can read text file to list:

您可以读取文本文件以列出:

List<String> lst = Files.readAllLines(Paths.get("C:\test.txt"));

and then access each line as you want

然后根据需要访问每一行

P.S. Files - java.nio.file.Files

PS 文件 - java.nio.file.Files

回答by Kevin Mee

You should use an ArrayList.

你应该使用一个ArrayList.

File file = new File(fileName);
Scanner input = new Scanner(file);
List<String> list = new ArrayList<String>();

while (input.hasNextLine()) {
    list.add(input.nextLine());
}

Then you can access to one specific element of your list from its index as next:

然后,您可以从其索引访问列表中的一个特定元素,如下所示:

System.out.println(list.get(0));

which will give you the first line (ie: Purlplemonkeys)

这会给你第一行(即:Purlplemonkeys)

回答by Viktor Mellgren

If you use Java 7 or later

如果您使用 Java 7 或更高版本

List<String> lines = Files.readAllLines(new File(fileName));

for(String line : lines){
   // Do whatever you want
   System.out.println(line);
}

回答by Paulius Matulionis

Sinse JDK 7 is quite easy to read a file into lines:

Sinse JDK 7 很容易将文件读入行:

List<String> lines = Files.readAllLines(new File("text.txt").toPath())

String p1 = lines.get(0);
String p2 = lines.get(1);

回答by mcacorner

You can use apache.commons.io.LineIterator

您可以使用 apache.commons.io.LineIterator

LineIterator it = FileUtils.lineIterator(file, "UTF-8");
 try {
   while (it.hasNext()) {
     String line = it.nextLine();
     // do something with line
   }
 } finally {
   it.close();
 }

One can also validate line by overriding boolean isValidLine(String line)method. refer doc

还可以通过覆盖boolean isValidLine(String line)方法验证行。 参考文档

回答by Jan

How about using commons-io:

如何使用commons-io

List<String> lines = org.apache.commons.io.IOUtils.readLines(new FileReader(file));

//Direct access if enough lines read
if(lines.size() > 2) {
  String line1 = lines.get(0);
  String line2 = lines.get(1);
}

//Iterate over all lines
for(String line : lines) {
  //Do something with lines
}

//Using Lambdas
list.forEach(line -> {
  //Do something with line
});

回答by Rehman

File file = new File(fileName);
Scanner input = new Scanner(file);
while (input.hasNextLine()) {
  System.out.println(input.nextLine());
}