java 用java中的文本文件制作一个列表?

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

make a list out of a text file in java?

javalisttext-files

提问by Ben Fossen

I have a text file full of numbers and I want to read the numbers into Java and then make a list that I can sort. it has been a while since I have used java and I am forgetting how to do this.

我有一个充满数字的文本文件,我想将这些数字读入 Java,然后制作一个可以排序的列表。自从我使用 java 已经有一段时间了,我忘记了如何做到这一点。

the text file looks something like this

文本文件看起来像这样

4.5234  
9.3564
1.2342
4.4674
9.6545
6.7856

采纳答案by Octavian A. Damiean

You do something like this.

你做这样的事情。

EDIT: I've tried to implement the changes dbkk mentioned in his comments so the code actually will be correct. Tell me if I got something wrong.

编辑:我已经尝试实现他的评论中提到的 dbkk 更改,因此代码实际上是正确的。告诉我是不是有什么问题。

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class ReadList {

    public static void main(String[] args) throws IOException {

        BufferedReader in = null;
        FileReader fr = null;
        List<Double> list = new ArrayList<Double>();

        try {
            fr = new FileReader("list.txt");
            in = new BufferedReader(fr);
            String str;
            while ((str = in.readLine()) != null) {
                list.add(Double.parseDouble(str));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            in.close();
            fr.close();
        }

        for (double d : list) System.out.println(d);
    }

}

回答by Colin Hebert

You can use a Scanneron a Fileand use the nextDouble()or nextFloat()method.

您可以Scanner在 a 上File使用a并使用nextDouble()ornextFloat()方法。

Scanner scanner = new Scanner(new File("pathToYourFile"));
List<Double> doubles = new ArrayList<Double>();
while(scanner.hasNextDouble()){
    doubles.add(scanner.nextDouble());
}
Collections.sort(doubles);


Resources :

资源 :

回答by Sean Patrick Floyd

This is really fun and simple if you use Guava:

如果你使用Guava,这真的很有趣也很简单:

final File f = new File("your/file.txt");
final List<Float> listOfFloats =
    Lists.transform(Files.readLines(f, Charset.defaultCharset()),
        new Function<String, Float>(){

            @Override
            public Float apply(final String from){
                return Float.valueOf(from);
            }
        });

And here's a similar version using Apache Commons / IO:

这是使用Apache Commons / IO的类似版本:

final File f = new File("your/file.txt");
final List<String> lines = FileUtils.readLines(f);
final List<Float> listOfFloats = new ArrayList<Float>(lines.size());
for(final String line : lines){
    listOfFloats.add(Float.valueOf(line));
}

回答by Abhinav Sarkar

Use java.util.Scanner:

使用java.util.Scanner

public List<Doubles> getNumbers(String fileName) {
  List<Double> numbers = new ArrayList<Double>();
  Scanner sc = new Scanner(new File(fileName));

  while (sc.hasNextDouble()) {
      numbers.add(sc.nextDouble());
  }

  return numbers;
}

回答by Grodriguez

Short instructions without code:

没有代码的简短说明:

  1. Create a BufferedReaderto read from your file.
  2. Iterate all over the file reading line by line with reader.readLine(), until readLine()returns null(== end of file)
  3. Parse each line as a Floator Double(e.g. Float.valueOf(line)or Double.valueOf(line)
  4. Add your Floator Doubleobjects to an ArrayList.
  1. 创建一个BufferedReader以从您的文件中读取。
  2. reader.readLine(),逐行遍历整个文件读取,直到readLine()返回null(== 文件结尾)
  3. 将每一行解析为一个FloatDouble(例如Float.valueOf(line)Double.valueOf(line)
  4. 将您的FloatDouble对象添加到ArrayList.

回答by Lokathor

Not accounting for the fact that you need to setup exception handling and such based on your sourrounding code, it'll look something like this:

不考虑您需要根据您的周围代码设置异常处理等事实,它看起来像这样:

BufferedReader br = new BufferedReader(new FileReader(new File("filename.txt")));
ArrayList<Double> ald = new ArrayList<Double>();
String line;
while(true)
{
    line = br.readLine();
    if(line == null) break;
    ald.add(new Double(line));
}