Reading and writing files | Tutorialspoint | W3school

Reading and writing files:

As discussed earlier, a stream can be classified as a series of data. To read data InputStream is used from a source as well as for writing data the outputStream is used.
Here comes the hierarchy of classes that we have to deal with Output and Input Streams.

5

You know about the most common used streams and that are FileoutStream and Inputstream, which would be defined in this tutorial.

Types of constructors available:

Following constructor takes a filename like a string to create an input stream object to read the file −

InputStream f = new FileInputStream(“C:/java/hello”);
Following constructor takes a file object to create an inputstream object in order to read the file. First we have to create a file object by using File() method as follows −

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

InputStream f = new FileInputStream(f);

Once you got InputStream object in your hand, then there is a series of things of helper methods which can also be used to read stream or to do other operations on that stream.

Method & Description

  1. public void close() throws IOException{}This way shut downs the file output stream. Releases any system resources associated with the file. Throws an IOException.
  2. protected void finalize()throws IOException {}This method cleans up the connection to the file. It Makes sure that the close method of this file output stream is known when there no more references to this stream. Throws an IOException.
  3. public void write(int w)throws IOException{}This methods writes the specified byte to the output stream.
  4. public void write(byte[] w)Writes w.length bytes from the mentioned byte array to the OutputStream.

Example

Given below is the illustration to represent InputStream and OutputStream −

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”);
}
}
}

Above code would create file test.txt and also write given numbers in the binary format. Same would be the output of Stdout screen.

File Navigation and I/O:

There are so many other classes that we would go through to know about the basics of I/O and File navigation.

FileReader Class

File Class

FileWriter Class

A directory- its a file which can include a list of other directories and files. You can use File object to make directories, to list down files that are available in a directory. For full detail, check a list of the methods available which can be call on File object which are also related to other directories.

Creating Directories

There are two File Utility methods which can be used as to create directories-
The mkdir() this method is used to create a directory. If it fails then it means the path specified in file object exists already and if the directory not able to create then this mean the entire path not contain any directory.

  • The mkdir( ) method creates a directory, returning true on success and false on failure. Failure indicates that the path specified in the File object already exists, or that the directory cannot be created because the entire path does not exist yet.
  • The mkdirs() method creates both a directory and all the parents of the directory.

Following example creates “/tmp/user/java/bin” directory −

Output

test1.txt
test2.txt
ReadDir.java
ReadDir.class