0

I'm getting the following error

java.io.FileNotFoundException: in.txt, (The system cannot find the file specified)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(Unknown Source)
    at java.io.FileInputStream.<init>(Unknown Source)
    at java.io.FileReader.<init>(Unknown Source)
    at FileTest.main(FileTest.java:50)

Yet Im certain that I have created a in.txt file under the src, bin, and root directory. I also tried specifying the full directory in my main parameters, but still not working. Why isn't Eclipse picking it up?

import java.io.*;
public class FileTest {
    public static void main(String[] args) {

        try {
            String inFileName = args[0];
            String outFileName = args[1];
            BufferedReader ins = new BufferedReader(new FileReader(inFileName));
            BufferedReader con = new BufferedReader(new InputStreamReader(System.in));
            PrintWriter outs = new PrintWriter(new FileWriter(outFileName));

            String first = ins.readLine(); // read from file
            while (first != null) {
                System.out.print("Type in a word to follow " + first + ":");
                String second = con.readLine(); // read from console
                // append and write
                outs.println(first + ", " + second);
                first = ins.readLine(); // read from file
            }
            ins.close();
            outs.close();
        } catch (IOException ex) {
            ex.printStackTrace(System.err);
            System.exit(1);
        }

    }
}
user133466
  • 3,391
  • 18
  • 63
  • 92

7 Answers7

6

Given the error message, I would guess that Java is looking for the file name in.txt,, with a trailing comma.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
2

By default, Eclipse will set the working directory to the project folder. If you have made changes to the settings, you can still find out the working directory by this simple line of code:

System.out.println(new java.io.File("").getAbsolutePath());

Put your text file in the folder printed, and you should be fine.

nhahtdh
  • 55,989
  • 15
  • 126
  • 162
2

I took your code and executed it with the following command-line params:

in.txt out.txt

It works with no problems at all. Check your command line.

Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
0

Put it in the project directory and if that doesn't work, the bin folder

Tom G
  • 3,650
  • 1
  • 20
  • 19
0

Open your "Debug Configurations" and set, under the tab "Arguments", the Working Directory. Relative paths will be relative to the Working directory.

Rudolf Mühlbauer
  • 2,511
  • 16
  • 18
0

I created a new project Learning in my eclipse and put in.txt file inside a source directory . I tested using the sample java file .

public static void main(String[] args) {
    File f = new File("a.txt");
    System.out.println(f.getAbsolutePath());
}

Output:

/home/bharathi/workspace/Learning/a.txt

It looks in a project root directory . You can put in.txt inside a project's root directory or give the path until source directory .

kannanrbk
  • 6,964
  • 13
  • 53
  • 94
0

There can be two problems:

  1. In your console, the error shown with the ',' check -- >
    java.io.FileNotFoundException: in.txt,
    so probably JVM looks for a filename with the ','

  2. The second case can be, You in.txt file is not in the right location. Check whether it's is the Root of the project. or the main path.

That would solve these types of problems.

KingFeming
  • 1,053
  • 10
  • 21