File Handling In Java

File handling:

The java.io package contains nearly every category you would possibly ever got to perform input and output (I/O) in File Handling in Java. although of these streams represent AN input supply ANd an output destination. The stream within the java.io package supports much information like primitives, objects, localized characters, etc.

Stream

A stream will be outlined as a sequence of information. There are 2 forms of forms in File Handling:-

  1. InPutStream − The InputStream is employed to browse information from a supply.
  2. OutPutStream − The OutputStream is employed for writing information to a destination.

Byte Streams

Java computer memory unit streams are wont to perform input and output of 8-bit bytes. tho’ there are several categories associated with computer memory unit streams however the foremost oftentimes used categories ar, FileInputStream and FileOutputStream. so, the Following is AN example that makes use of those 2 categories to repeat AN input data into AN computer file.

Example

import java.io.*;

public category CopyFile 

      }finally 

         if (out != null) 

      }

   }

}

Character Streams

Java computer memory unit streams are wont to perform input and output of 8-bit bytes, whereas Java Character streams are wont to perform input and output for 16-bit Unicode. tho’ there are several categories associated with character streams however the foremost oftentimes used categories are, FileReader and FileWriter. Though internally FileReader uses FileInputStream and FileWriter uses FileOutputStream however here the foremost distinction is that FileReader reads 2 bytes at a time and FileWriter writes 2 bytes at a time.

although We can re-write the higher than example, which makes the employment of those 2 categories to repeat input data (having Unicode characters) into a computer file.

Example

import java.io.*;

public category CopyFile 

      }finally 

         if (out != null) 

      }

   }

}

Standard Streams

All the programming languages offer support for normal I/O wherever the user’s program will take input from a keyboard then manufacture AN output on the pc screen. If you’re responsive to C or C++ programming languages, then you want to remember 3 commonplace devices STDIN, STDOUT and STDERR. Similarly, Java provides the subsequent 3 commonplace streams −

  1. Standard Input − this can be won’t to feed the info to user’s program and typically a keyboard employs as commonplace input stream and pictured as System.in.
  1. Standard Output − so, this can be won’t to output the info made by the user’s program and typically a video display employs for normal output stream and pictured as System.out.
  1. Error (Standard) − though this can be won’t to output the error information make by the user’s program and typically a video display employs for normal error stream and pictured as System.err.

Following may be an easy program, that creates InputStreamReader to browse commonplace input stream till the user sorts a “q” −

Example

Live Demo

import java.io.*;

public category ReadConsole 

      }

   }

}

Reading and Writing Files

As delineated earlier, a stream will be outline as a sequence of information. The InputStream employs to browse information from a supply and therefore the OutputStream employs for writing information to a destination.

Here may be a hierarchy of categories to affect Input and Output streams.

Files IO

The two necessary streams are FileInputStream and FileOutputStream, which might be mentioned during this tutorial.

FileInputStream

thus, This stream is employed for reading information from the files. hence, Objects will be created victimization the keyword new and there are many forms of constructors on the market.

Following creator takes a file name as a string to make AN input stream object to browse the file −

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

Following creator takes a file object to make AN input stream object to browse the file. 1st we tend to produce a file object victimization File() methodology as follows −

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

InputStream f = new FileInputStream(f);

Once you have got InputStream to object in hand, then there’s an inventory of helper ways which may be wont to browse to stream or to try to different operations on the stream.

Directories in Java

A directory may be a File which may contain an inventory of different files and directories. You utilize File object to make directories, to list down files on the market in an exceeding directory. also, For complete detail, check an inventory of all the ways that you’ll invoke the File object and what are associated with directories.

Creating Directories

There are 2 helpful File utility ways, which may be wont to produce directories :

  1. The mkdir( ) methodology creates a directory, returning true on success and false on failure. Failure indicates that the trail per the File object already exists, or that the directory can’t be created as a result of the complete path doesn’t exist nevertheless.
  1. thus, The mkdirs() methodology creates each a directory and every one the oldsters of the directory.

Conclusion | File Handling: 

  • The Path class has methods for manipulating a path.
  • however, The Files class has methods for file operations, such as moving, copy, deleting, and also methods for retrieving and setting file attributes.
  • hence, The File System class has a variety of methods for obtaining information about the file system.

written by: Aman Yadav

reviewed by: Soutik Maity

If you are Interested In Machine Learning You Can Check Machine Learning Internship Program
Also Check Other Technical And Non Technical Internship Programs

Leave a Comment

Your email address will not be published. Required fields are marked *