java2db

How to read file using FileInputStream in java



The java.io package provides service to do all file input and output operations.See the below example to read file using FileInputStream class.

read file using FileInputStream Read file using FileInputStream read() method  :

read file using FileInputStream Create a file (Example : read.txt) in any valid location (Example: C:\\Java2dbExamples\\read.txt)   ….add some text to it.

read file using FileInputStream Specify the location of the (read.txt) file in the File class constructor.

read file using FileInputStream Read the  file data using  (FileInputStream)  read() method.
read file using FileInputStream Finally close the file.

 
Note : While doing this , it throws an IOException. So handle it by throws IOException or using try,catch block.

read file using FileInputStream Read file using FileInputStream read(byte[] b method  :

read file in java using FileInputStream The read(byte[] b)  method returns a byte array .

read file in java using FileInputStream b : It (the byte array) reads the file data according to its size(in our example it is 2500).

 

read file in java using FileInputStream Read file using FileInputStream read(byte[] b, int off, int len)  method  :

read file in java using FileInputStream This  method also returns a byte array .

read file in java using FileInputStream b : It (the byte array) reads the file data according to its size(In our example it is 2500).

read file in java using FileInputStream off : the start offset (In the below example the the input stream will store after 10 spaces) in the ….destination array b.

read file in java using FileInputStream len : Read specified number of bytes(In the example code ,the byte array reads 2400 bytes only).

read.txt




Related Posts :
   [will not be published]



^ <