繁体   English   中英

Eclipse Java获取路径程序

[英]Eclipse Java get path program

我尝试了一个程序来获取文件名和路径,但问题出在: dPath = Path.get(path);

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(String)

您正在查看错误的类,即Path而不是Paths。 查看具有get方法的Paths Java文档

应该是路径而不是路径。 路径是包含将字符串转换为路径的get()的类。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM