-1

In my code, I would like to read input from a file, and I am getting an exception.

Exception in thread "main" java.io.FileNotFoundException: TestValues.csv (The system cannot find the file specified)

Exception in thread "main" java.io.FileNotFoundException: (The system cannot find the file specified)

Project Hierarchy in Eclipse :

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

How to solve this problem ?

How to find file path, in java while using eclipse?

How to find relative file path, instead of using long absolute path name ?

user2041390
  • 43
  • 1
  • 3
  • 9
  • [Exception in thread "main" java.io.FileNotFoundException: Error](http://stackoverflow.com/questions/13592325/exception-in-thread-main-java-io-filenotfoundexception-error) – dbank Mar 29 '15 at 23:01
  • [java.io.FileNotFoundException: the system cannot find the file specified](http://stackoverflow.com/questions/19871955/java-io-filenotfoundexception-the-system-cannot-find-the-file-specified) – dbank Mar 29 '15 at 23:02
  • [java.io.FileNotFoundException: in.txt, (The system cannot find the file specified)](http://stackoverflow.com/questions/12866146/java-io-filenotfoundexception-in-txt-the-system-cannot-find-the-file-specifie) – dbank Mar 29 '15 at 23:03
  • [Error in file is :- java.io.FileNotFoundException: \files\storetime.txt (The system cannot find the path specified)](http://stackoverflow.com/questions/22654041/error-in-file-is-java-io-filenotfoundexception-files-storetime-txt-the-sys) – dbank Mar 29 '15 at 23:04
  • [java.io.FileNotFoundException in eclipse](http://stackoverflow.com/questions/22978170/java-io-filenotfoundexception-in-eclipse) – dbank Mar 29 '15 at 23:04

1 Answers1

0

After seeing the question several times, and still struggling, I wanted to share the approach I followed to solve the problem.

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());

    }
}

Output:

  1. Throws Exception as file does not Exists, Dummy File Name, to find the path Path : C:\WorkSpace\CodeWorkSpace\OCJD\JavaCertification\TestValues.txt

  2. File will be read, as file Exists, under the Data folder, and path is correct Path : C:\WorkSpace\CodeWorkSpace\OCJD\JavaCertification\Data\TestValues.txt

user2041390
  • 43
  • 1
  • 3
  • 9
  • Why didn't you add this as an answer to [the original question](http://stackoverflow.com/questions/681059/read-from-file-in-eclipse) instead of asking a completely new question, that isn't a question, and self answering it? – Jonny Henly Mar 29 '15 at 22:25