Home : Course Map : Chapter 9 : Java :
File IO
JavaTech
Course Map
Chapter 9

Introduction
Overview
Streams
Wrappers,Buffers
Console I/O
  Text Output 
     Demo 1

  Formatter/printf()
     Demo 2

  Tex 2t Input
     Demo 3

  Scanner
     
Demo 4
File Class
  File I/O
  File Output-Text
     Demo 5

  Formatter to File
     Demo 6

  File Input - Text
    Demo 7

  Scanner - Files
     Demo 8

  File I/O - Binary
     Demo 9
   Demo 10
File Chooser Dialog
  Demo 11

Character Codes
  Demo 12
   Demo13
Object I/O
Types to Bytes
Stream Filters
Other I/O Topics
Exercises

    Supplements
Character I/O
  Demo 1   Demo 2
Random Access
  Demo 3
ZIP/GZIP Streams
  Demo 4
Piped Streams
  Demo 5
NIO Framework
More NIO
  Demo 6

     About JavaTech
     Codes List
     Exercises
     Feedback
     References
     Resources
     Tips
     Topic Index
     Course Guide
     What's New

The base file stream classes in java.io package are

FileInputStream   - binary file input base class
FileOutputStream - binary file output base class

FileReader             - read text files
FileWriter             - write to text files

These classes treat a file as the source of the stream. Each of these classes has several overloaded constructors, including constructors that take a string file name as the parameter and constructors that take File as a parameter. For example, an input stream from a file can be created with

File file_in = new File ("data.dat");
FileInputStream in = new FileInputStream (file_in);

If the file doesn't exist, this will throw an an instance of FileNotFoundException.

Usually, the FileInputStream object is wrapped with another stream to obtain greater functionality. For example, BufferedInputStream is used to smooth out the data flow by filling a buffer that sends the data when it is full. This class also includes skip(), mark(), and reset() methods. The reset operation causes the data from the point where mark() was invoked to be reread.

Similarly, output streams to files are opened like:

File file_out = new File ("tmp.dat");
FileOutputStream out = new FileOutputStream (file_out);

If the file doesn't exist, it will be created.

Typically, output streams to files are wrapped with one or more other streams to obtain greater functionality such as BufferedOutputStream.

To create a directory use the mkdir() method in File class.

To append to an existing file use the overloaded constructor:

File file_out = new File ("old.dat");
FileOutputStream out = new FileOutputStream (file_out,true);

where the second argument indicates that the file should be opened for appending. (RandomAccessFile can also be used for appending.)

References & Web Resources

 

Latest update: Apr.17, 2006

              Tech
Histogram I/O
Hist I/O - Get/Set
  Demo 1
Hist I/O - Objects
  Demo 2
HistogramStream
  Demo 3
Filtering Data
  Demo 4
Exercises

           Physics
Physics Model
Simulation Design
Physics Simulator
  Demo 1
Experiment Design
Experiment Sim.
  Demo 2
Analysis
Expt. + Analysis
  Demo 3
Exercises

  Part I Part II Part III
Java Core 1  2  3  4  5  6  7  8  9  10  11  12 13 14 15 16 17
18 19 20
21
22 23 24
Supplements

1  2  3  4  5  6  7  8  9  10  11  12

Tech 1  2  3  4  5  6  7  8  9  10  11  12
Physics 1  2  3  4  5  6  7  8  9  10  11  12

Java is a trademark of Sun Microsystems, Inc.