Eclipse Java获取路径程序
我尝试了一个程序来获取文件名和路径,但问题出在:Eclipse Java获取路径程序,java,eclipse,path,Java,Eclipse,Path,我尝试了一个程序来获取文件名和路径,但问题出在:dPath=path.get(path) 我收到以下错误消息: 类型路径的方法get(String)未定义 您看到的是错误的类,即路径而不是路径。看看哪个get方法应该是Path而不是Path。Paths是包含get()的类,用于将字符串转换为路径。是的,然后我遇到另一个问题:位于java.nio.file.Files.readAttributes(未知源)能否粘贴堆栈跟踪?否则很难找出根本原因。最佳猜测:文件不存在路径类实际上包含get方法。。N
dPath=path.get(path)代码>
我收到以下错误消息:
类型路径的方法get(String)未定义
您看到的是错误的类,即路径而不是路径。看看哪个get方法应该是Path而不是Path。Paths是包含get()的类,用于将字符串转换为路径。是的,然后我遇到另一个问题:位于java.nio.file.Files.readAttributes(未知源)
能否粘贴堆栈跟踪?否则很难找出根本原因。最佳猜测:文件不存在路径
类实际上包含get
方法。。NotPath
这不是一个可以更改问题以使现有(和已接受)答案无效的论坛。如果你有一个新问题,那么创建一个新问题
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!");
}
}