Java Reference
In-Depth Information
Java has no concept of a record, you have to break the record apart into its compo-
nent fields. The String constructor that converts portions of a byte array into a
string illustrates one method of doing this.
T HE F ILE C LASS
The File class is an abstraction of an entry in the file system of the underlying op-
erating system. A File instance can represent either a file or a directory. The File
has many useful methods for inquiring about and manipulating the file system.
Here are some valid file names using the various conventions:
C:\documents\mybook\chapterone.doc (Windows)
/home/account/documents/mybook/chapterone.doc (Unix)
\\mymachine\myshared_drive\documents\mybook\chapterone.doc (UNC under Windows)
Java and the JVM often will deal with these differences as transparently as pos-
sible. With most JVMs, you can instantiate a file called ./documents/mybook/chap-
terone.doc on a Windows system or a UNIX system and have the same behavior. If
you need to be able to run on different platforms, the more correct way of doing
this would be as follows:
String myFilename = File.separator + "documents" + File.separator +
"mybook" + File.separator + "chapterone.doc";
File.separator is a static String variable in the File class that will be set cor-
rectly for the operating system in which the JVM is running. The JVM on Windows
will set the File.separator to a backslash (\); the JVM on UNIX will set it to a for-
ward slash (/). So myFileName would equate to \documents\mybook\chapterone.doc
on Windows and /documents/mybook/chapterone.doc on UNIX. The File.separa-
torChar is a static char value that acts the same way.
You have four ways to create a new File instance.
File myFile = new File(String pathName);
File myFile = new File(File parent, String child);
File myFile = new File(String parent, String child);
File myFile = new File(URI uri);
The first way takes a string that defines the file or directory as a full or relative
pathname. The second and third ways require a parent and a child name. The par-
ent is the directory name where the child resides. The child may be either a file or a
Search WWH ::




Custom Search