In Java Read operation can be performed using FileIntPutStream and Filereder. For writing streams of character, Filereader is preferred and FileIntputStream is used for binary data like images.
FileReader - Reading streams of characters
FileInputStream - Reading streams of raw bytes
FileReader - Reading streams of characters
FileInputStream - Reading streams of raw bytes
Read from a file using FileInputStream :-
FileInputStream wrapped in BufferedInputStream increases efficiency of read operation.When an instance of FileInputStream(file) is created, a file descriptor object is created internally.File descriptor acts as an handle to the underlying machine-specific structure representing an open file. i.e: File descriptor binds stream with the file being used for read operation. Below sample code shows how FileInputStream wrapped with BufferedInputStream can be used for read operation. Complete sample Java program for read and write operation is here.
// With buffered input:- Read buffered input file and write to output file |
the value -1 is returned.( Add System.out.println(byteCoint) inside while loop and right after while loop and verify that integer between 0 to 255 and -1 is be printed on console.)
Read from a file using FileReader :-
For character streams, FileReader is used and it extends InputStreamReader(a bridge from byte streams to character streams).InputStreamReader reads streams and convert it into character(s) using character set converter. On each invocation of read operation, byte(s) read is converted into characters So,efficiency of this read operation and conversion can be improved by buffering the InputStream using BufferedWriter.Note :- Since FileReader internally relies on InputStreamRedaer encoding and byte buffer. So, constructor of class FileReader assume that the default character encoding and the default byte-buffer size are appropriate.
Below sample code shows how FileReader wrapped with BufferedReader. Complete sample Java program for read and write operation is here.
// With buffered input:- Read input text file and write to output file
|
Tags:
Java Input Output
Aivivu đại lý vé máy bay, tham khảo
ReplyDeleteve may bay di my gia re
đại lý vé máy bay tại quy nhơn
vé máy bay từ hà nội về sài gòn
giá vé máy bay pacific airlines sài gòn hà nội
mua vé máy bay từ mỹ về vn
taxi sân bay 2 chiều
combo tour nha trang