file handling in java

file handling in java

Creating, reading, updating and deleting files are the core functions of any file management software. Java has a standard package called java.io that deals with files and folders.

It also provides helper classes to create, read, write, append and delete files in your application. This article lists several examples of file handling in java :

File – The primary logical unit of information in a computer; a collection of related data usually stored as a single entity; an organized set of information.

In this article you will learn How to create Files in Java How to check if given Folder exists or not ? How to Read from a File? How to Write into a File? Readers will learn how to handle Files in Java in different scenarios and how to use the correct methods for each case.

Java Check Folder Existence before create a file – file handling in java

To access a file, the first thing we need is to make sure the given folder exists. So, the first thing to do before manipulating a file is to check if the given folder exists or not. We can use the method exists() method from the Files class to check if the folder exists or not.

This method returns a boolean value indicating exists or not. If the given folder doesn’t exist, then create the folder in order to access the file.

We can use the createNewFolder() method from the File class, which creates the folder. This method takes two arguments – name of the folder to create and a boolean value indicating if the new folder should be created in the same directory as the current directory, or in a different directory.

Java Create New File – file handling in java

A file can be created by using the createNewFile() method from the File class. This method creates a new file with the given name in the current directory.

file handling in java

We can also create a new file in a specified folder using the createNewFile() method, which takes the path of the folder as an argument. This method returns a File object, which can be used to set read and write permissions on the file.

Java Read from a File

File can be read by using the readAllBytes() method from the File class. This method reads the entire file and returns the bytes of the file as a byte array.

We can also read the specific line of the file by using the readLine() method from the File class. This method reads the line of the file and returns the line as a string.

Java Write into a File

File can be written by using the writeAllBytes() method from the File class. This method writes the entire file and takes a byte array as an argument.

We can also write specific lines of the file by using the writeLine() method from the File class. This method writes the line of the file and takes the string as an argument.

Java Delete a file

File can be deleted by using the delete() method from the File class. This method deletes the file specified by the path of the file.

To delete a file that is in the current directory, we use the deleteIfExists() method from the File class. This method deletes the file specified by the path of the file if it exists.

Conclusion

These are the methods of file handling in Java. We can use these methods to read and write data to the files in our applications.

We can also create and delete folders in order to store the files in them. In order to use these methods, we need to import the java.io package in our code.

Add a Comment

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