Search code examples
javanetbeansapplet

File default location


Why the program search the file:

    File FILE_PATH = new File("‪‪C:\\Users\\home\\Desktop\\DbWord.txt");
    System.out.println(FILE_PATH.exists());
    System.out.println(FILE_PATH.getAbsoluteFile());
    FileInputStream fIn = new FileInputStream(FILE_PATH);
    Scanner reader = new Scanner(fIn);

at: C:\Users\home\Documents\NetBeansProjects\MyDatabase\‪‪C:\Users\home\Desktop\DbWord.txt

How can i counteract the default location?

If something in this post not good, please tell me and no negative vote.

Thanks!

why negative votes??????????????? whats your problems??????????


Solution

  • Please double check your error details. You might have seen something like the below error. Actually the program is not searching for the file "C:\Users\home\Documents\NetBeansProjects\MyDatabase\‪‪C:\Users\home\Desktop\DbWord.txt", it is trying to locate the file "C:\Users\home\Desktop\DbWord.txt" which does not exists in your machine. You are seeing "C:\Users\home\Documents\NetBeansProjects\MyDatabase\‪‪C:\Users\home\Desktop\DbWord.txt" along with the error because you have already used System.out.println(FILE_PATH.getAbsoluteFile()); statement in your code.

    false
    Exception in thread "main" java.io.FileNotFoundException: ‪‪C:\Users\home\Desktop\DbWord.txt (The filename, directory name, or volume label syntax is incorrect)
        at java.io.FileInputStream.open0(Native Method)
        at java.io.FileInputStream.open(FileInputStream.java:195)
        at java.io.FileInputStream.<init>(FileInputStream.java:138)
    C:\Users\home\Documents\NetBeansProjects\MyDatabase\‪‪C:\Users\home\Desktop\DbWord.txt
        at com.stackoverflow.answer.SimpleFileHelper.main(SimpleFileHelper.java:17)
    

    Hope you are clear now.

    There are three main chances where a FileNotFoundException may be thrown.

    1. The named file does not exist.
    2. The named file is actually a directory not file.
    3. The named file cannot be opened for reading due to some reason.

    The first two reasons are unlikely based on your description, please check the third point using file.canRead() method.

    If the test above returns true, I would suspect the following:

    You might have forgotten to explicitly throw or catch the potential exception (i.e., FileNotFoundExcetion). If you work in an IDE, you should have got some complaint from the compiler. But I suspect you didn't run your code in such an IDE.

    Try the following code and see if the exception would be gone:

    package com.stackoverflow.answer;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.util.Scanner;
    
    public class SimpleFileHelper {
    
        public static void main(String[] args) throws FileNotFoundException {
            File FILE_PATH = new File("C:/Users/home/Desktop/DbWord.txt");
            System.out.println(FILE_PATH.exists());
            System.out.println(FILE_PATH.getAbsoluteFile());
            FileInputStream fIn = new FileInputStream(FILE_PATH);
            Scanner reader = new Scanner(fIn);
        }
    }