Java 以双数组读取和存储文件内容

Java 以双数组读取和存储文件内容,java,Java,我需要编写一个程序,在Java中以双数组读取和存储输入的文件。文件中的值数存储在文件的第一行,然后是实际数据值 以下是我到目前为止的情况: public static void main(String[] args) throws FileNotFoundException { Scanner console = new Scanner(System.in); System.out.print("Please enter the name of the input file: "

我需要编写一个程序,在Java中以双数组读取和存储输入的文件。文件中的值数存储在文件的第一行,然后是实际数据值

以下是我到目前为止的情况:

public static void main(String[] args) throws FileNotFoundException
{
    Scanner console = new Scanner(System.in);
    System.out.print("Please enter the name of the input file: ");
    String inputFileName = console.next();

    Scanner in = new Scanner(inputFileName);

    int n = in.nextInt();
    double[] array = new double[n];

    for( int i = 0; i < array.length; i++)
    {
        array[i] = in.nextDouble();
    }

    console.close();
}
新建扫描程序(字符串)
构造函数扫描指定的字符串。不是字符串中由路径名表示的文件

如果要扫描文件,请使用

Scanner in = new Scanner(new File(inputFileName));

检查以下代码。扫描仪必须提供
文件
,而不仅仅是
字符串
,如以下代码段所示:

public class Main {
    public static void main(String[] args) {
        Scanner console = new Scanner(System.in);
        System.out.print("Please enter the name of the input file: ");
        String inputFileName = console.nextLine();

        Scanner in = null;
        try {
            in = new Scanner(new File(inputFileName));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        int n = in.nextInt();
        double[] array = new double[n];

        for (int i = 0; i < array.length; i++) {
            array[i] = in.nextDouble();
        }
        for (double d : array) {
            System.out.println(d); 
        }
        console.close();
    }
}
公共类主{
公共静态void main(字符串[]args){
扫描仪控制台=新扫描仪(System.in);
System.out.print(“请输入输入文件名:”);
字符串inputFileName=console.nextLine();
扫描仪输入=空;
试一试{
in=新扫描仪(新文件(inputFileName));
}catch(filenotfounde异常){
e、 printStackTrace();
}
int n=in.nextInt();
double[]数组=新的double[n];
for(int i=0;i
样本输出:

请输入输入文件的名称:c:/hadoop/sample.txt
43628.45
36584.94
76583.47
36585.34
86736.45
46382.5
34853.02
46378.43
34759.42
37658.32


上面代码段中的哪一行对应于第33行?int n=in.nextInt()@coscdummy您能在问题中包含输入文件吗?我想您需要
Scanner in=new Scanner(新文件(inputFileName))我更改了那一行,仍然收到错误消息:线程“main”java.io.FileNotFoundException中的异常:java.io.FileInputStream.open0(本机方法)java.io.FileInputStream.open(未知源代码)java.io.FileInputStream(未知源代码)java.util.Scanner中的inputValues.txt(系统找不到指定的文件)。(未知来源)在Project6.main(Project6.java:33)上,消息很清楚…无法找到您指定的文件
inputValues.txt
。请尝试给出一个绝对路径名而不是相对路径名…请不要只是发布一些代码。解释为什么它适用于OP。顺便说一句,此代码看起来未经测试。
System.out.printlN(d)
谢谢!现在输入详细路径时输出的代码是正确的,但当我只输入文件名时收到一个错误。这是因为我保存它的位置还是我的代码中有错误?这是一个很好的问题。这不是程序中的错误。是时候让你探索绝对路径和相对路径了。;-)
public class Main {
    public static void main(String[] args) {
        Scanner console = new Scanner(System.in);
        System.out.print("Please enter the name of the input file: ");
        String inputFileName = console.nextLine();

        Scanner in = null;
        try {
            in = new Scanner(new File(inputFileName));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        int n = in.nextInt();
        double[] array = new double[n];

        for (int i = 0; i < array.length; i++) {
            array[i] = in.nextDouble();
        }
        for (double d : array) {
            System.out.println(d); 
        }
        console.close();
    }
}