What’s File Dealing with in Java?

file handling in java

File dealing with in Java is outlined as studying and writing information to a file. The actual file class from the bundle referred to as java.io permits us to deal with and work with totally different codecs of information. Thus,  if we need to use a file class, we have to create an object of that exact class and will specify the filename or listing title.

If you wish to deep dive additional, do try our Software program Engineering Programs at Nice Studying in collaboration with high engineering schools and universities, together with IIT Madras, Nice Lakes, & IIIT Hyderabad. Take part in commonly organized profession accelerated applications and placement drives provided by Nice Studying and get employed by the highest main corporations throughout totally different industries.

  1. Streams
  2. Understanding Streams
  3. Enter and Output Streams
  4. The Courses for Enter and Output
  5. Primary Enter Stream Operations 
  6. Primary Output Streams Operations 
  7. The Normal Streams
  8. Studying and Writing Information
  9. Strategies
  10. Object 
  11. Java Directories
  12. Querying Information and Directories


As mentioned above, the bundle that helps stream enter/output is java.io, and it’s huge. This bundle defines over seventy lessons and interfaces, a lot of which have a lot of strategies. 

  • It will likely be capable of provide you with an opportunity to learn information from the information.
  • It can give us an opportunity to create a formatted line for the command.
  • It can give us the prospect to learn or write primary information.
  • It can give us an opportunity to learn or write information containing objects.

Additionally Learn: Java Tutorial for Inexperienced persons

Understanding Streams

The stream is the one illustration of enter or output that’s possibly a supply or vacation spot of the information. We will additionally write the information within the stream or learn the actual information from the stream. We will additionally visualize the stream of information as a sequence of bytes that stream out of this system.

Image result for understanding file streams diagram

Enter and Output Streams

Writing information within the stream or to a stream is known as an output stream. The actual output stream can go to any gadget which may join by way of a tough disk or possibly any stream which may comprise the sequence of bytes. An output stream additionally will be displayed on any output display screen, which has its true functionality. Stream output to your show is output to your command line. Whenever you begin writing one thing in your show display screen, it will possibly solely show the characters however not the graphical content material. Graphical output requires extra specialised help.

You learn information from an enter stream. In precept, this could solely be any supply of serial information however is often a disk file, the keyboard, or a distant laptop.

Additional, underneath regular circumstances, the file enter and output you write in a machine can solely be by way of a java utility. It’s not accessible to java apples besides strictly to a restricted extent. 

This has two benefits: First, you don’t have to fret in regards to the detailed mechanics of every gadget, that are taken care of behind the scenes. 

The streams which you write underneath the enter and output can solely have a small quantity of information like a single character, however no more than that. Transferring information to or from a stream like this can be extraordinarily inefficient, so a stream typically geared up with a buffer in reminiscence, wherein case it’s referred to as a buffered stream.

The Courses for Enter and Output

The logical construction varieties with the variety of stream lessons. As soon as you understand how they’re associated, you shouldn’t have a lot bother utilizing them. We’ll work by way of the category hierarchy from the highest down, so it is possible for you to to see how the lessons grasp collectively and how one can mix them in numerous methods to swimsuit totally different conditions.

java.io it accommodates all of the lessons for the help of the streams.

Class Description
Enter Stream The bottom class for byte stream enter operations.
Output Stream The bottom class for byte stream output operations.

Enter Stream and Output Stream are each summary lessons.

Primary Enter Stream Operations 

As you noticed within the earlier assertion, the Enter Stream class is summary, so you can’t create objects of this class kind.

Technique Description
learn() This methodology is summary within the Enter Stream class, so it needs to be outlined in a subway. This methodology returns the subsequent byte accessible as stream int. As soon as the stream is getting in direction of an finish, the tactic provides -1. An exception of kind IOException can be thrown if an I/O error happens. 
learn(byte[] array) It reads byte from the successive streams to the array. The utmost of array.size bytes can be learn. This methodology won’t return the information till the stream will get to the tip. If the tactic will attain the tip then it won’t return the tip of bytes.
learn(byte[] array, int offset, int size) It really works the identical because the earlier strategies besides the size.

Primary Output Streams Operations 

Unbiased output stream accommodates three write strategies for writing the information right into a stream. As will be anticipated, these mirrors the learn() strategies of the Enter Stream class. This class can also be summary, so subclasses will be instantiated. The principal direct subclasses of Output Stream are proven.  

  • FileOutputStream – for writing to a file
  • ObjectInputStream – for writing objects to a stream
  • ByteArrayOutputStream – It’s used to put in writing the array of bytes
  • PipeOutputStream – It’s used for writing to a piped stream
  • FilterOutputStream – it’s used for filtering the output from an finish stream

The Normal Streams

Your working system will sometimes outline three customary streams which can be accessible by way of members of the system class in java:

  • A Normal enter stream that corresponds to the actual keyboard would be the default. That is encapsulated by the in a member of the system class and is of the sort enter stream.
  • A Normal output stream will at all times correspond to the command line of the output. That is encapsulated by the out member of the system class and is of kind print stream.
  • A Normal error output stream for error messages that normally maps to the command-line output by default. It is usually encapsulated by err members of a category system and print stream.

Studying and Writing Information

Studying information

The method we use for studying a file is sort of just like writing a file. First, you’ll acquire a file channel of an object from a file stream and likewise use the identical channel to learn the information for a number of buffers. Initially, you can be utilizing a channel object that you simply acquire from a FileInputStream object to learn a file. Later you can be utilizing a FileChannel object obtained from a RandomAccessFile object to learn and write the identical file.

A FileInputStream object encapsulates a file that’s basically meant to be learn, so the file should exist already and comprise some information.


FileInputStream inputFile = null;  
          // place to retailer the enter stream reference
Strive {
     inputFile = new FileInputStream(*C:/Beg Java Stuff/myFile.txt*);
Catch(FileNotFoundException e) {

Creating File Enter Streams

A FileInputStream object encapsulates a file that it desires to learn however it ought to exist already within the file and must also comprise some information to learn. It says that the constructor of a category in an object can be created in file provided that it already exists.

There’s a chance even to make use of a file object that you’ve obtained by calling the getFD() methodology for an current FileInputStream object, or presumably a random entry file object. 

File aFile = new File(* C:/Beg Java Stuff/myFile.textual content*);
FileInputStream inputFile1=null;
FileDescriptor fd=null;

Strive {
   // create the stream opened to put in writing
   inputFile1=new FileInputStream(aFile);
} catch (IOException e)   {

// now you can create one other enter stream for the file from the file descriptor…
    FileInputStream inputFile2=new FileInputStream(fd); 

Upon getting created the ObjectInputStream object, you name its readObject() methodology to learn an object from the file.

Object myObject = null;
Strive {
    myObject = objectIn.readObject();

}  catch(ClassNotFoundException e) {

} catch(IOException e) {

Writing Information

The method for writing a file is principally fairly easy. For writing a file, you’ll load a file that you’ve created as a number of buffers and name a technique for that exact object to put in writing information to that file which is encapsulated by the file stream. 

To start out with, you can be utilizing the only write() methodology for a file channel that writes the information contained in a single ByteBuffer object to a file. The variety of bytes written to the file is decided by the buffers place and restrict when the write() methodology executes.

To create a file in a particular listing, specify the trail of the file and use double black slashes to flee the “” character.



File theFile = new File(“ MyFile”);
   //Take a look at the file…

   //Create the item output stream for the file
   ObjectOutPutStream objectOut = null;
   Strive {
       Objectout = new ObjectOutputStream(new FileOutputStream(theFile))

The 5 totally different exceptions {that a} channel write() operation will throw:



canread() – It’s a Boolean kind and it says whether or not the file which we wish is readable or not.

canwrite() – It’s a Boolean kind and it says whether or not the file which we wish is writable or not.

createnewfile() – It’s a Boolean kind that claims that the tactic creates an empty file.

delete() – It’s a Boolean kind which deletes a file which we wish.

exist() – It’s a Boolean kind and says whether or not the file exists or not.

getname() – It’s a string kind which returns the file title or title of the file.

getabsolutepath() –  It’s a string kind which returns the right path of title of the file.

size() – It’s a lengthy kind which returns the needed dimension of the file in bytes.

listing() – It’s a string kind which returns the array of the file within the listing.

mkdir() – It’s a Boolean kind which creates a listing.

File navigation and I/O

This idea comes as an enter and output of a file the place the stream of information that both you’ll be able to learn or you’ll be able to write or you’ll be able to carry out learn and write operations in a file completely. Java makes use of these enter and output streams to carry out these duties.


  • InputStream
    • FileInputStream
    • ByteArrayInputStream
    • FileInputStream  
    • ObjectInputStream
    • OutPutStream
  • FileOutputStream
    • ByteArrayOutputStream
    • FileOutputStream
    • ObjectOutputStream

An summary of file navigation and I/O is above diagram

Image result for java file navigation and io in java

Java Directories

Creating and Modifying Information and Directories listing 

renameTo(File path) The file represented by the present object can be renamed to the trail represented by the File object handed as an argument to the tactic. This doesn’t change the present file object in your program and it alters the bodily file.
setreadonly() Units the file represented by the present object as read-only and returns true if the operation is efficiently.
mkdir() Creates a listing with the trail specified by the present file object.
mkdirs() Creates the listing represented by the present file object, together with mum or dad directories which can be required.
createnewfile() Create a brand new empty file with the pathname outlined by the present file object so long as the file does exist.
delete() It will delete the file and listing represented by the present file object and return true if the delete is profitable.

Querying Information and Directories

exists() Returns true if the file or listing referred to by the file object exists and false in any other case.
isfile() Returns true if the file object refers to an current file and false in any other case.
canread() Returns true if you’re permitted to learn the file referred by the file object.
canwrite() Returns true if you’re permitted to put in writing the file referred by the file object.

This brings us to the tip of the weblog on File Dealing with in Java.

Newsletter Updates

Enter your email address below to subscribe to our newsletter

Leave a Reply