In Java, a stream is a sequence of data composed of bytes. It’s synonymous to a stream because like a stream of water, it continues to flow. Based on the datatype that is transferred through the stream, they are classified as one of the following based on the byte:
Output streamIn order to write data to a destination, the Java application utilizes an output streammay be a peripheral device, socket, file, or an array.
The OutputStream class is an abstract class. It is the super-class of all classes denoting an output-stream of bytes. An output-stream takes in output bytes and sends them to some reservoir. Important methods of OutputStream
Java FileOutputStream is a type of output stream used for writing data to a destination file. When it is necessary to write primitive values into a file, use the FileOutputStream class. You can write byte-type as well as character-type data via FileOutputStream class, but FileWriter is preferred to FileOutputStream for character-type data. FileOutputStream class declarationThe Java.io.FileOutputStream class is declared as: public class FileOutputStream extends OutputStream. FileOutputStream class methods
import java.io.FileOutputStream; public class FileOutputStreamExample { public static void main(String args[]){ try{ FileOutputStream fileout=new FileOutputStream("D:\\testout.txt"); fout.write(65); fileout.close(); System.out.println("success..."); }catch(Exception e){System.out.println(e);} } } Output Java FileOutputStream Example 1
The InputStream class is an abstract class. It is the parent-class of all classes that represents an input stream of bytes. Important methods of InputStream
The Java FileInputStream class acquires input bytes data from a file. It reads byte-associated dataraw bytes stream, such as image data, audio, video, etc., and can read character-stream data. Nonetheless, in order to read streams of characters, the FileReader class is recommended. Java FileInputStream class declarationThe Java.io.FileInputStream class is declared as: public class FileInputStream extends InputStream. Java FileInputStream class methods
import java.io.FileInputStream; public class DataStreamExample { public static void main(String args[]){ try{ FileInputStream fileinp=new FileInputStream("D:\\testout.txt"); int i=fileinp.read(); System.out.print((char)i); fin.close(); }catch(Exception e){System.out.println(e);} } } Java FileInputStream example 1: to read a single character I hope the content of this shot, despite its brevity, answers some of the burning questions you had before reading through it. RELATED TAGS
Submit a bug or feature |