Sunday, December 5, 2021

Java - Files and I/O

Java I/O (Input and Output) is used to process the input and produce the output. The Java I/O package (java.io), provides a set of input streams and a set of output streams used to read and write data to files or other input and output sources.

There are important categories of classes in java.io:

input streams, and output streams.



Input Streams:
Input streams read data from an input source. An input source can be a file, a string, or memory, anything that can contain data.

All input streams inherit from InputStream - an abstract class that defines the programming interface for all input streams. The InputStream class defines a programming interface for reading bytes or arrays of bytes, marking locations in the stream, skipping bytes of input, finding out the number of  bytes that are available for reading, and resetting the current position within thestream. An input stream is automatically opened when you create it.

  Following constructor takes a file name as a string to create an input stream object to read the file−

InputStream f = new FileInputStream("E:/java/hello");

Following constructor takes a file object to create an input stream object to read the file. First we
create a file object using File() method as follows −

File f = new File("E:/java/hello");

InputStream f = new FileInputStream(f);

  • Useful methods of InputStream:

 


Method



Description



1) public abstract int read() throws IOException



reads the next byte of data from the input stream. It returns -1 at the end of the file.



2) public int available() throws IOException



returns an estimate of the number of bytes that can be read from the current input stream.



3) public void close()throws IOException



is used to close the current input stream.




 Output Streams:


Output streams write data to an output source. An output source can be anything that can contain data: a file, a string, or memory.

The OutputStream class is used to write data that can then be read by an input stream. The OutputStream class defines a programming interface for writing bytes or arrays of bytes to the stream and flushiing the stream. Like an input stream, an output stream is automatically opened when you create it.

Following constructor takes a file name as a string to create an input stream object to write the file −

 OutputStream f = new FileOutputStream("C:/java/hello")

 Followingconstructor takes a file object to create an output stream object to write the
file. First, we create a file object using File() method as follows −

File f = new File("C:/java/hello");

OutputStream f = new FileOutputStream(f);

Useful methods of OutputStream:



Method



Description



1) public void write(int)throws IOException



is used to write a byte to the current output stream.



2) public void write(byte[])throws IOException



is used to write an array of byte to the current output stream.



3) public void flush()throws IOException



flushes the current output stream.



4) public void close()throws IOException



is used to close the current output stream.


Ex:

import java.io.*;

public class fileStreamTest {

   public static void main(String args[]) {

        try {

         byte bWrite [] = {11,21,3,40,5};       

OutputStream os = new FileOutputStream("test.txt");        

for(int x = 0; x < bWrite.length ; x++) {           

os.write( bWrite[x] );   // writes the bytes

         }       

os.close();

InputStream is = new FileInputStream("test.txt");

int size = is.available();        

for(int i = 0; i < size; i++) {           

System.out.print((char)is.read() + "  ");

}       

is.close();

} catch (IOException e) {       

System.out.print("Exception");

 }   

 }

}


0 comments:

Post a Comment

Data Structures with C++



NET/SET/CS PG



Operating Systems



Computer Networks



JAVA



Design and Analysis of Algorithms



Programming in C++

Top