英文:
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<String> 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<String> content = Files.readAllLines(Paths.get("C:", "Calcs.txt"));
Otherwise there exists a method of
in the Path class since Java 11. Therefore you can write something like that:
List<String> content = Files.readAllLines(Path.of("C:", "Calcs.txt"));
答案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("\n", Files.readAllLines(Paths.get("/home/hassan", "Foo.java")));
} catch (IOException e1) {
e1.printStackTrace ();
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论