2016-02-22 49 views
1

我試過一個程序來獲取文件名&路徑,但問題是:dPath = Path.get(path);的Eclipse的Java獲取路徑程序

import java.io.IOException; 
import java.nio.file.*; 
import java.nio.file.attribute.*; 
import java.io.*; 


public class Ch9_4_4 { 

    public static void main(String[] args) throws IOException { 
     String file = "Ch9_4_3.java"; 
     String path = "temp2"; 
     Path fPath, dPath; 
     fPath = FileSystems.getDefault().getPath(".", file); 
     dPath = Path.get(path); 
     System.out.println(fPath.getFileName()); 
     System.out.println("temp2 is absolute path: "+dPath.isAbsolute()); 

     BasicFileAttributes attr = Files.readAttributes(fPath, BasicFileAttributes.class); 
     if (Files.exists(fPath)) { 
      System.out.println("Directory: " + attr.isDirectory()); 
      System.out.println("File: " + attr.isRegularFile()); 
      System.out.println("Create date: " + attr.creationTime()); 
      System.out.println("Size: " + attr.size()); 
     } 
     else System.out.println("[" + fPath + "] does not exist!"); 

     Files.createDirectory(dPath); 
     System.out.println("[" + dPath + "] directory is created!"); 
    } 
} 

我收到以下錯誤消息:

GET方法(字符串)未定義爲類型路徑

+1

'Paths'類包含'GET'方法其實..不'Path' –

+0

這不是一個論壇,在這裏你可以改變的問題無效現有的(和接受)的答案。如果你有一個新的問題,然後創建一個新的問題 – Tom

回答

1

應該是路徑,而不是路徑。路徑是包含get()將String轉換爲Path的類。

+0

是的,然後我得到了另一個問題: '在java.nio.file.Files.readAttributes(來源不明)' – Snowman

+0

能否請您粘貼堆棧跟蹤?否則,很難找出根本原因。最佳猜測:文件不存在 – Prashant

1

您正在尋找錯誤的類,即路徑而不是路徑。看看Paths Java doc其中有get方法