7. File Access

Introduction

// Following import is assumed throughout this section.
import java.io.*;

try {
    BufferedReader input = new BufferedReader(
        new FileReader("/usr/share/dict/words"));
    String line;
    while ((line = input.readLine()) != null)
        if (line.indexOf("blue") != -1)
            System.out.println(line);
    input.close();
} catch (IOException ex) {
    System.err.println("Error occured");
}

Opening a File

Opening Files with Unusual Filenames

Expanding Tildes in Filenames

Making Perl Report Filenames in Errors

Creating Temporary Files

try {
    File temp = File.createTempFile("prefix", ".suffix");
    temp.deleteOnExit();
} catch (IOException ex) {
    System.err.println("File could not be created");
}


// vim: set sw=4 : set tw=85 :

Storing Files Inside Your Program Text

Writing a Filter

Modifying a File in Place with Temporary File

Modifying a File in Place with -i Switch

Modifying a File in Place Without a Temporary File

Locking a File

Flushing Output

Reading from Many Filehandles Without Blocking

Doing Non-Blocking I/O

Determining the Number of Bytes to Read

Storing Filehandles in Variables

Caching Open Output Filehandles

Printing to Many Filehandles Simultaneously

Opening and Closing File Descriptors by Number

Copying Filehandles

Program: netlock

Program: lockarea