我需要获取我的目录之一的绝对路径。因此,我单击属性以获取该绝对路径,并在我的代码中输入:
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
所以我的课或我尝试阅读它的方式一定有问题吗?
答案 0 :(得分:1)
inputFile.getAbsolutePath();
尝试此操作以获取绝对路径
您无法将目录传递给Scanner,也不能尝试将其传递给我们。
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());
}