Java allows us to perform operations on files.
We can read contents of a file and even write some content to the file through a Java program.
Isn’t that awesome? 😀
To perform file operations java provides several classes.
These classes are all declared in the java.io package.
Let’s explore these classes and the methods used to perform operations on files.
Creating a file
To perform any operation on the file, we first need to create that file.
To do so, createNewFile() method is used.
createNewFile() method is defined in the class File which is present in the java.io package.
createNewFile() method returns a boolean value which tells whether the file was successfully created or not.
Let’s write a program which will create a file named lazyMe.txt for us.
import java.io.File; //importing File class from java.io package
public class Main {
public static void main(String[] args) {
File file = new File(“lazyMe.txt”); //instantiating new object
try {
boolean newFile = file.createNewFile(); //create new file
System.out.println(“Successful? “+newFile);
}
catch (Exception e) {
System.out.println(“Oops! Some problem occured.”);
}
}
}
The output of above code will be:
Successful? true
Since the file was successfully created, the createNewFile() method returned true.
In case the file wasn’t created successfully, the output would have been
Successful? false
Awesome! We learnt how to create a file. Let’s write some text in that file.
Writing into a file
To write in a file java provides many classes.
FileWriter – It can be used to write int and string to the file. It is preferred when the number of writes is less.
BufferedWriter – It uses a buffer to write into files. It is used in case the number of writes is more. It improves the performance.
FileOutputStream – FileWriter and BufferedWriter are used to write text to files. FileOutputStream is used to write raw data to the files.
Let’s write a program in java to write a string into a file using FileOutputStream.
import java.io.FileOutputStream;
import java.io.File;
import java.io.IOException;
public class Main {
public static void main(String[] args) {
FileOutputStream writeFile = null;
File file;
String myText = “Let’s write something in this file.”;
try {
file = new File(“lazyMe.txt”); //new File object
writeFile = new FileOutputStream(file); //FileOutputstream object
byte[] byteData = myText.getBytes(); //string to bytes conversion
writeFile.write(byteData); //writing converted byteData to file
System.out.println(“Successful.”);
}
catch (IOException ioe) {
System.out.println(“OOPs! There was some problem.”);
}
}
}
The output of the program will be
Successful.
And “Let’s write something in this file.” will be written in the file lazyMe.txt.
Here, we used FileOutputStream and write() to write text in the file.
Reading a file
The contents of a file can be read using FileInputStream and BufferedReader.
read() method is used to read the contents of the file character by character.
Let’s read the contents of the file using FileInputStream.
import java.io.FileInputStream;
public class Main {
public static void main(String args[]){
try{
FileInputStream myFile= new FileInputStream(“lazyMe.txt”);
int i=0;
while((i=myFile.read())!=-1){
//reading every character
System.out.print((char)i); // printing to the screen
}
myFile.close(); //closes the file after reading
}
catch(Exception e){
System.out.println(e);
}
}
}
By the above code content in file will be read and printed to the screen.
In case the file is not found
java.io.FileNotFoundException: lazyMe.txt (No such file or directory)
Will be printed on the output screen.
Delete a file
Now, we know how to create, read and write a file.
Let’s see how we can delete a file.
delete() method of File class is used to do so.
Here is a program in java which deletes a file.
import java.io.File;
public class Main {
public static void main(String[] args) {
File file = new File(“lazyMe.txt”); //file to be deleted
boolean deleteMe = file.delete(); //delete method
System.out.println(“Deleted? ” + deleteMe);
}
}
delete() above method returns a boolean value based on whether the file was deleted or not.
So the output will be
Deleted? true
And, if the file was not deleted
Deleted? false
To Summarize
To perform file operations java provides several classes which are all declared in java.io package.
createNewFile() method is used to create a new file.
write() method is used to write into a file and any of the FileWriter, BufferedWriter or FileOutputStream can be used to do so.
Similarly, the contents of a file can be read using FileInputStream and BufferedReader. read() method is used to do so.
And, delete() method of File class is used to delete a file in Java.