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
make a list out of a text file in java?
提问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 Scanner
on a File
and 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:
没有代码的简短说明:
- Create a
BufferedReader
to read from your file. - Iterate all over the file reading line by line with
reader.readLine()
, untilreadLine()
returnsnull
(== end of file) - Parse each line as a
Float
orDouble
(e.g.Float.valueOf(line)
orDouble.valueOf(line)
- Add your
Float
orDouble
objects to anArrayList
.
- 创建一个
BufferedReader
以从您的文件中读取。 - 用
reader.readLine()
,逐行遍历整个文件读取,直到readLine()
返回null
(== 文件结尾) - 将每一行解析为一个
Float
或Double
(例如Float.valueOf(line)
或Double.valueOf(line)
- 将您的
Float
或Double
对象添加到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));
}