-2

I have an error in this code:

Source:

        System.out.println("Load");
        Path path = Paths.get("BillboardHot100.csv");
        Path textpath = path;
        user1.User_Playlist.load(textpath);

        user1.User_Playlist.shuffle(40);

        Stream.Users.add(user1);

        Stream.userList();

Attempt:

public void load(Path textpath){
    if (textpath != null){
        try {

            File playlistFile = new File(textpath);

            Scanner fileScanner = new Scanner(playlistFile);

            System.out.println("Processing playlist file " + playlistFile + ":");

I get an error on: File playlistFile = new File(textpath);

The error says: The constructor File(Path) is undefined

I need help fixing this error

user
  • 7,435
  • 3
  • 14
  • 44
cscscs
  • 55
  • 7
  • 5
    Have you tried calling `textpath.toFile()` instead? – Jon Skeet May 06 '20 at 18:14
  • 1
    Are you referring to class [`java.io.File`](https://docs.oracle.com/javase/8/docs/api/java/io/File.html) and interface [`java.nio.file.Path`](https://docs.oracle.com/javase/8/docs/api/java/nio/file/Path.html) ? – Abra May 06 '20 at 18:17
  • yes I am referring to that – cscscs May 06 '20 at 18:35
  • Be careful about converting a `Path` to a `File`. It will, by default, only work for `Path` instances belonging to the default `FileSystem`. That's not necessarily always going to be the case as there are other implementations out there (e.g. zip, jrt, jimfs, etc.). Besides, `Scanner` has a constructor which accepts a `Path`—just use that. – Slaw May 06 '20 at 19:31

1 Answers1

1

java.io.File does not have Path type constructor. You can convert textpath in to String.

File playlistFile = new File(textpath.toString);

Or you can also use Scanner scan = new Scanner(textpath);

Rahul Gupta
  • 504
  • 4
  • 17
  • 1
    The `Path#getParent()` method returns `Path`. This will lead to the exact same problem the OP is currently having. If you want to use a `File` constructor then use `new File(nioPath.toString())` or, preferably, use `Path#toFile()` as described in the duplicate. – Slaw May 06 '20 at 19:27