File Concept Simplified In Java

digitization, transformation, binary
Spread the love

File in Java
File in Java

File in Java:

File is an important concept while doing any I/O operation using java. Frequently to read and write to a storage we use file. This is placed inside package.

In UNIX operating system, there is no separate thing defined for files and directories. Java File IO concept is defined and implemented based on UNIX. Hence File IO operations are same for file and directories.

We can store values using variables and arrays but

  • The data is lost either when a variable goes out of scope or when the program is terminated.That means the storage is temporary.
  • It is difficult to handle large volume of data using variables and arrays.

To overcome these problems we can place the data to a secondary storage devices (like hard disk,floppy drive,pen drive etc ). The data stored in secondary devices are called persistent data.

A file is a collection of related records placed in a particular area of the disk.A record is composed of several fields and a filed is a group of characters.
Characters in java are Unicode characters composed of two bytes,each byte is having eight binary bits(0 or 1).Storing and managing data using file is known as file processing which includes tasks such as creating files,updating files and manipulation of data.Reading and writing of data in a file can be done at the level of bytes or characters or fields depending on requirements. The process of reading and writing objects is called Object serialization.Refer here and here for serialization.

READ  Internationalization Concept Simplified In Java

Class file is written here in details.Please take a look.

File f=new File("path\filename.txt");

This statement in java will not create a new file instead it will check if there is any physical file exists in the given path. If so it will simply point to that file.If not it will do anything.

File fl=new File("c:\test.txt");
//this will simply return false
//This will create a file
//Now it will return true

Similarly for directories also we can use the same commands…

File fl=new File("c:\test");
//this will simply return false
//This will create a directory inside C drive named as test
//Now it will return true

Let us check the constructors available in file…

File f=new File(String FileName)
// Checks if a file is present in the current working directory
File f=new File(String subDirectory,string fileName)
//This will create a file in given directory
// if the path is not given as a part of FileName  
File f=new File(File SubDirectory,String FileName)
//This will create a file in the sub directory under current directory
// if the path is not given as a part of FileName

Important methods for file:

  • boolean exists() This method checks if the physical file is available or not. If that is available it returns true or else it returns false
  • boolean createNewFile() This method checks if the physical file is available or not. If it is available it will return false without creating a new one. Else it will create a new file with the name provided.
  • boolean mkdir()  Same as above.
  • boolean isFile()  If the file object points to a valid physical file then it returns true else it will return false.
  • boolean isDirectory() Same as above.
  • String[] list() It returns all files and subdirectories present in specific sub directory.
  • long length() It returns the number of character presents in a file.
  • boolean delete() if file object points to a physical file and is able to delete the same, it will return true else will return false.
READ  Empty Statements in Java

Spread the love
I am the founder and owner of the blog -, always love to share knowledge on test automation,tools,techniques and tips.I am a passionate coder of Java and VBScript.I also publish articles on Travel ideas and great honeymoon destinations.Apart from these, I am a gear-head,love to drive across India. I have shared lots of articles here on How to travel several parts of India.Customization of cars aka car modification is my another hobby.Get in touch with me on


Please enter your comment!
Please enter your name here