下面是摘自这里的一段代码:
import static java.nio.file.StandardOpenOption.*;
Path logfile = ...;
// Convert the string to a
// byte array.
String s = ...;
byte data[] = s.getBytes();
try (OutputStream out = new BufferedOutputStream(
logfile.newOutputStream(CREATE, APPEND))) {
...
out.write(data, 0, data.length);
} catch (IOException x) {
System.err.println(x);
}
但是,我无法使用newOutputStream方法编译日志文件(这是一个路径对象)...仅Files.newOutputStream(path,StandardOpenOption..);
发布于 2013-06-16 20:29:25
Path仅包含有关文件(或其他内容)所在位置的信息,不提供有关如何处理该文件的任何信息。因为您知道路径是一个文件,所以您可以使用file类来处理它,在本例中是在它上面打开一个流。
在语言方面,Path没有newOutputStream方法,因此它将无法编译。
来自Path上的Oracle文档
路径可以与files类一起使用,以对文件、目录和其他类型的文件进行操作。
https://stackoverflow.com/questions/17133304
复制相似问题