简体   繁体   English

如何从.txt文件中读取双精度到ArrayList中

[英]How to read doubles from .txt file into an ArrayList

I'm trying to read doubles from a file that contains 100,000 doubles, arranged in lines of two doubles each separated by a space. 我正在尝试从包含100,000个双打的文件中读取双打,这些文件以两个双打的行排列,每个双打由一个空格隔开。 Like this: 像这样:

2.54343 5.67478
1.23414 5.43245
7.64748 4.25536
...

My code so far: 我的代码到目前为止:

Scanner numFile = new Scanner(new File("input100K.txt").getAbsolutePath());
ArrayList<Double> list = new ArrayList<Double>();
while (numFile.hasNextLine()) {
    String line = numFile.nextLine();
    Scanner sc = new Scanner(line);
    sc.useDelimiter(" ");
    while(sc.hasNextDouble()) {
        list.add(sc.nextDouble());
    }
    sc.close();
}
numFile.close();
System.out.println(list);
}

After this code runs, it prints to the console and empty ArrayList [] , and I can't figure out why. 此代码运行后,它打印到控制台并清空ArrayList [] ,我无法弄清楚原因。

Removing the getAbsolutePath() from the file gives me this line: 从文件中删除getAbsolutePath()给了我这一行:

Scanner numFile = new Scanner(new File("input100K.txt"));

but when I run the program, it is giving me a FileNotFoundException. 但是当我运行程序时,它给了我一个FileNotFoundException。 I know the file exists, I can see it and open it. 我知道文件存在,我可以看到并打开它。

input100K.txt is located in the src package folder along with the program. input100K.txt与程序一起位于src包文件夹中。 is there somewhere specific where the file must be for this to work? 有什么特定的文件必须在哪里工作?

EDIT : As Evgeniy Dorofeev pointed out, the file needs to be in the project folder (parent of src folder) for the program to find it. 编辑 :正如Evgeniy Dorofeev所指出的,文件需要在项目文件夹(src文件夹的父文件夹)中才能找到程序。

When you create Scanner like this new Scanner(new File("input100K.txt").getAbsolutePath()) ; 当你像这个new Scanner(new File("input100K.txt").getAbsolutePath())仪一样创建扫描new Scanner(new File("input100K.txt").getAbsolutePath()) ; you are scanning file path as input not file itself. 您正在扫描文件路径作为输入而不是文件本身。 Do it this way new Scanner(new File("input100K.txt")); 这样做new Scanner(new File("input100K.txt"));

You can try using split() method on the line you just have read, then parsing each part to a double . 您可以尝试在刚读过的行上使用split()方法,然后将每个部分解析为double Note that it's not necessary to create two Scanner s: 请注意,没有必要创建两个Scanner

Scanner sc = new Scanner(new File("input100K.txt"));
sc.useDelimiter(" ");
ArrayList<Double> list = new ArrayList<Double>();

while (sc.hasNextLine()) {
    String[] parts = sc.nextLine().split(" "); // split each line by " "
    for (String s : parts) {
        list.add(Double.parseDouble(s)); // parse to double
    }
}
sc.close();

System.out.println(list);

replace the line 替换线

Scanner numFile = new Scanner(new File("input100K.txt").getAbsolutePath());

with

Scanner numFile = new Scanner(new File("input100K.txt"));

Here is a sample program : 这是一个示例程序:

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

        List<Double> doubleList = new ArrayList<Double>();
        Scanner scanner = new Scanner(new File("/home/visruthcv/inputfile.txt"));
        while (scanner.hasNextDouble()) {

            double value = scanner.nextDouble();

            System.out.println(value);

            doubleList.add(value);

        }
        /*
         * for test print
         */
        for (Double eachValue : doubleList) {
            System.out.println(eachValue);
        }

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM