Text file not found

So I can't seem to get my program to read a file "testOne.txt" and it keeps throwing the file not found exception. I am using eclipse and am keeping the testOne.txt file in the src folder in the project. I am hoping another set of eyes might be able to spot why my program cannot find the text file.

*Edit- I was able to get that initial question resolved but I am having another problem not related to the original question. Now I am getting a NullPointerException on line 8 in my Main class (which I just posted below my BubbleSort class). Is it because I incorrectly declared the array or something?

package cse.unl;
import java.util.*;
import java.io.File;
import java.io.FileNotFoundException;

public class BubbleSort {

int[] array;

public BubbleSort(String filename) {
    Scanner scanner;

    try {
        scanner = new Scanner(new File("testOne.txt"));
    } catch (FileNotFoundException ex) {
        System.out.println("File Not Found");
        return;
    }

    while(scanner.hasNext()){
        String[] numbers = scanner.next().split(",");
         int array[] = new int[numbers.length];
         for (int i=0; i<numbers.length; i++){
             array[i] = Integer.parseInt(numbers[i]);
         }  
    }
}


public void print() {
    for(int m=0; m<array.length;m++){
        System.out.println(array[m]);
    }
}

public void sort() {
    for(int j=0; j<array.length;j++){
        if(array[j]>array[j-1]){
            int temp = array[j];
            array[j] = array[j-1];
            array[j-1] = temp;
        }
    }       
}


}

*Edit- Main class

package cse.unl;


public class Main {

public static void main(String args[]){
    BubbleSort myBubSort = new BubbleSort("tesOne.txt"); 
    myBubSort.sort();
    myBubSort.print();
}

}

Upvotes: 1

Views: 5980

Answers (5)

sanbhat
sanbhat

Reputation: 17622

try

new File("src/testOne.txt")

EDIT:

For your second problem, i see that your int[] array field of the class BubbleSort is not initialized (causing NPE)

I see that you are using a local variable in constructor (Remember, local variable have higher precedence over class fields)

    String[] numbers = scanner.next().split(",");
    (here) ---> int array[] = new int[numbers.length];

It should be

    String[] numbers = scanner.next().split(",");
    array = new int[numbers.length];

So that it initializes the class field.

Upvotes: 2

dinesh
dinesh

Reputation: 19

Please Put that in New Folder named "resources" in main project folder. And add that in the Java Build Path Source Tab.

The Give the folder path way in of text file.

File file = new File("./filename.txt");

            Scanner scanner = new Scanner(file);

            while (scanner.hasNextLine()) {
                String line = scanner.nextLine();
                System.out.println(line);
            }

This might help fil to you.

Upvotes: 0

Vishal Santharam
Vishal Santharam

Reputation: 2023

Keep one copy of file in PROJECT MAIN folder also, because while running the system calls the class file from project main folder .

Upvotes: 0

user3265132
user3265132

Reputation: 21

From the Javadoc: http://docs.oracle.com/javase/6/docs/api/java/io/File.html

A pathname, whether abstract or in string form, may be either absolute or relative. An absolute pathname is complete in that no other information is required in order to locate the file that it denotes. A relative pathname, in contrast, must be interpreted in terms of information taken from some other pathname. By default the classes in the java.io package always resolve relative pathnames against the current user directory. This directory is named by the system property user.dir, and is typically the directory in which the Java virtual machine was invoked.

You specified a relative pathname, which means, the file must be in the current user directory. Try to specify an absolute path instead.

Upvotes: 0

Juned Ahsan
Juned Ahsan

Reputation: 68715

Eclipse looks in your project root directory for files. So placing the file under project directory should make it work.

Recommendation: But for your deployment scenario you may need to chose a better path for your files

Upvotes: 0

Related Questions