File Concept Simplified In Java

0
153
Spread the love
  • 1
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
    1
    Share

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 Java.io 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.

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");
System.out.println(fl.exist());
//this will simply return false
fl.createNewFile();
//This will create a file
System.out.println(fl.exist());
//Now it will return true

Similarly for directories also we can use the same commands…

File fl=new File("c:\test");
System.out.println(fl.exist());
//this will simply return false
fl.mkdir();
//This will create a directory inside C drive named as test
System.out.println(fl.exist());
//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.
Don't miss out!
Subscribe To Newsletter

Receive top technical news, lesson ideas, travel tips and more!

Invalid email address
Give it a try. You can unsubscribe at any time.

Spread the love
  • 1
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
    1
    Share

LEAVE A REPLY

Please enter your comment!
Please enter your name here