Home » Java » Core Java » Java File I/O Basics

About Michael Scharhag

Michael Scharhag
Michael Scharhag is a Java Developer, Blogger and technology enthusiast. Particularly interested in Java related technologies including Java EE, Spring, Groovy and Grails.

Java File I/O Basics

Java 7 introduced the java.nio.file package to provide comprehensive support for file I/O. Besides a lot of other functionality this package includes the Files class (if you already use this class you can stop reading here).
Files contains a lot of static methods that can be used to accomplish common tasks when working with files. Unfortunately it looks to me that still a lot of newer (Java 7+) code is written using old (pre Java 7) ways of working with files. This does not have to be bad, but it can make things more complex than needed. A possible reason for this might be that a lot of articles and high rated Stackoverflow answers were written before the release of Java 7.

In the rest of this post I will provide some code samples that show how you can accomplish common file related tasks with Java 7 or newer.

Working with files

// Create directories
// This will create the "bar" directory in "/foo"
// If "/foo" does not exist, it will be created first

// Copy a file
// This copies the file "/foo/bar.txt" to "/foo/baz.txt"
Files.copy(Paths.get("/foo/bar.txt"), Paths.get("/foo/baz.txt"));

// Move a file
// This moves the file "/foo/bar.txt" to "/foo/baz.txt"
Files.move(Paths.get("/foo/bar.txt"), Paths.get("/foo/baz.txt"));

// Delete a file

// Delete a file but do not fail if the file does not exist

// Check if a file exists
boolean exists = Files.exists(Paths.get("/foo/bar.txt"));

Most methods of Files take one or more arguments of type Path. Path instances represent a path to a file or directory and can be obtained using Paths.get(). Note that most methods shown here also have an additional varargs parameter that can be used to pass additional options.

For example:

Files.copy(Paths.get("/foo.txt"), Paths.get("/bar.txt"), StandardCopyOption.REPLACE_EXISTING);

Iterating through all files within a directory

Files.walkFileTree(Paths.get("/foo"), new SimpleFileVisitor<Path>() {
  public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
    System.out.println("file: " + file);
    return FileVisitResult.CONTINUE;

Here the visitFile() method will be called for every file within the /foo directory. You can override additional methods of SimpleFileVisitor if you want to track directories too.

Writing and reading files

// Write lines to file
List<String> lines = Arrays.asList("first", "second", "third");
Files.write(Paths.get("/foo/bar.txt"), lines, Charset.forName("UTF-8"));

// Read lines
List<String> lines = Files.readAllLines(Paths.get("/foo/bar.txt"), Charset.forName("UTF-8"));

The shown methods work with characters. Similar methods are available if you need to work with bytes.


If you didn’t know about java.nio.file.Files you should definitely have a look at the Javadoc method summary. There is a lot of useful stuff inside.

Reference: Java File I/O Basics from our JCG partner Michael Scharhag at the mscharhag, Programming and Stuff blog.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

and many more ....


Leave a Reply

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


Want to take your Java Skills to the next level?
Grab our programming books for FREE!
  • Save time by leveraging our field-tested solutions to common problems.
  • The books cover a wide range of topics, from JPA and JUnit, to JMeter and Android.
  • Each book comes as a standalone guide (with source code provided), so that you use it as reference.
Last Step ...

Where should we send the free eBooks?

Good Work!
To download the books, please verify your email address by following the instructions found on the email we just sent you.