如何设置newBufferWriter中的路径
我如何设置到 newBufferWriter
的路径。我从 oracle 页面获取 newBufferWriter
的示例用法:
Charset charset = Charset.forName("US-ASCII");
String s = ...;
try (BufferedWriter writer = Files.newBufferedWriter(file, charset)) {
writer.write(s, 0, s.length());
} catch (IOException x) {
System.err.format("IOException: %s%n", x);
}
我很困惑如何设置 file
参数,我应该在哪里获取 Path 对象,例如我想在目录中创建文件,所以我必须设置一个
Path
对象,在这段代码中,路径对象是 file
参数,所以,如何给它一个字符串值?或者如何赋予它我想要创建某个文件的目录的任何值?
还有其他的事情,那个异常怎么样?这是什么意思 ?
线程“AWT-EventQueue-0”中出现异常 java.lang.NoClassDefFoundError: java/nio/file/Path
How can i set up the path right to newBufferWriter
. I'm getting the example usage of the newBufferWriter
from oracle page:
Charset charset = Charset.forName("US-ASCII");
String s = ...;
try (BufferedWriter writer = Files.newBufferedWriter(file, charset)) {
writer.write(s, 0, s.length());
} catch (IOException x) {
System.err.format("IOException: %s%n", x);
}
I'm comfused how to set the file
parameter, Where should i get the Path
object, for example i want to create file in a directory , so i have to set a Path
object, and in this code the path object is file
parameter so , how to give it a string value ? or how to give it any value of a directory where i want to create a certain file ?
And something else, what about that exception ? What does it mean ?
Exception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError: java/nio/file/Path
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您的
java.nio.file.Path
出现NoClassDefFoundError
,那么您的 Java 环境有问题。您很可能混合使用 Java 版本;使用 JDK 7 进行编译,但尝试在 Java 6 或更早版本上运行。当你输入java -version
时你会得到什么?Java 7 中的
Path
或多或少是 Java 6 及更早版本中的File
的替代品。您可以获得像这样的
Path
:If you are getting a
NoClassDefFoundError
forjava.nio.file.Path
then there is something wrong with your Java environment. You are most likely mixing Java versions; compiling with JDK 7, but trying to run on Java 6 or older. What do you get when you typejava -version
?Path
in Java 7 is more or less the replacement forFile
in Java 6 and older.You can get a
Path
like this: