Append to a File in Java

In this tutorial, we will learn How to Append to a File in Java. We do write to a file in Java. If file already exists, then we need to append the content to it. Let’s take a File and try to append a string to it. File Contents:

Example:

Output:

Other tutorials: Write to a file in Java Read a File in Java Create a File in Java Delete a File in Java

Read More

List all files and folders in a folder in Java

In this tutorial, we will see How to list all files and folders in a folder in Java. File class in java.io package provides listFiles() method to list files in a folder. The listFiles() method returns an array of files in the directory. Let’s see the usage of listFiles() method with an example. Example:

Output:

You can also learn about: Read a File in Java Write to a file in Java

Read More

How to make a file read only in Java ?

In this tutorial, we will see How to Make a file read only in Java. We will use setReadOnly() method from File class to make a file read only in Java. setReadOnly() method returns a boolean value which can be used to verify whether the file has become read only or not. We will use canWrite() method to check if the file is read only or not.

Output:

You can also learn about Read a file in Java Write to a file in Java

Read More

Delete a File in Java

  Many times we need to delete a file once the job is done. In this tutorial we will see how to delete a file in Java. File class in Java has a method delete(). This method deletes the file or directory denoted by the abstract pathname. If the pathname denotes a directory, then the directory must be empty in order to be deleted. Syntax:

Let’s see file deletion with an example. File Deletion:

Output:

Read More

Read a File in Java

  We come across many times that we need to read from a file in Java. In Java we can do this using FileReader class. FileReader is a Class for reading character files. This class provides many constructors and methods. In Java, we can read a file using different ways. Here we will use read() method from FileReader class to read from file. This method reads a single character. This method returns the character read or -1 if the end of the stream has been reached. Syntax:

Let’s see how…

Read More

Write to a file in Java

  When you are dealing with Files, you may need to write to a file in Java. In this tutorial we will see how to write to a file. In Java, we can write to a file in different ways. Let’s see how to write to a file using FileWriter class. Using FileWriter: FileWriter  is a class for writing streams of characters to a File. FileWriter class provides many constructors to create objects. This class provides many methods for file operations but we will use write() method to write to a…

Read More

Create a File in Java

  What are Files ? Files are a primary source and destination for data within many programs. A File is an abstract representation of file and directory pathnames. A File object is used to obtain or manipulate the information associated with a disk file, such as the permissions, time, date, and directory path, and to navigate sub-directory hierarchies. The java.nio.file package defines interfaces and classes for the Java virtual machine to access files, file attributes, and file systems. File class defines many methods that obtain the standard properties of a…

Read More