【问题标题】:Java Eclipse - The system cannot find the file specified ( java.io.FileNotFoundException ) [duplicate]Java Eclipse - 系统找不到指定的文件(java.io.FileNotFoundException)[重复]
【发布时间】:2015-03-29 22:05:24
【问题描述】:

在我的代码中,我想从文件中读取输入,但遇到了异常。

线程“main”java.io.FileNotFoundException中的异常:TestValues.csv(系统找不到指定的文件)

线程“main”java.io.FileNotFoundException中的异常:(系统找不到指定的文件)

Eclipse 中的项目层次结构:

Project folder
     src
          Package Folder
               FileName.java
     JRE System Library
     Data
          TestValues.txt

如何解决这个问题?

使用eclipse时如何在java中查找文件路径?

如何找到相对文件路径,而不是使用长的绝对路径名?

【问题讨论】:

标签: java eclipse filenotfoundexception file-not-found


【解决方案1】:

看了几次问题,还是很纠结,想分享一下我解决问题的方法。

package javacertification;

import java.io.File;
import java.io.IOException;

public class FindFileName {

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

    FindFileName ffn = new FindFileName();
    ffn.printFilePath();
}

public void printFilePath(){

    // http://stackoverflow.com/questions/681059/read-from-file-in-eclipse
    System.out.println("1. Throws Exception as file does not Exists, Dummy File Name, to find the path");
    File file = new File("TestValues.txt");
    System.out.println("\t" + "Path : " + file.getAbsolutePath());
    System.out.println("");
    System.out.println("2. File will be read, as file Exists, under the Data folder, and path is correct");
    file = new File("Data\\TestValues.txt");
    System.out.println("\t"+ "Path : " + file.getAbsolutePath());

    }
}

输出:

  1. 当文件不存在时抛出异常,虚拟文件​​名,查找路径 路径:C:\WorkSpace\CodeWorkSpace\OCJD\JavaCertification\TestValues.txt

  2. 文件将被读取,因为文件存在,位于 Data 文件夹下,并且路径正确 路径:C:\WorkSpace\CodeWorkSpace\OCJD\JavaCertification\Data\TestValues.txt

【讨论】:

  • 您为什么不将其添加为the original question 的答案,而不是提出一个全新的问题,这不是问题,然后自己回答?
猜你喜欢
  • 2013-11-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多