无法从字符串创建Path对象 [英] Unable to create a Path object from a String

查看:144
本文介绍了无法从字符串创建Path对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在遵循Oracle.com上的基本I/O教程",但是在制作Path对象方面遇到困难:

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");

哪个出现错误:

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

我在Linux上,并且在Eclipse Kepler中工作.我正在尝试访问当前目录中的文本文件.使用扫描仪和文件,我可以使用文件,但是我也想弄乱文件的路径,以便继续本教程.

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:整个程序如下.第二部分是我是一名菜鸟,并确认文件存在/正常工作.当我注释掉路径定义时,我得到"Test"的输出,该输出位于"save.txt"文件中.:

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

}

其他:我之前曾问过这个问题,并被要求进行编辑.我添加了修改内容,等待了几天,但没有更多回复.我应该以某种方式对其进行刷新,还是与评论的作者联系?我是Stack Exchange的新手,但是非常感谢大家的努力.

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.

推荐答案

Paths.get("/some/path")是有效的方法调用.如果代码是您发布的代码,则可能是与Eclipse意图有关的错误.您的错误The method get(URI) in the type Paths...不是javac错误,而是Eclipse错误.

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.

您的类非常简单,因此请尝试使用javac或除Eclipse之外的其他IDE对其进行编译,看看是否出现编译错误.

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.

这篇关于无法从字符串创建Path对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆