How to set up the path in newBufferWriter
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("开发者_如何学GoIOException: %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
If you are getting a NoClassDefFoundError
for java.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 type java -version
?
Path
in Java 7 is more or less the replacement for File
in Java 6 and older.
You can get a Path
like this:
Path file = Paths.get("myfile.txt");
精彩评论