-3

I need to get the absolute path of one of my directories. So I clicked on properties to grab that absolute path and had in my code:

 Scanner sc = new Scanner(System.in);

 public void Find(String path) {
        System.out.println("Enter in location");

        path = sc.nextLine();
        try 
        {
            File inputFile = new File(path);
            Scanner reader = new Scanner(inputFile);
        }
        catch(FileNotFoundException e)
        {
            System.out.println("Try again file not found");
        }
    }

I'm trying to get the absolute path of the directory through user input, so I enter in the absolute path of the directory through user input by calling the class in the main method and I get:

Enter in location of file 
C:\Users\Trevor's PC\Documents\NetBeansProjects
Try again file not found

So something must be off with my class or the way I'm trying to read it in?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
W.Smith
  • 91
  • 7

1 Answers1

1
inputFile.getAbsolutePath();

Try this to get the absolute path

You can't pass a directory to Scanner and what exactly you trying.

File f = new File(fname); 
 //apply File class methods on File object 
    System.out.println("File name :"+f.getName()); 
    System.out.println("Path: "+f.getPath()); 
    System.out.println("Absolute path:" +f.getAbsolutePath()); 
    System.out.println("Parent:"+f.getParent()); 
    System.out.println("Exists :"+f.exists()); 
    if(f.exists()) 
    { 
        System.out.println("Is writeable:"+f.canWrite()); 
        System.out.println("Is readable"+f.canRead()); 
        System.out.println("Is a directory:"+f.isDirectory()); 
        System.out.println("File Size in bytes "+f.length()); 
    }