Snowman
Snowman

Reputation: 59

Eclipse Java get path program

I tried a program to get file name & path, but the problem is at: 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!");
    }
}

I get the following Error message:

The method get(String) is undefined for the type Path

Upvotes: 1

Views: 4900

Answers (2)

Prashant
Prashant

Reputation: 5423

Should be Paths and not Path. Paths is the class that contains get() to convert a String to Path.

Upvotes: 1

M Sach
M Sach

Reputation: 34424

You are looking at wrong class i.e Path instead of Paths. Look at Paths Java doc which has get method

Upvotes: 1

Related Questions