File IO Stream
File IO Streams provide READ/WRITE access to Files. There two BYTE File IO Streams (FileInputStream and FileOutputStream) and there are two CHARACTER File IO Streams (FileReader and FileWriter).
FileInputStream
FileInputStream takes a File or File name as a Constructor parameter and provides three distinct READ api's to access the file contents.
1. read() :-
- Reads a single byte at a time.
- Blocks if no input is available.
- Returns -1 if EOF is encountered.
int eachByte; FileInputStream fileInputStream = new FileInputStream("fileinputstream.txt"); // Data Structure to contain the Bytes from the File byte[] byteContainer = new byte[fileInputStream.available()]; // Reads a single byte at a time. // Blocks if no input is available. // Returns -1 if EOF is encountered. while((eachByte = fileInputStream.read()) != -1) { System.out.println("An estimate of the remaining bytes --> "+fileInputStream.available()); byteContainer[(byteContainer.length - 1) - fileInputStream.available()] = (byte)eachByte;; } String fileContent = new String( byteContainer ); System.out.println("fileContent --> \n"+fileContent); fileInputStream.close();
2. read(byte[] b) :-
- Reads number of byte equal to the size of the array or size of the data, which ever is small.
- Blocks if no input is available.
- Returns -1 if EOF is encountered or the number of bytes read.
FileInputStream fileInputStream = new FileInputStream("fileinputstream.txt"); // Data Structure to contain the Bytes from the File byte[] byteContainer = new byte[fileInputStream.available()]; // Reads number of byte equal to the size of the array. // Blocks if no input is available. // Returns -1 if EOF is encountered or the number of bytes read. System.out.println("Number of bytes read --> "+fileInputStream.read(byteContainer)); String fileContent = new String( byteContainer ); System.out.println("fileContent --> \n"+fileContent); fileInputStream.close();
3. read(byte[] b, int offset, int length) :-
- Reads starting from the 'offset' number of bytes equal to the 'length' or size of the data, which ever is small.
- Blocks if no input is available.
- Returns -1 if EOF is encountered or the number of bytes read.
FileInputStream fileInputStream = new FileInputStream("fileinputstream.txt"); // Data Structure to contain the Bytes from the File byte[] byteContainer = new byte[fileInputStream.available()]; // Reads number of byte equal to the size of the array. // Blocks if no input is available. // Returns -1 if EOF is encountered or the number of bytes read. System.out.println("Number of bytes read --> "+fileInputStream.read(byteContainer, 5, fileInputStream.available()-5)); String fileContent = new String( byteContainer ); System.out.println("fileContent --> \n"+fileContent); fileInputStream.close();FileOutputStream
FileOutputStream takes a File or File name as a Constructor parameter and provides three distinct WRITE api's.
1. write() :-
- Writes a single byte at a time.
//Bytes to be written into the file. byte inputByte[] = "1::Hi There !!!\nHow is life".getBytes(); FileOutputStream fileOutputStream = new FileOutputStream("fileoutputstream.txt"); for(int byteCount = 0; byteCount < inputByte.length; byteCount++) { // Writes one byte at a time. fileOutputStream.write(inputByte[byteCount]); } fileOutputStream.close();2. write(byte[] b) :-
- Writes a complete byte Array at a time.
//Bytes to be written into the file. byte inputByte[] = "2::Hi There !!!\nHow is life".getBytes(); FileOutputStream fileOutputStream = new FileOutputStream("fileoutputstream.txt"); // Writes the whole byte array at once. fileOutputStream.write(inputByte); fileOutputStream.close();3. write(byte[] b, int offset, int length) :-
- Writes a byte Array starting from the 'offset' till the 'length' at a time.
//Bytes to be written into the file. byte inputByte[] = "3::Hi There !!!\nHow is life".getBytes(); FileOutputStream fileOutputStream = new FileOutputStream("fileoutputstream.txt"); // Writes partial byte array at once. fileOutputStream.write(inputByte, 5, inputByte.length-5); fileOutputStream.close();
<< Java IO Stream Overview | Filter IO Stream >> |
No comments:
Post a Comment