Kalec
Kalec

Reputation: 2881

File not found exception that does not make sense (For me)

I have a program that reads and writes from a file. I EVEN tried to create the file in main but it still doesn't work.

public static void main(String[] args) throws NumberFormatException, 
                                              IOException, 
                                              FileNotFoundException {
    System.out.println("Work in progress");

    File f = new File("Data.txt");
    System.out.println(f.getAbsolutePath());
            // Yes, it's there.


    UI ui = new UI();
    GenericDictionary<Integer> gd = new GenericDictionary<Integer>();
    Repository repo = new Repository("Data.txt", gd);
            // Should work, right ?

Now my Repository:

public Repository (String fileName, GenericDictionary gd) 
                                               throws IOException, 
                                                      NumberFormatException, 
                                                      FileNotFoundException {
    this.fileName = fileName;
    this.gd = gd;


    FileReader input = null;
    BufferedReader  inputBuffer = null;

    try {
        input = new FileReader(this.fileName);
        inputBuffer = new BufferedReader (input);

        String line;
        while ((line = inputBuffer.readLine()) != null) {
            String[] inputData = line.split(",");

            Node<Integer> newNode = new Node<Integer> (
                                       Integer.parseInt(inputData[0]),
                                       Integer.parseInt(inputData[1]));
            this.gd.add(newNode);
        }



    }catch (NumberFormatException nfe){
        System.out.println(
               "Repository could not load data due to NumberFormatException: "
               + nfe); 
    }catch (FileNotFoundException fnfe) {
        System.out.println("File not found, error: " + fnfe);
    }finally {
        inputBuffer.close();
        input.close();
    }
}

Now even though I create my file it does not want to use it. Initially it was in the constructor of my repository, I moved it into the main file, still no success.

This is what Eclipse prints within console:

Upvotes: 0

Views: 1696

Answers (4)

codeMan
codeMan

Reputation: 5758

I hope this works :

Replace this line

Repository repo = new Repository("Data.txt", gd);

with :

Repository repo = new Repository(f, gd);

and in your

public Repository (String fileName, GenericDictionary gd) 
                                           throws IOException, 
                                                  NumberFormatException, 
                                                  FileNotFoundException

use this

public Repository (File f, GenericDictionary gd) 
                                           throws IOException, 
                                                  NumberFormatException, 
                                                  FileNotFoundException

and in try { } instead of

input = new FileReader(this.fileName);

do this

input = new FileReader(f.getAbsolutePath());

Upvotes: 1

Robin
Robin

Reputation: 3840

As others Stated you don't create a file, try touch() method from here: ApacheFileUtils

Upvotes: 1

Bohemian
Bohemian

Reputation: 424973

A File is an abstract path. Executing this:

File f = new File("Data.txt");

Does absolutley nothing on disk. Nor is this

System.out.println(f.getAbsolutePath());

Any test for existence of the file.

Do this:

if(file.exists()) {
    // yes it's there
}

Upvotes: 2

Jon Skeet
Jon Skeet

Reputation: 1499770

This doesn't do what you think it does:

File f = new File("Data.txt");
System.out.println(f.getAbsolutePath());
        // Yes, it's there.

That doesn't create a file on disk. It just creates a File object representing a path name. If you use:

System.out.println(f.exists());

that will show you whether or not it really exists.

So unless D:\Info\Java workspace\Laborator_4\Data.txt really, really exists, it's entirely reasonable for you to get an exception. Create the file and try again.

Additionally, you're getting a NullPointerException in your finally block because you're assuming that inputBuffer and input are both non-null: don't make that assumption. Check before closing.

Upvotes: 4

Related Questions