Java IO - Part 3: File IO - 2

Gökhan Kanber
3 min readFeb 13, 2021
Milky Way

This article is a part of the Java Programming Language article series.

Contents

  • Read, Write, and Create File
  • Random Access Files
  • Create and Read Directories
  • Links, Symbolic or Otherwise
  • Other Useful Methods
  • Legacy File IO

Reading, Writing, and Creating Files

Methods By Complexity
Methods By Complexity

The OpenOption Parameter

  • An optional parameter
  • The StandardOpenOption enum

StandardOpenOption

  • READ: Open for read access
  • WRITE: Opens the file for write access
  • APPEND: Appends the new data to the end of the file, used with the WRITE or CREATE options
  • TRUNCATE_EXISTING: Truncates the file to zero bytes, used with the WRITE option
  • CREATE_NEW: Creates a new file and throws an exception if the file already exists
  • CREATE: Opens the file if it exists or creates a new file if it does not
  • DELETE_ON_CLOSE: Deletes the file when the stream is closed, useful for temporary files
  • SPARSE: Hints that a newly created file will be sparse
  • SYNC: Keeps the file (both content and metadata) synchronized with the underlying storage device
  • DSYNC: Keeps the file content synchronized with the underlying storage device

For Small Files

Reading All Bytes

Writing All Bytes

Buffered IO Methods for Text Files

Reading a File

Writing a File

Unbuffered Streams and Interoperable with java.io APIs

Reading a File

Writing a File

Channels and ByteBuffers

  • Reads a buffer at a time

Reading a File

Writing a File

Creating Files

The newOutputStream method is another option

Creating Temporary Files

  • The format of the temporary file name is platform specific

Random Access Files

The SeekableByteChannel interface

  • position: returns the channel’s current position
  • position(long): sets the channel’s position
  • read(ByteBuffer): reads bytes into the buffer from the channel
  • write(ByteBuffer): writes bytes from the buffer to the channel
  • truncate(long): truncates the file (or other entity) connected to the channel

Creating and Reading Directories

Listing a File System’s Root Directories

Creating a Directory

Creating a Temporary Directory

Listing a Directory’s Contents

Filtering a Directory Listing By Using Globbing

Glob

  • A pattern specified as a string

Glob syntax

  • An asterisk (*): matches any number of characters (including none)
  • Two asterisks (**): works like * but crosses directory boundaries, used for matching complete paths
  • A question mark (?): matches exactly one character
  • Braces ({}): specify a collection of subpatterns
  • Square brackets ([]): convey a set of single characters or, when the hyphen character (-) is used, a range of characters ([aeiou], [a-z,A-Z]), within the square brackets, *, ?, and \ match themselves
  • To match *, ?, or the other special characters, use the backslash character (\\, \?)

Writing Your Own Directory Filter

Links, Symbolic or Otherwise

Creating a Symbolic Link

Creating a Hard Link

Detecting a Symbolic Link

Finding the Target of a Link

Other Useful Methods

Determining MIME Type

Default File System

  • The getDefault method

Path String Separator

File System’s File Stores

  • File store: mounted file system (UNIX), volume (Windows)

Legacy File IO

Drawbacks

  • Many methods didn’t throw exceptions when they failed
  • If a file deletion failed, the cause may not be determined: the file didn’t exist or the user didn’t have permissions or another problem
  • The rename method didn’t work consistently across platforms
  • There was no real support for symbolic links
  • Accessing file metadata was inefficient
  • Many of the File methods didn’t scale: Requesting a large directory listing over a server could result in a hang, also cause memory resource problems
  • It was not possible to write reliable code that could recursively walk a file tree

Interoperability With Legacy Code

  • The toPath method: converts java.io.File to java.nio.file.Path
  • The toFile method: converts java.nio.file.Path to java.io.File

Mapping java.io.File Functionality to java.nio.file

Mapping for Legacy File IO to NIO.2
Mapping for Legacy File IO to NIO.2

--

--