Unable to create a Path object from a String

风流意气都作罢 提交于 2019-12-25 02:53:45

问题


I'm following along the Basic I/O Tutorial on Oracle.com, but I'm having difficulty making a Path object:

Path p1 = Paths.get("/tmp/foo");

Which gives the error:

error: The method get(URI) in the type Paths is not applicable for the arguments (String).

I'm on Linux and I'm working in Eclipse Kepler. I'm trying to access a text file in the current directory. Using Scanner and File I can work with the file, but I'd also like to fiddle around with a path to the file so I can continue with the tutorial.

edit: The entirety of the program is below. The second half is me being a rookie and confirming the file exists/works. When I comment out the Path definitions, I get the output of "Test" which is in the 'save.txt' file.:

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

}

Additional: I've asked this question before, and was asked for edits. I added in the edits, waited a few days, but no more responses. Am I supposed to refresh it somehow, or contact the authors of the comments? I'm very new to Stack Exchange, but appreciate everyone's efforts immensely.


回答1:


Paths.get("/some/path") is a valid method call. If the code is as you posted it, this may be a bug with Eclipse intentions. Your error The method get(URI) in the type Paths... is not a javac error, it's an Eclipse error.

Your class is fairly simple, so try compiling it with javac or using some other IDE besides Eclipse and see if you get a compilation error.




回答2:


In your line:

Path p1 = Paths.get("/tmp/foo");

You are using a string while the get method is expecting an URI.

Just define your path as an URI first.

URI uri = null;
try {
    uri = new URI("file:///myFile.txt");  
             }
Path file=Paths.get(uri);



回答3:


Path p1 = Paths.get("/tmp/foo");

where Paths.get accepts only URI arguments so we need to provide like

Path dir = Paths.get(new URI("/tmp/foo"));

I guess, it will solve your issue.




回答4:


Your code should work, but here's a simple workaround:

Paths.get("save.txt", "");

This will force the system to use the string-based get method instead of the URI.




回答5:


Changing the JRE to java 7 will solve this. On Eclipse go to project properties -> build path -> libraries -> select the JRE system library -> click edit -> change execution environment to jdk 7. Now you can pass String into Paths.get().



来源:https://stackoverflow.com/questions/22740086/unable-to-create-a-path-object-from-a-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!