0

I am a beginner in java. I wrote the following code in netbeans.

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sanfile;

import java.io.*;
/**
 *
 * @author santak
 */
public class SanFile {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        File f = new File("doc.dat");
        
        boolean b;
        b = f.exists();
        if(b == true)
            System.out.println("File exists.");
        else
            System.out.println("File doesn't exist.");
        
        // TODO code application logic here
    }
}

the file doc.dat and SanFile.java are in the same directory but still the output is File doesn't exist. what may be the reasons?

2
Contributors
2
Replies
3
Views
5 Years
Discussion Span
Last Post by santakdalai90
0

Hi santakdalai90,

Please be reminded, on netbeans, classes are stored in "[dirpath]/packagename/src" directory which might be also the directory where you have stored "doc.dat". While on your program, you are trying to look on a file with the path "[dirpath]/packagename" (notice that there is no "src" directory on it).

So, to solve your problem, make sure "doc.dat" file exist on "[dirpath]/packagename" or change your code from File f = new File("doc.dat"); to File f = new File("src/doc.dat"); I hope that would help. Let me know if you are still getting any problem after you did that.

Edited by jieryn: n/a

0

Hi santakdalai90,

Please be reminded, on netbeans, classes are stored in "[dirpath]/packagename/src" directory which might be also the directory where you have stored "doc.dat". While on your program, you are trying to look on a file with the path "[dirpath]/packagename" (notice that there is no "src" directory on it).

So, to solve your problem, make sure "doc.dat" file exist on "[dirpath]/packagename" or change your code from File f = new File("doc.dat"); to File f = new File("src/doc.dat"); I hope that would help. Let me know if you are still getting any problem after you did that.

Thanks for your reply it was really searching in [dirpath]/src/ folder. I made the following replacement: File f = new File("src/sanfile/doc.dat"); and it worked fine.
Thanks again.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.