我沿着基本I/O教程Oracle.com下面創建Path對象,但我有困難作出Path
對象:無法從字符串
Path p1 = Paths.get("/tmp/foo");
這給錯誤:
error: The method get(URI) in the type Paths is not applicable for the arguments (String).
我在Linux上,我在Eclipse Kepler工作。我正試圖訪問當前目錄中的文本文件。使用Scanner
和File
我可以使用該文件,但我也想擺弄該文件的路徑,以便繼續教程。
編輯:整個程序如下。下半場我是一名新秀,並確認文件存在/作品。當我註釋掉路徑定義,我得到的「測試」,這是在「save.txt的」文件:
package projectSARA;
import java.util.*;
import java.io.*;
import java.net.URI;
import java.nio.file.Path;
import java.nio.file.Paths;
public class Main {
public static void main(String[] args) {
String saveFile = "save.txt";
Path p1 = Paths.get(saveFile);
Path p2 = Paths.get("save.txt");
File file = new File(saveFile);
try{
Scanner in = new Scanner(file);
String test = in.next();
System.out.println(test);
}
catch(FileNotFoundException e){
System.out.println("File not found");
}
}// end main
}
你代碼看起來有效,它不應該導致編譯器錯誤。請複製/粘貼一個完整的例子到你的問題中(包括'import'語句),這會產生這個編譯器錯誤。 –