简体   繁体   English

我知道的文件的FileNotFoundException在目录中

[英]FileNotFoundException for a file I know is in the directory

In the following program I am trying to read from the Hw1_1.java source code. 在下面的程序中,我尝试从Hw1_1.java源代码中读取。 I get a FileNotFoundException every time (probably for a good reason). 我每次都会收到FileNotFoundException(可能是有充分的理由)。 I know the program isn't complete as I am just trying to stop getting the exception. 我知道程序还没有完成,因为我只是想停止获取异常。 I am at a loss. 我很茫然。

If someone could point me in the right direction I would greatly appreciate it. 如果有人能指出正确的方向,我将不胜感激。

package hw1_1;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;

public class Hw1_1 {

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

        Scanner console = new Scanner(System.in);
        System.out.println("Please enter the name of a java source code file");

        String inputFileName = console.next();
        String outputFileName = (inputFileName + ".txt");

        try {

            File inputFile = new File(inputFileName);
            Scanner in = new Scanner(inputFile);
            PrintWriter out = new PrintWriter(outputFileName);

            while ( in .hasNextLine()) {
                String line = console.nextLine();
                out.println(line);
            }

            in .close();
            out.close();

        } catch (FileNotFoundException exception) {
            System.out.println("File Not Found");
        }
    }
}

It's really a good idea - to first check the user directory of your java program. 这是一个好主意-首先检查Java程序的用户目录。 Once you know, you can easily debug the FileNotFoundException issue. 知道之后,您可以轻松调试FileNotFoundException问题。

You can simply print the user directory from following code. 您可以简单地从以下代码中打印用户目录。

System.out.println(System.getProperty("user.dir")) ;

Using absolute path for the file is another way of solving problem, but that's a little irregular way of doing. 对文件使用绝对路径是解决问题的另一种方法,但这是不规则的做法。

You need to be aware of path complexity in your code, especially if you are using IDE as IDE can have a different execution path 您需要注意代码中的路径复杂性,尤其是在使用IDE的情况下,因为IDE可以具有不同的执行路径

Based on your code, if the value inputFileName is just the file name (let's say log.txt ) and the execution path is actually different, then your code will never find the path 根据您的代码,如果值inputFileName只是文件名(假设为log.txt )并且执行路径实际上不同,那么您的代码将永远找不到该路径

The quickest and dirty solution to quickly prove this is to use the full absolute path as the value of inputFileName for example: 快速证明这一点的最快,最肮脏的解决方案是使用完整的绝对路径作为inputFileName的值,例如:

String inputFileName = "/var/tmp/log.txt"

or 要么

String inputFileName = "C:/workspace/temp/log.txt"

Once this verifies that your code can read the file, then you can start handling the path issue, good luck. 验证您的代码可以读取文件后,即可开始处理路径问题,祝您好运。

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

相关问题 FileNotFoundException(没有这样的文件或目录) - FileNotFoundException (no such file or directory) Spring FileNotFoundException ..(没有此类文件或目录) - Spring FileNotFoundException .. (No such file or directory) IntelliJ IDEA文件FileNotFoundException,目录和文件是否存在? - IntelliJ IDEA File FileNotFoundException, directory and file exists? java.io.FileNotFoundException:..(无此类文件或目录) - java.io.FileNotFoundException: .. (No such file or directory) FileNotFoundException 打开失败:ENOENT(没有这样的文件或目录) - FileNotFoundException open failed: ENOENT (No such file or directory) java.io.FileNotFoundException:没有这样的文件或目录错误 - java.io.FileNotFoundException: No such file or directory Error java.io.FileNotFoundException:[文件路径](没有这样的文件或目录) - java.io.FileNotFoundException: [filepath] (No such file or directory) FileNotFoundException(不是目录) - FileNotFoundException (Not a directory) 在我的SpringBoot项目中,当我将文件目录更改为src / main / resources时,出现FileNotFoundException异常 - In my SpringBoot project, an FileNotFoundException occred when i change the file directory to src/main/resources FileNotFoundException:ENOENT ...但是,但是,我有一个文件 - FileNotFoundException: ENOENT…but, but, but I have a file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM