Files IO

Creating New Directories in Java

Learn to create a new single directory or nested directory along with parent directories in a specified path using Java IO and NIO classes.

Writing Byte[] to a File in Java

Learn to write the given byte[] into a file using different solutions. We will be using the Java NIO, Commons IO and Guava APIs that provide simple APIs for this usecase.

Reading a Large File Efficiently in Java

Learn to read all lines from a large file (size in GB) in Java and avoid any performance pitfalls such as very high usage of memory or even OutOfMemoryError if the File is large enough.

Rename or Move a File or Directory in Java

Learn to rename a file or directory at a specified path or move to a new directory in Java. We will learn to use the classes from Standard IO, New IO, Guava and Commons IO.

Getting Filesystem Paths in Java

Learn the difference between path, absolute and canonical paths. Also, learn to get the path of a file in Java using standard IO and New IO classes.

JUnit 5 @TempDir Example: Temp Directory / File Support

Sometimes tests require temporary directories and files for performing intermediate test steps. JUnit 5 provides the @TempDir annotation that supplies a temporary directory to the test method or lifecycle methods. 1. JUnit @TempDir Annotation JUnit Jupiter provides an extension model that allows us to extend the behavior of the test …

Java FileWriter

The Java FileWriter class is for writing the text to the character-based files using a default buffer size. It uses character encoding default to the platform, if not provided otherwise. FileWriter is usually wrapped by higher-level Writer types, such as BufferedWriter or PrintWriter. FileWriter provides better performance and higher-level, more …

Java FileReader

Java FileReader class can be used to read data (stream of characters) from files. In this tutorial, we will learn about FileReader class, its constructors, methods and usages with the help of examples. 1. Introduction The FileReader class is: 2. Creating FileReader To use the FileReader in the application, we …

Java 11 Files.readString()

Learn to read a file to string in Java using Files.readString(path) method. This API has been introduced in Java 11.

Appending to a File in Java

Learn to append the data to a file in Java using Standard IO’s BufferedWritter, PrintWriter, FileOutputStream and NIO Files classes.

Copying a Directory in Java

Learn to copy a directory into a new location in Java. Learn to apply FileFilter to include or exclude subdirectories and specific files.

Making a File Read-Only in Java

Learn to make a file read-only in Java. A read-only file can be opened for reading, but we cannot modify or delete the file contents.

Copying a File in Java

Copying a file from one place to another in Java is a common task. Learn to copy files using Java NIO, Commons-IO and Guava APIs.

Creating a New File in Java

Learn to create a new file using different techniques including NIO Files and Path, IO File, File OutputStream, and open-source libraries.

Java Delete Temporary File

Learn how to delete a temporary file in Java. We will see examples to use File.deleteOnExit() and File.delete() methods.

Java Create Temp File or Directory

Learn to create a temp file and directory in Java using the legacy and NIO classes. Also, learn to delete temp files or temp directories on JVM shutdown.

Writing to Temporary File in Java

Learn to create a temporary file and write to it in Java. We will use the code sample used for creating a temporary file example.

Read File to Byte[] in Java

Learn reading data from files into a byte array in Java using NIO Files, FileInputStream, Commons IO FileUtils, and Guava ByteStreams classes.

Guide to Java LineNumberReader

For reading a file line by line, the LineNumberReader class could be used that allows to keep track of which line we are currently processing.

Java FileFilter (with Examples)

Java FileFilter accepts File objects and thus can be used to filter the files based on other attributes such as read-only, directory etc.

Java Read a File Line by Line

In this Java tutorial, we will learn to read a text file line-by-line methods such as Streams or FileReader. We will also learn to iterate through lines and filter the file content based on some conditions. 1. Files.lines() – Stream through Lines of a File in Java 8 The Files.lines() …

Parse and Read a CSV File in Java

In Java, there are different ways of reading and parsing CSV files. Let’s discuss some of the best approaches such as OpenCSV, Super CSV etc.

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.