user3532152
user3532152

Reputation: 17

Reading a .txt file in java in eclipse

I am trying to read a file from my computer but it says java.io.FileNotFoundException (The system cannot find the file specified)

System.out.println("READING FILE"); 


    File file = new File("Testing.txt"); //Reading file from E
    FileInputStream in = null;
    BufferedInputStream buff = null;
    DataInputStream data= null;

    String Line=""; // declare a string 
    try
    {

        in = new FileInputStream(file); // pick up the file

        buff = new BufferedInputStream(in);
        data = new DataInputStream(buff);

        while (data.available() != 0) 
        { // Read the file line by line till it reaches the end of file

            Line=Line+data; // concatenate line into string

            System.out.println(data.readLine()); // print line by line 
        }

    } catch (IOException e)

    {
        e.printStackTrace();
    }

Upvotes: 0

Views: 146

Answers (6)

matthiasboesinger
matthiasboesinger

Reputation: 458

if you use windows try:

File file = new File("e:/Testing.txt");

(that means: use slash / instead of backslash \ )

Upvotes: 1

user2875775
user2875775

Reputation: 58

Give full path instead of relative path

Upvotes: 0

DucRP
DucRP

Reputation: 139

Specifying the complete path of the file would work.

Upvotes: 0

Wilber Torres
Wilber Torres

Reputation: 1

Where is the path of Testing.txt ???

File file = new File("Testing.txt"); //Reading file from E

Here java You are reading from out of src path if the file is in src you should modify the path

File file = new File("src/Testing.txt"); //Reading file from E

And another solution is enter the full path of file

Upvotes: 0

Rika
Rika

Reputation: 796

File file = new File("E:\\Testing.txt"); 

If you trying to access E this is one of the ways to do it

Upvotes: 1

Dave Newton
Dave Newton

Reputation: 160321

You need to either:

  • Specify the complete path to the file, or...
  • Set the app's default run directory in the project's run settings, or...
  • Put the file on the classpath and read it as a resource

Upvotes: 0

Related Questions