如何使用 Paths.of 解决这个错误?

huangapple go评论89阅读模式
英文:

How do I resolve this error using Paths.of?

问题

我尝试了许多提供的示例,但都没有成功。以下是我目前尝试的代码,在Eclipse中出现了错误,指出Paths.of(of的部分被红色下划线标出)处需要重命名。

String content;

try {
    content = Files.readAllLines(Paths.of("C:", "Calcs.txt"));
} catch (IOException e1) {
    e1.printStackTrace();
}

System.out.println(content);
英文:

I have been trying many of the examples provided and have yet to be successful. Here is the code I am currently trying, but getting an error in Eclipse on Paths.of (the of is underlined in red) that says: "rename in file".

String content; 

try {
    content = Files.readAllLines(Paths.of("C:", "Calcs.txt"));
} catch (IOException e1) {
    e1.printStackTrace ();
}
				
System.out.println (content);

答案1

得分: 1

首先,如果返回类型是列表,是不可能将其赋值给字符串的。因此你必须这样写:

List<String> content;

其次,根据Java 8文档,该类中没有可用的of方法。你可以像这样使用get方法:

List<String> content = Files.readAllLines(Paths.get("C:", "Calcs.txt"));

另外,自Java 11起,Path类中存在一个method of。因此,你可以这样写:

List<String> content = Files.readAllLines(Path.of("C:", "Calcs.txt"));
英文:

First it is not possible, if you get a list as return type, to assign this to a string. So you must write:

List&lt;String&gt; content;

Second regarding to the Java 8 documentation there is no method of available for this class. You can use the method get like this:

List&lt;String&gt; content = Files.readAllLines(Paths.get(&quot;C:&quot;, &quot;Calcs.txt&quot;));

Otherwise there exists a method of in the Path class since Java 11. Therefore you can write something like that:

List&lt;String&gt; content = Files.readAllLines(Path.of(&quot;C:&quot;, &quot;Calcs.txt&quot;));

答案2

得分: 0

你可能正在寻找 Paths.get 方法:

String content; 
try {
    content = String.join("\n", Files.readAllLines(Paths.get("/home/hassan", "Foo.java")));

} catch (IOException e1) {
    e1.printStackTrace();
}
英文:

You're probably looking for Paths.get:

String content; 
try {
    content = String.join(&quot;\n&quot;, Files.readAllLines(Paths.get(&quot;/home/hassan&quot;, &quot;Foo.java&quot;)));

} catch (IOException e1) {
    e1.printStackTrace ();
}

huangapple
  • 本文由 发表于 2020年10月4日 03:06:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/64187979.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定