Greetings. I need on the basis of an existing stream of writing to a specific file (FileOutputStream outputStream) - to create a stream of reading from it. I get a file descriptor from outputStream and create a read stream

FileInputStream fileInputStream = new FileInputStream(outputStream.getFD(). 

At attempt of reading from a file (fileInputStream.read) the exception takes off: '

 Exception in thread "main" java.io.IOException: Отказано в доступе at java.io.FileInputStream.read0(Native Method) at java.io.FileInputStream.read(FileInputStream.java:207) 

When writing to this file via outputStream, there were no problems, only when reading through a descriptor. In addition, the descriptor method fd.valid () returns true. Help me to understand

  • one
    I suspect that FileOutputStream opens a stream only for writing and cannot be read from it. This is apparently determined by the system level handl. You can probably get around this with the use of temporary files or look at the RandomAccessFile side: docs.oracle.com/javase/8/docs/api/java/io/RandomAccessFile.html - m. vokhm
  • But there is a special structure, FileDescriptor, which is specifically designed for use with the FileInputStream. In the description of the 'new FileInputStream (fileDescriptor)' it is written: "Creates a fileInputStream by using the file descriptor fdObj, which represents the file system." It also said: “If you’re a security manager, you’ll find out how to get a file descriptor. - Roman Kulagin
  • How can I still get a read stream from it from an existing write stream to a file, provided that I only have a link to this stream, and I know for sure that this stream is valid - is the data written to it successfully? - Roman Kulagin

0