
int write(byte b) − This method accepts a byte array as parameter and writes data from it to the current OutputStream.
int write(int b) − This method accepts a single byte and writes it to the current OutputStream. Let’s convert a File to an InputStream in numerous ways of implementation. Let’s look at some of the Java InputStream examples in this tutorial. Then write the data to a specified file using either of the variants of write() method − In this article, we have discussed how to convert a java file to an InputStream.There are 5 very easy ways to convert a java file to InputStream by Using Traditional java IO Package, using Java 8, Using Apache Common IO Library, Using Guava Library. FileDescriptor descriptor = new FileDescriptor() įileOutputStream outputStream = new FileOutputStream(descriptor) You can also instantiate a FileOutputStream class by passing a FileDescriptor object. To write the contents of a file using this class −įileOutputStream outputStream = new FileOutputStream("file_path") įileOutputStream outputStream = new FileOutputStream (file) It is usually used to write the contents of a file with raw bytes, such as images. This writes data into a specific file or, file descriptor (byte by byte). Public static void main(String args) throws IOException Output Data copied successfully. Int read(byte b, int off, int len) − This method accepts a byte array, its offset (int) and, its length (int) as parameters and reads the contents of the current InputStream, to the given array.Īssume we have the following image in the directory D:/imagesįollowing program reads contents of the above image using the FileInputStream. This method returns an integer representing the total number of bytes or, -1 if the end of the file is reached.
Int read(byte b) − This method accepts a byte array as parameter and reads the contents of the current InputStream, to the given array This method returns -1 if the end of the file is reached. Int read() − This simply reads data from the current InputStream and returns the read data byte by byte (in integer format). Then read the contents of the specified file using either of the variants of read() method −.First of all, you need to instantiate this class by passing a String variable or a File object, representing the path of the file to be read.įileInputStream inputStream = new FileInputStream("file_path") įileInputStream inputStream = new FileInputStream(file).To read the contents of a file using this class − It is usually used to read the contents of a file with raw bytes, such as images. This class reads the data from a specific file (byte by byte). OutputStream − This is used to write data to a destination.InputStream − This is used to read (sequential) data from a source.There are two types of streams available − Java provides I/O Streams to read and write data where, a Stream represents an input source or an output destination which could be a file, i/o devise, other program etc.