繁体   English   中英

Java,从input.txt读取文件有问题

[英]Java, reading the file from input.txt has an issue

我正在做这个项目。 出于我的目的,我需要使用它们来找到中位数。

在我看来,我需要看看阅读

我还创建了如下所示的 input.txt

3 7
1 4 5 7 9 11 13

在代码片段下方,我为读取路径创建了变量。

// need the variable of filename to read
    private static final String INPUT_FILE_PATH = "input.txt";

所以,然后我在 main 函数中附加了需要读取 input.txt 中的数字整数的代码,如下所示

public static void main(String args[]){
        // read the input file
        // TODO need to fix this readpath that gets the bad input
        // ! ASAP
        Path inputPath = Paths.get(INPUT_FILE_PATH);
        Charset charset = Charset.forName("UTF-8");
        List<String> fileLines = new ArrayList<>(0);
        try {
            fileLines = Files.readAllLines(inputPath, charset);
        } catch (IOException ex) {
            System.err.println("Error reading file: " + ex.getMessage());
            System.exit(1);
        }
        int read_line = 0;
        try {
            read_line = Integer.parseInt(fileLines.get(0));
        } catch (NumberFormatException ex) {
            System.err.println("bad file input");
            System.exit(1);
        }
        System.out.println("reading... " + read_line);
        // end of reading the filename operation
}

因此,这段代码应该可以工作。 我得到的输出是错误的文件输入。 我不明白为什么它会得到坏文件。 顺便说一下,我将所有文件放在同一个目录中。

    int read_line = 0;
    int read_line2 = 0;
    try {
        String[] words = fileLines.get(0).split("\\s+"); // Split on whitespace.
        read_line = Integer.parseInt(words[0]);
        read_line2 = Integer.parseInt(words[1]);
    } catch (NumberFormatException ex) {
        System.err.println("bad file input - not a number; " + e.getMessage());
        System.exit(1);
    }

该行包含两个数字,并导致 NumberFormatException。

我正在做这个项目。 出于我的目的,我需要使用它们来找到中位数的中位数。

就我而言,我需要看读

我还像下面那样创建了input.txt

3 7
1 4 5 7 9 11 13

在代码段下方,我为readpath创建了变量。

// need the variable of filename to read
    private static final String INPUT_FILE_PATH = "input.txt";

因此,然后我在主函数中的input.txt中附加了需要读取数字整数的代码,如下所示

public static void main(String args[]){
        // read the input file
        // TODO need to fix this readpath that gets the bad input
        // ! ASAP
        Path inputPath = Paths.get(INPUT_FILE_PATH);
        Charset charset = Charset.forName("UTF-8");
        List<String> fileLines = new ArrayList<>(0);
        try {
            fileLines = Files.readAllLines(inputPath, charset);
        } catch (IOException ex) {
            System.err.println("Error reading file: " + ex.getMessage());
            System.exit(1);
        }
        int read_line = 0;
        try {
            read_line = Integer.parseInt(fileLines.get(0));
        } catch (NumberFormatException ex) {
            System.err.println("bad file input");
            System.exit(1);
        }
        System.out.println("reading... " + read_line);
        // end of reading the filename operation
}

结果,此代码假定可以正常工作。 我得到的输出是错误的文件输入。 我不明白为什么它会得到错误的文件。 顺便说一句,我将所有文件放在同一目录中。

暂无
暂无

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

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