Home : Course Map : Chapter 9 : Java :
The File Class
Course Map
Chapter 9

Console I/O
  Text Output 
     Demo 1

     Demo 2

  Tex 2t Input
     Demo 3

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
Object I/O
Types to Bytes
Stream Filters
Other I/O Topics

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
     Topic Index
     Course Guide
     What's New

Files and directories are accessed and manipulated via the java.io.File class. The File class does not actually provide for input and output to files. It simply provides an identifier of files and directories.

Always remember that just because a File object is created, it does not mean there actually exists on the disk a file with the identifier held by that File object.

The File class includes several overloaded constructors. For example, the following instance of File refers to a file named myfile.txt in the current directory of the program that the JVM is running:

File file = new File ("myfile.txt");

Again, the file myfile.txt may or may not exist in the file system. An attempt to use File object that refers to a file that does not exist will cause a FileNotFoundException to be thrown.

The File instance can also created with a filename that includes path:

File fileA = new File("/tmp/filea.txt");

Another overloaded constructor allows separate specification of the path and the file name:

  File fileA = new File("/tmp", "filea.txt");

Directories can also be specified:

  File direc = new File("/tmp");

There are a number of useful methods in File, e.g.:

Boolean exist();       - does the file exist
Boolean canWrite();    - can the file be written to
Boolean canRead();     - can the file be read
Boolean isFile();      - does it represent a file
Boolean isDirectory(); - or a directory

There are also methods to get the file name and path components, to make a directory, to get a listing of the files in a directory, etc.

String getName () - get the name of the file (no path included)
String getPath () - get the abstract file path
String getCanonicalPath () - get the name of the file with path
String getAbsolutePath () - get the absolute file path

Note that path names use different separator characters on different hosts. Windows uses "\", Unix"/", Macintosh ":". The static variables:

File.separator - string with file separator
File.separatorChar - char with file separator
- string with path separator
- char with path separator

can be used to insure that your programs are platform independent. For example, this snippet shows how to build a platform independent path:

String dirName = "dataDir";
String filename = "data.dat";
File filData = new File(dirName + File.separator + filename);

Other talents of the File class include the method

boolean mkdir ()

This method will create a directory with the abstract path name represented by the File object if that File object represents a directory. The method returns true if the directory creation succeeds and false if not. A situation in which the directory cannot be created is, for example, when the File object refers to an actual file that already exists in the file system.

References & Web Resources

  • File - class specs


Latest update: Nov. 12, 2004

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

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

  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
22 23 24

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.