1

I am writing a program for my comp sci class, andI keep getting the same error.

Exception in thread "main" java.io.FileNotFoundException: data.txt (No such file or directory)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(FileInputStream.java:120)
    at java.util.Scanner.<init>(Scanner.java:636)
    at Search.main(Search.java:18)

Here is the beginning of my code:

import java.io.File;
import java.util.ArrayList;
import java.util.Scanner;
import java.io.IOException;
public class Search{
    public static void main(String[] args)throws IOException{
        Scanner inData = new Scanner(new File("data.txt"));

        String data=inData.nextLine();
        String[] arr = data.split(" ");   

        while(inData.hasNext()){
            String search=inData.nextLine();
            int len=search.length();
            ArrayList<String> result = new ArrayList<String>();

I have a text file in the same Java Project, so I'm not sure what the problem is and I've tried moving the location of the file around but nothing is working.

OoOoOoOoOoO
  • 131
  • 3
  • 10
  • do you have the file in the root level of the project? – Thusitha Thilina Dayaratne Jun 07 '15 at 16:45
  • Create a source folder as described in [this post](http://stackoverflow.com/a/9278270/1057230). Now access the file using `Scanner scanner = new Scanner ( Search.class.getResourceAsStream ( "data.txt" ) );` Or simply place the file inside `bin` folder. – nIcE cOw Jun 07 '15 at 16:49
  • @ThusithaThilinaDayaratne Yes – OoOoOoOoOoO Jun 07 '15 at 16:53
  • @nIcEcOw I'm getting a java.lang.NullPointerException when I do that – OoOoOoOoOoO Jun 07 '15 at 16:56
  • @OoOoOoOoOoO: If I follow the steps, as mentioned in my previous comment, then it is working fine, though I have to use `scanner = new Scanner ( ShowFile.class.getResourceAsStream ( "/files/data.txt" ) );`. Here is a [working project](https://www.dropbox.com/s/kc20dzivmt0svzl/TestingFile.rar?dl=0). This project is made in `Eclipse Mars`. I just created a Source Folder named `resources` inside it I manually created a folder `files` and copied `data.txt` file to this folder `files`. – nIcE cOw Jun 07 '15 at 17:40

2 Answers2

2

The working path of your executed code can be determined with this code:

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

Usually this is the target or the classes or the bin folder, depending on your IDE.

B4dT0bi
  • 623
  • 4
  • 21
  • Once I find that out, how can I change that or put the text file there? – OoOoOoOoOoO Jun 07 '15 at 16:42
  • you simply browse your files system to the folder ... it is most of the time in your workspace. and place the `data.txt` there . What is the result of this System.out.println.. please ? –  Jun 07 '15 at 17:13
2

You have to put the file data.txt in the root of your eclipse java project , outside your folder /src/.

enter image description here

  • is it like in the picture ? –  Jun 07 '15 at 16:46
  • Yes it looks exactly like that: Search, src, JRE... , data.txt – OoOoOoOoOoO Jun 07 '15 at 16:50
  • try `new File("/data.txt")` in the `Scanner` ? –  Jun 07 '15 at 16:54
  • is `data.txt` a *read-only* file ? check the properties of data.txt ? you are in which OS ? –  Jun 07 '15 at 16:58
  • what ever OSryx suggested u, it should work. Check these two links 1>http://stackoverflow.com/questions/22978170/java-io-filenotfoundexception-in-eclipse 2>http://stackoverflow.com/questions/19871955/java-io-filenotfoundexception-the-system-cannot-find-the-file-specified – Bacteria Jun 07 '15 at 17:06