Java Glossary : File

CMP home Java glossary home Menu no menu Last updated 2004-06-28 by Roedy Green ©1996-2004 Canadian Mind Products

Java definitions: 0-9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

You are here : home : Java Glossary : F words : File.

Files & Directories
Without even opening the file, File lets you find out about a file or directory, and lets you rename or delete it.

Because java.io.File can represent a file, wildcard, or directory node, this class has many more uses than its name would suggest. File is a misnomer, because File does not represent a file at all, but rather the name of a file that may or may not actually exist.

File myFile = new File( "C:\\temp\\abc.txt" );

File myDir = new File( "/home/fundir" );

gets you access to a file. You feed it a filename or directory name suitable for the native platform, not a URL. On windows, you can use / and \ interchangeably. \ has to be doubled because it is also the Java escape character.

Methods For Manipulating Filenames and Files

Hints

File and Record Locking

Java has no features for file or record locking. You can kludge them by creating a dummy file whenever you are using the main file, and deleting it when you are done. If all threads and processes using the file do likewise, and sleep while the dummy semaphore file is present, waking periodically to see if it has disappeared, you can arrange exclusive access. Other than that, you would have to use JNI to hook into the OS's native file locking scheme.
Peter van der Linden's FAQ for more detail on file locking

The easiest way to deal with this is to use an SQL database manager which can deal with concurrent updates.

Platform Specific Filenames

Every platform uses a different filename format. Your Java apps will deal directly with those filenames. You have only three tools to help you:

static String lineSeparator = System.getProperty ( "line.separator" );
File.separatorChar
File.pathSeparatorChar

You are safest to build up filenames programmatically like this:

/**
 * Build a platform independent filename out of pieces
 *
 * @param drvLetter Drive letter or 0 (not '0') for none.
 *
 * @param path directories possibly containing embedded / or \
 *
 * @param fileName name of the file, possibly containing an embedded .
 */
String makeQualName(char drvLetter, String path, String fileName)
   {
   // assume drive letter is separated by :
   return(drvLetter == 0 ? "" : drvLetter + ":" )
   + File.separatorChar
   + path
   + File.separatorChar
   + fileName;
   } // makeQualName

For WORA, it is best to use very conservative file naming conventions:

Anything else is bound to confuse some OS somewhere.
Win9x/NT Linux Apple iMac Groupe Bull GCOS 8
(JDK 1.1.6 beta)
lineSeparator "\r\n" "\n" "\r" "\n"
separatorChar '\\' '/' ':' '/'
pathSeparatorChar ';' ':' N/A ':'
absolute "C:\\myDir\\myFile.txt" "/usr/local/my.Dir/my.File.txt" "::myVolume:myDir:file.txt" "gcosuser/catalog/file.txt"
relative "myDir\\myFile.txt" "my.Dir/my.File.txt" "myDir:file.txt" "/catalog/file.txt"
(subordinate to the current GCOS 8 userid)
root "C:\\." "/." "::myVolume:" N/A
parent "..\\." "../." File.getParent() N/A
case sensitive no yes no no
charset A-Z a-z 0-9 accented - _ ~ ! @ # $ A-Z a-z 0-9 accented - _ ~ ! @ # $ Almost anything, Full Unicode, even control chars a-z 0-9 - _ .
avoid . : ; * @ % ^ & ( ) [ ] { } " ' < > ? + = / | : ; * @ % ^ & ( ) [ ] { } " ' < > ? + = / | : (. at beginning) ; * @ % ^ ( ) " ' < > ? + = / | : ; * @ % ^ & ( ) [ ] { } " ' < > ? + = / | ~ ! @ # $ accented
zip and jar files       These files types are treated as a "container database" for a collection of Java class files and other resources. The pathnames stored within these files are not case sensitive and can be arbitrarily long.

GCOS 8 supports read-access to these files.

restrictions      
  1. Each directory or file component must be less than or equal to 12 characters.
  2. Both "." and ".." can be valid file names.
  3. The construct "." does not refer to the current directory.
  4. The construct ".." does not refer to the parent directory.
GCOS is a mainframe JVM implementation without GUI. It runs on mainframes that use 36 bit words with non-IEEE floating point hardware. It has its roots in GE's GECOS, was later taken over by Honeywell, and had been run by the French Company, Groupe Bull since 1989. It is included mainly to show you just how different a platform can be and still run Java apps unmodified.

Console I/O

The console i/o methods are in System, e.g.
System.in.read(); read a byte
System.err.print() print a value
System.err.println(); print a value followed by a newline
System.out.print(); print a value
System.out.println(); print a value followed by a newline.
System.setIn(); redirect stdin to a file
System.setErr(); redirect stderr to a file
System.setOut(); redirect stdout to a file

Applets

Unsigned Applets cannot read files on the local hard disk. The java.io.File class won't let you read or write files on some other remote machine, e.g. Applets can't read or write files on the server, even if they are signed.

binary file formats ¤ File I/O Amanuensis to teach you how to do all manner of file, in RAM and remote I/O ¤ filemon to monitor file activity ¤ filter ¤ I/O ¤ JConfig ¤ LEDataStream for little-endian i/o ¤ nio ¤ pipe ¤ println formats ¤ redirection ¤ remote file access ¤ StreamTokenizer ¤ StringTokenizer ¤ UnifiedIO


CMP logo
CMP_home
home
Canadian Mind Products CSS
HTML Checked!
ICRA ratings logo
mindprod.com IP:[24.87.56.253]
Your IP:[80.134.30.163]
You are visitor number 13664.
Please send errors, omissions and suggestions
to improve this page to Roedy Green.
You can get a fresh copy of this page from: or possibly from your local J: drive mirror:
http://mindprod.com/jgloss/file.html J:\mindprod\jgloss\file.html