ceri Westcott
ceri Westcott

Reputation: 45

Inputting a text file into a program?

I had a lecture today on inputting and outputting but it didn't really seem to explain where the text file is etc..

here is my code:

  package inputoutput;

import java.util.*;
import java.io.*;

public class input {

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

    String name;
    int lineCount = 0;

    File input = new File("lab1task3.txt");
    Scanner in = new Scanner(input);

    while(in.hasNextLine()){
        lineCount++;

    }
    System.out.println(lineCount);


}

}

I get a file not found exception but the text file is in the same folder as the program?

Upvotes: 0

Views: 93

Answers (3)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285403

You're confusing class file location and the "user's working directory", the latter being what Java uses to determine the root of the file path (unless absolute paths are needed), and you can find its location easily via:

System.out.println(System.getProperty("user.dir"));

I advise you to forgo use of files altogether when all you need to do is read in data, and instead get the text file as a program resource:

// where you swap the name of your class for MyClass
InputStream fileResource = MyClass.class.getResourceAsStream("myFile.txt");
Scanner scanner = new Scanner(inputStream);

Note that if you must use a File, then find out what the user's working directory is, as shown above, and then tailor your file path so that it is relative to this working directory.

Try:

File file = new File("src/inputoutput/lab1task3.txt");

Upvotes: 1

ThePerson
ThePerson

Reputation: 3236

Please first read up on the difference between relative and absolute paths. An absolute path is: C:\Users\Ceri\workspace1\inputoutput\src\inputoutput\lab1task3.txt

A relative path would be just "lab1task3.txt", which is what is given. That means that lab1task3.txt can be found relative to the working directory (e.g if the working directory was "C:\Users\Ceri\workspace1\inputoutput\src\inputoutput\" then it would find it).

However, you could also use an absolute path, but remember that doing so means that it will only work if a file is in the same place on the machine running it. E.g, if you submit with "C:\Users\Ceri\workspace1\inputoutput\src\inputoutput\" in your code then it will only work if someone else has that same file and location on their computer. Please note that if this is an assignment, the module convenor/marker probably does not have afolder called C:\Users\Ceri.... If you submit your work using a relative path, anyone using your code just needs to make sure the file is relatively in the same place (e.g in the same folder).

If this doesn't matter, you need to escape the back slash characters with another back slash in the path. This should work:

package inputoutput;

import java.util.*;
import java.io.*;

public class input {

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

    String name;
    int lineCount = 0;

    File input = new File("C:\\Users\\Ceri\\workspace1\\inputoutput\\src\\inputoutput\\lab1task3.txt");
    Scanner in = new Scanner(input);

    while(in.hasNextLine()){
        lineCount++;

    }
    System.out.println(lineCount);


    }
}

I notice you are using eclipse. Your "working directory" is your workspace. Therefore you want to move your file to:

C:\Users\Ceri\workspace1\inputoutput\lab1task3.txt

This should work for you using a "relative" path which you had in your opening post.

Upvotes: 3

HavelTheGreat
HavelTheGreat

Reputation: 3386

My guess is that your current working directory is not the same place as the project location. If your working directory were, the file would definitely be found if it does indeed have that name.
To workaround this issue you can always be using a InputStream instead, like so:

InputStream inputStream = new InputStream("lab1task3.txt");
Scanner scanner = new Scanner(inputStream);

If you want to see your current working directory you can use something like this:

public class JavaApplication1 {
  public static void main(String[] args) {
       System.out.println("Working Directory = " +
              System.getProperty("user.dir"));
  }
}

Upvotes: 0

Related Questions