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?

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.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.