无法在Java中修复FileNotFoundException

时间:2017-10-30 20:38:22

标签: java io filenotfoundexception

代码:

import java.io.*;
import java.util.Scanner;

public class WriteCSV {
    public static void main(String[] args) {
        String inputFilename = "coords.txt";
        String outputFilename = changeFileExtToCsv(inputFilename);

        // Open files
        PrintWriter output = openOutput(outputFilename);
        Scanner input = openInput(inputFilename);
        String line;

        while (input.hasNextLine()) 
        {
            line = input.nextLine();
            line.replace(' ', ',');
            output.println(line);
        }

        input.close();
        output.close();
    }

    /**
     * Changes file extension to ".csv"
     * @param filename
     * @return new filename.extension
     */
    public static String changeFileExtToCsv(String filename) {
        return filename.substring(0,filename.lastIndexOf('.')) + ".csv";
    }
    /**
     * Open input for reading
     * @param filename
     * @return a Scanner object
     */
    public static Scanner openInput(String filename) {
        Scanner in = null;
        try {
            File infile = new File(filename);
            in = new Scanner(infile);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            //System.out.println(filename + " could not be found");
            System.exit(0);
        }
        return in;
    }
    /**
     * Open output for writing
     * @param filename
     * @return a PrintWriter object
     */
    public static PrintWriter openOutput(String filename) {
        PrintWriter output = null;
        try {
            File outFile = new File(filename);
            output = new PrintWriter(outFile);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            //System.out.println(filename + " could not be found");
            System.exit(0);
        }
        return output;
    }
}

错误讯息:

java.io.FileNotFoundException: coords.txt (The system cannot find the file specified)
    at java.io.FileInputStream.open0(Native Method)
    at java.io.FileInputStream.open(FileInputStream.java:195)
    at java.io.FileInputStream.<init>(FileInputStream.java:138)
    at java.util.Scanner.<init>(Scanner.java:611)
    at WriteCSV.openInput(WriteCSV.java:44)
    at WriteCSV.main(WriteCSV.java:13)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)

Process finished with exit code 0

第44行是File infile = new File(filename);,而coords.txt(和.csv)位于.java文件所在的目录中。

当给File对象只提供文件名及其扩展名时,它是否应该在.java文件所在的同一目录中查找该文件?

输入整个路径后,程序运行正常(只要coords.txt文件存在)。

此外,当提交给评分程序(它是如何对我隐藏的)时,它会给出:“程序超时”。我不确定这意味着什么。

4 个答案:

答案 0 :(得分:0)

String inputFilename = "coords.txt";

您的磁盘上存在哪些内容?确保你的代码指向文件位置,我怀疑它目前没有。您可以尝试相对于类路径获取它。

InputStream input = getClass().getResourceAsStream("coords.txt"); 

答案 1 :(得分:0)

相对路径(例如coords.txt)相对于程序运行的目录而不是源代码所在的目录进行解析。

尝试打印:

System.out.println(new File("coords.txt").getAbsolutePath());

查看文件解析的绝对路径。如果你在IDE中运行,这通常是项目根目录,而不是源目录 - 但你必须弄清楚它在哪里运行。

此外,更喜欢Path APIs而不是File - Java 7引入了Path,它是一个更安全,更强大的文件I / O实用程序。

答案 2 :(得分:-1)

这与您的.java源文件的位置无关。我认为您的问题已在此处得到解答https://stackoverflow.com/a/3844316/8843830

答案 3 :(得分:-1)

我一直在乱搞它,现在它在评分程序中起作用。缺点:我不知道有什么不同。对我来说,这个问题可以被认为是封闭的,所以如果你想帮助后代,请随时发布任何想到的解决方案。

新代码:

import java.io.*;
import java.util.Scanner;

public class WriteCSV {
    public static void main(String[] args) {

        //  Grading program needs hardcoded filename. Oh, well. "
        String inputFilename = "coords.txt";
        String outputFilename = changeFileExtToCsv(inputFilename);

        // Open files
        PrintWriter output = openOutput(outputFilename);
        Scanner input = openInput(inputFilename);
        String line;

        while (input.hasNextLine()) {
            line = input.nextLine();
            line = line.replace(' ', ',');
            output.println(line);
        }

        input.close();
        output.close();
    }

    /**
     * Changes file extension to ".csv"
     * @param filename
     * @return new filename.extension
     */
    public static String changeFileExtToCsv(String filename) {
        return filename.substring(0,filename.lastIndexOf('.')) + ".csv";
    }
    /**
     * Open input for reading
     * @param filename
     * @return a Scanner object
     */
    public static Scanner openInput(String filename) {
        Scanner in = null;
        try {
            File infile = new File(filename);
            in = new Scanner(infile);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            //System.out.println(filename + " could not be found");
            System.exit(0);
        }
        return in;
    }
    /**
     * Open output for writing
     * @param filename
     * @return a PrintWriter object
     */
    public static PrintWriter openOutput(String filename) {
        PrintWriter output = null;
        try {
            File outFile = new File(filename);
            output = new PrintWriter(outFile);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            //System.out.println(filename + " could not be found");
            System.exit(0);
        }
        return output;
    }
}