找不到文件Java [英] File not found java

查看:59
本文介绍了找不到文件Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Java中读取文本文件时遇到问题.该类是 FlashCardReader ,我有以下构造函数可以处理部分读数.

I have a problem when reading a text file in java. The class is FlashCardReader and I have the following constructor that handles the part of the reading.

public FlashCardReader( String fileName ) {
    try{

        reader = new BufferedReader(new FileReader(fileName));

    }catch(FileNotFoundException e){
        System.out.println("The file was not found or the name may be wrong!");
    }
}

我的主要方法如下:

public static void main(String[] args) {
    FlashCardReader fcr = new FlashCardReader("Questions.txt");
}

最后的输出是: 找不到文件或名称可能不正确!

And the final output is: The file was not found or the name may be wrong!

一些帮助将不胜感激,欢呼!

Some help would be greatly appreciated, cheers!

推荐答案

您可以使用此Java代码打印从中执行Java程序的当前目录,

You can print the current directory of your java program where it is executed from with this java code,

System.out.println("CurrentDir: " + (new File(".").getCanonicalPath()));

说它打印,

CurrentDir: D:\pkr\test

然后,您可以正确选择可以正确定位文件的路径.

Then you can correctly choose a path through which your file can be correctly located.

最有可能的是,您的 src 文件夹应位于 test 目录中,在这种情况下,您可以将文件从 src 文件夹移至 test 文件夹,或像这样在代码中引用您的文件,

Most likely, your src folder should be in test directory and in that case you can either move your file from src folder to test folder or refer your file in your code like this,

.. \\ Questions.txt

应该能够读取您的文件.

which should be able to read your file.

让我知道这是否可行.

这篇关于找不到文件Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆