Home » Core Java » io » File

File

Delete File in Java Example

In this example we are going to see how to delete a file in Java. Of course Java offers a very convenient API to perform deletion and creation. Most of them are placed in File class. We are going to use delete() methods that deletes the file or directory. If the file is deleted successfully, the methods returns true, else ...

Read More »

Get the filepath of a File in Java

In this tutorial we are going to see how to get the absolute file path as well as the path of the parent directory of a specific file. This is very useful because it gives a generic way to get the absolute paths of files, independently of the operating system you use. Additionally, among many other uses cases, if you’ve ...

Read More »

How to Read an Object from File in Java

In the previous tutorial we saw how to write an Object to a file in Java. In this example we are going to see how to read an Object from the file that we’ve stored it earlier. Basically, to read an Object from a file, one should follow these steps: Open a FileInputStream to the file that you’ve stored the ...

Read More »

Get free disk space in Java example

With this tutorial we are going to see how to use the File class in order to get the size of certain disk partitions in your file system. You can use: getTotalSpace() to get the total capacity of the disk partition you want. getFreeSpace() to get the free space of the disk partition. getUsableSpace() to get the usable space in ...

Read More »

Change File Last Modified date in Java example

In this example we are going to see how you can change the “Last Modified” date of a File in your File System in Java. We are simply going to use the setLastModified method of the File class. We are also going to see how you can parse a string with a date format to a Date object which is ...

Read More »

Move Files in Java example

In this example we are going to see how you can move a file to a new location in your File Path. In Java, there is no generic function you can use to do that. Nevertheless, you can use two ways to perform this operation: Use the renameTo function of the File class to do it. Copy the the File ...

Read More »

4 Ways to Copy File in Java

Although Java offers a class that can handle file operations, that is java.io.File, it doesn’t have a copy method that will copy a file to another. The copying action is an important one, when your program has to handle many file related activities. Nevertheless, there are several ways you can perform a file copying operation in Java and we will ...

Read More »

Set File permissions in Java example

With this example we shall show you how to set file permissions with Java. This is a very important task to take into consideration when you’re working in a shared system. Although this operation is operation system specific, Java offers a generic API that you can use in all platforms. As you know in UNIX systems you can go much ...

Read More »