Java I/O
Java I/O (Input and Output) is used to process the input and produce the output.Java uses the concept of a stream to make I/O operation fast.
The java.io package contains all the sessions required for feedback and outcome functions.
Stream
A stream is a sequence of data.A stream is an abstract representation of an input and output device used to read and write data.Stream differentiate in two way in java.
1 Text stream: Its containing ‘characters’
2 Binary stream:- Its containing 8-bit information’01101001’
In java, these streams are created automatically.Therse Streams are attached with the console.
1)System.in: standard input stream
2) System. Out: standard output stream
3) System. Err: standard error stream
System.in: This is the standard input stream that is used to read characters from the keyboard or any other standard input device.
example:- Scanner scan = new Scanner(System.in);
System. Out: This is the standard output stream that is used to produce the result of a program on an output device like the computer screen.
example:- System.out.print(" First Name: ");
System.out.println("First Name:");
System. Err: This is the standard output stream that is used to output all the error data a program might throw, on a computer screen or any standard output device.
InputStream
Java program uses a port circulation to read information from a source, it may be
knowledge file,an range,peripheral system or outlet.
OutputStream
Java program uses an outcome circulation to write information to a location, it may be a knowledge file,an range,peripheral system or outlet.
Java File Input stream
This stream is used for reading data from the files. Objects can be created using the keyword new.
Example:- FileInputStreamfin = newFileInputStream("D:\\abc.txt");Methods
Methods | Description |
read() | Used for reading bytes from a file |
available() | It is used for getting the number of available bytes that can be used. |
skip() | Used to skip or discard a specified number of bytes. |
close() | It is used to close the file input stream. |
finalize() | It ensures that the close method is called. |
getFD() | It is used to return the description of the file from the input stream. |
Java File Output Stream
FileOutputStream is used to create a file and write data into it file. The stream would create a file, if it doesn't already exist, before opening it for output.
Example:- OutputStreamf = newFileoutputStream("D:\\abc.txt");
Method | Description |
void finalize() | It is used to clean up the connection with the file output stream. |
void Write(int W) | This methods writes the specified byte to the output stream. |
void write(byte[] ary) | It is used to write ary.length bytes from the byte array to the file output stream. |
void close() | It is used to closes the file output stream. |
Комментарии