Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Java中获取绝对目录路径?_Java_File_Directory_Absolute Path - Fatal编程技术网

在Java中获取绝对目录路径?

在Java中获取绝对目录路径?,java,file,directory,absolute-path,Java,File,Directory,Absolute Path,我需要得到我的一个目录的绝对路径。因此,我单击属性以获取绝对路径,并在代码中包含: Scanner sc = new Scanner(System.in); public void Find(String path) { System.out.println("Enter in location"); path = sc.nextLine(); try { File inputFile = new

我需要得到我的一个目录的绝对路径。因此,我单击属性以获取绝对路径,并在代码中包含:

 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");
        }
    }
我试图通过用户输入获取目录的绝对路径,因此通过调用main方法中的类,通过用户输入输入目录的绝对路径,我得到:

Enter in location of file 
C:\Users\Trevor's PC\Documents\NetBeansProjects
Try again file not found
所以我的课上肯定有什么问题,或者我读它的方式有什么问题

inputFile.getAbsolutePath();
尝试此操作以获得绝对路径


你不能将目录传递给扫描器,你到底在尝试什么

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()); 
    } 

无法将目录传递给扫描仪。你到底想做什么?顺便说一句,文件夹名称加上撇号是一个人最糟糕的想法