系统无法找到指定的文件 [英] System not able to find the specified file

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

问题描述

我试图从Java文件中读取文件。但是,当文件与源文件位于同一目录中时,它显示一个错误,指出系统找不到指定的文件。

下面是代码:

  import java.io.BufferedReader ; 
import java.io.FileReader;
import java.io.IOException;

公共类文件
{

public static void main(String [] args){
BufferedReader br = null;

尝试{

String sCurrentLine;

br = new BufferedReader(new FileReader(RoomList.txt)); ((sCurrentLine = br.readLine())!= null){
System.out.println(sCurrentLine);

while

$ b $ catch(IOException e){
e.printStackTrace();
} finally {
try {
if(br!= null)br.close();
} catch(IOException ex){
ex.printStackTrace();






I尽我所能,但它只是不起作用。请让我知道这个的原因。
我再次补充说,文件RoomList.txt和这个代码文件是在同一个目录中,甚至在使用完全限定的路径名​​后,它不起作用。我在Stackoverflow中寻找类似的问题,并尝试了他们,但似乎并没有工作。请帮忙。

解决方案

在你的代码中没有问题,它只是当你不给任何文件位置系统试图找到文件在项目根目录中。



检查下图:

:如果您使用Eclipse,应该是这样的。不要只是粘贴项目目录的截图。
$ b $ p code> C \ CSMAIN\ws\TestFile\src\file .java



C:\CSMAIN\ws\TestFile\RoomList.txt
如果你没有使用任何IDE,那么你可以创建一个项目并把这两个文件放到项目目录中,并使用命令提示符编译它并运行它。它的工作。


I am trying to read from a file using Java. But it shows an error saying that system cannot find the specified file, when the file is lying in the same directory as the source file.

below is the code:

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class file
{

  public static void main(String[] args) {
    BufferedReader br = null;

    try {

      String sCurrentLine;

      br = new BufferedReader(new FileReader("RoomList.txt"));

      while ((sCurrentLine = br.readLine()) != null) {
        System.out.println(sCurrentLine);
      } 

    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      try {
        if (br != null) br.close();
      } catch (IOException ex) {
        ex.printStackTrace();
      }
    }
  }
}

I tried all I could but it just doesn't work. Please let me know the reasons for this. Again I will add that the file RoomList.txt and this code file are in the same directory and even after using fully qualified pathname, it doesn't work. I looked for similar answers for this problem in Stackoverflow and tried them but still it does not seem to be working. Please help.

解决方案

There is not problem in your code, its just that when you do not give any file location The System tries to find the file in the Project root.

Check the image below:

EDIT: If you are using Eclipse, it should be like this. WHt dont you just paste a screenshot of your project directory.

C:\CSMAIN\ws\TestFile\src\file.java

C:\CSMAIN\ws\TestFile\RoomList.txt

If you are not using any IDE then, you can create a project and put both the files in the project directory and compile it using command prompt and run it. It works.

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

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