Java I/O-Java.nio.file.NoSuchFileException在尝试打开已写入的文件时引发

Java I/O-Java.nio.file.NoSuchFileException在尝试打开已写入的文件时引发,java,linux,io,Java,Linux,Io,我在java程序中遇到了I/O问题。我的程序在Linux下使用java 7和Tomcat 7。线程1创建一个对象并用JAXB将其序列化为文件: public static void export1(JAXBContext context, Object object, Path filePath){ try { Marshaller marshaller = context.createMarshaller(); marshaller.setPropert

我在java程序中遇到了I/O问题。我的程序在Linux下使用java 7和Tomcat 7。线程1创建一个对象并用JAXB将其序列化为文件:

public static void export1(JAXBContext context, Object object, Path filePath){
    try {
        Marshaller marshaller = context.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
        marshaller.marshal(object, filePath.toFile());
    } catch (JAXBException e) {
        throw new RuntimeException(e);
    }
}
然后向外部实体发送通知。处理传入请求的线程会遇到:

Caused by: java.nio.file.NoSuchFileException: PATH_TO_FILE
    at sun.nio.fs.UnixException.translateToIOException(Unknown Source)
    at sun.nio.fs.UnixException.rethrowAsIOException(Unknown Source)
    at sun.nio.fs.UnixException.rethrowAsIOException(Unknown Source)
    at sun.nio.fs.UnixFileSystemProvider.newByteChannel(Unknown Source)
    at java.nio.file.Files.newByteChannel(Unknown Source)
    at java.nio.file.Files.newByteChannel(Unknown Source)
我读过一些关于的帖子,但我仍然感到困惑。将我的代码重构到下一个使用无缓冲输出流的代码可以解决我的问题吗

public static void export2(JAXBContext context, Object object, Path filePath){
    try (OutputStream os = Files.newOutputStream(filePath)) {
        Marshaller marshaller = context.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
        marshaller.marshal(object, os);
    } catch (JAXBException | IOException e) {
        throw new RuntimeException(e);
    }
}
或者我应该直接使用文件描述符将操作系统缓冲区与底层设备同步

public static void export3(JAXBContext context, Object object, Path filePath) {
    try (FileOutputStream fos = new FileOutputStream(filePath.toFile())) {
        Marshaller marshaller = context.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
        marshaller.marshal(object, fos);
        fos.getFD().sync();
    } catch (JAXBException | IOException e) {
        throw new RuntimeException(e);
    }
}

你尝过吗?您的第二个示例应该可以正常工作。问题发生在我的系统负载很重的情况下。现在还没有用新代码复制它。