Files tutorial in java

Java Files

File handling is an important part of any application.

Java has several methods for creating, reading, updating, and deleting files.

Java File Handling

The File class from the java.io package, allows us to work with files.

To use the File class, create an object of the class, and specify the filename or directory name:

Example

import java.io.File; // Import the File class File myObj = new File("filename.txt"); // Specify the filename 

If you don’t know what a package is, read our Java Packages Tutorial.

The File class has many useful methods for creating and getting information about files. For example:

Method Type Description
canRead() Boolean Tests whether the file is readable or not
canWrite() Boolean Tests whether the file is writable or not
createNewFile() Boolean Creates an empty file
delete() Boolean Deletes a file
exists() Boolean Tests whether the file exists
getName() String Returns the name of the file
getAbsolutePath() String Returns the absolute pathname of the file
length() Long Returns the size of the file in bytes
list() String[] Returns an array of the files in the directory
mkdir() Boolean Creates a directory

You will learn how to create, write, read and delete files in the next chapters:

Источник

File Operations

The Files class is the other primary entrypoint of the java.nio.file package. This class offers a rich set of static methods for reading, writing, and manipulating files and directories. The Files methods work on instances of Path objects. Before proceeding to the remaining sections, you should familiarize yourself with the following common concepts:

Releasing System Resources

Many of the resources that are used in this API, such as streams or channels, implement or extend the java.io.Closeable interface. A requirement of a Closeable resource is that the close method must be invoked to release the resource when no longer required. Neglecting to close a resource can have a negative implication on an application’s performance. The try- with-resources statement, described in the next section, handles this step for you.

Catching Exceptions

With file I/O, unexpected conditions are a fact of life: a file exists (or doesn’t exist) when expected, the program doesn’t have access to the file system, the default file system implementation does not support a particular function, and so on. Numerous errors can be encountered.

All methods that access the file system can throw an IOException . It is best practice to catch these exceptions by embedding these methods into a try- with-resources statement, introduced in the Java SE 7 release. The try- with-resources statement has the advantage that the compiler automatically generates the code to close the resource(s) when no longer required. The following code shows how this might look:

Charset charset = Charset.forName(«US-ASCII»); String s = . ; try (BufferedWriter writer = Files.newBufferedWriter(file, charset)) < writer.write(s, 0, s.length()); >catch (IOException x)

Alternatively, you can embed the file I/O methods in a try block and then catch any exceptions in a catch block. If your code has opened any streams or channels, you should close them in a finally block. The previous example would look something like the following using the try-catch-finally approach:

Charset charset = Charset.forName(«US-ASCII»); String s = . ; BufferedWriter writer = null; try < writer = Files.newBufferedWriter(file, charset); writer.write(s, 0, s.length()); >catch (IOException x) < System.err.format("IOException: %s%n", x); >finally

In addition to IOException , many specific exceptions extend FileSystemException . This class has some useful methods that return the file involved ( getFile ), the detailed message string ( getMessage ), the reason why the file system operation failed ( getReason ), and the «other» file involved, if any ( getOtherFile ).

The following code snippet shows how the getFile method might be used:

try (. ) < . >catch (NoSuchFileException x)

For purposes of clarity, the file I/O examples in this lesson may not show exception handling, but your code should always include it.

Varargs

Several Files methods accept an arbitrary number of arguments when flags are specified. For example, in the following method signature, the ellipses notation after the CopyOption argument indicates that the method accepts a variable number of arguments, or varargs, as they are typically called:

Path Files.move(Path, Path, CopyOption. )

When a method accepts a varargs argument, you can pass it a comma-separated list of values or an array ( CopyOption[] ) of values.

In the move example, the method can be invoked as follows:

import static java.nio.file.StandardCopyOption.*; Path source = . ; Path target = . ; Files.move(source, target, REPLACE_EXISTING, ATOMIC_MOVE);

For more information about varargs syntax, see Arbitrary Number of Arguments.

Atomic Operations

Several Files methods, such as move , can perform certain operations atomically in some file systems.

An atomic file operation is an operation that cannot be interrupted or «partially» performed. Either the entire operation is performed or the operation fails. This is important when you have multiple processes operating on the same area of the file system, and you need to guarantee that each process accesses a complete file.

Method Chaining

Many of the file I/O methods support the concept of method chaining.

You first invoke a method that returns an object. You then immediately invoke a method on that object, which returns yet another object, and so on. Many of the I/O examples use the following technique:

String value = Charset.defaultCharset().decode(buf).toString(); UserPrincipal group = file.getFileSystem().getUserPrincipalLookupService(). lookupPrincipalByName("me");

This technique produces compact code and enables you to avoid declaring temporary variables that you don’t need.

What Is a Glob?

Two methods in the Files class accept a glob argument, but what is a glob?

You can use glob syntax to specify pattern-matching behavior.

A glob pattern is specified as a string and is matched against other strings, such as directory or file names. Glob syntax follows several simple rules:

Оцените статью