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 or directory that may or may not actually exist.
File myFile = new File( "C:\\temp\\abc.txt" ); File myDir = new File( "/home/fundir" );
new File ( ".", "C:/fileInRoot.txt" )if you try:
new File ( "myDir", "/otherDir" )you will get the same effect as:
File( "myDir/OtherDir" )not
File( "/OtherDir" );In other words, the file parameter has to be truly relative to the directory parameter. However, embedded /s like this will work:
new File ( "species", "clan/individual.txt" )and has the same effect as:
new File ( "species/clan/individual.txt" );
// if E:\mindprod is a directory, then these // four different ways of specifying it are // all equivalent in Windows. // You can use / or \ (coded as \\) // You can optionally have a trailing / or \. File dir1 = new File ( "E:\\mindprod" ); File dir2 = new File ( "E:\\mindprod\\" ); File dir3 = new File ( "E:/mindprod" ); File dir4 = new File ( "E:/mindprod/" );
If you need an URL or URI, you can use File.toURL or File.toURI.
File.createNewFile |
Atomically creates a new, empty file named if and only if a file with this name does not yet exist. Normally you don’t explicitly create the file. It is created as a side effect of writing to an OutputStream or Writer. |
File.exists |
does a file or directory of this name exist?. Beware of code like this:
String f = null; if ( new File( f + ".ext" ) .exists() ) ...will return true, because null.ext is considered to exist as the null device. In a similar way com1.txt is considered to exist as the COM1: port even when there is no such file. Avoid filenames of the form com?.* and lpt?.*. |
File.delete |
erase, remove, kill, destroy, unlink, and nuke a file. The File object is completely unaffected by deleting the file on disk, whether you do it with File.delete() or by any independent means. See also File.deleteOnExit. Can also be used to delete empty directories. delete() returns true if the file was successfully deleted, false if it did not exist, was already deleted or could not be deleted because it was in use or lack of sufficient privilege. |
File.renameTo |
renaming (or moving) a file. Note, it does not change the name inside the File object, just the name of the corresponding file on disk. You can’t use the File object to access the renamed file. You need a fresh one with the new name. |
File.length |
how many bytes are in this file. Coded as File.length() not File.length as you would with an array. |
File.canRead |
does this file exist and do you have permission to read it? |
File.canWrite |
does this file exist and do you have permission to write it? Returns false if the file does not exist, even though you probably could write such a file. Note that if it does not exist, you can write it, but canWrite will return false. setReadOnly lets you make a file read-only, but there is no way to undo the read-only status, other than my copying it or using exec. |
File.lastModified |
get last modified timestamp. setLastModified lets you change the lastModified date If you don’t have proper access permission, you will get -1 results. With Java 1.4 you can now set the date as well with File.setLastModified( long timestamp ). Windows keeps file timestamps accurate to 100 nanoseconds with 10,000 times as much precision as Java’s 1 ms. This means if you copy a file in Java, the new file might not have the exact same timestamp as the old. When you reconstitute a zipped file, its timestamp will be reconstituted accurate only to the even 2 seconds. |
File.isFile |
is there a file by this name? Is this file a true file rather than a directory? |
RandomAccessFile.setLength |
chop/grow this file to n bytes long. JDK 1.2+ only. You can’t use this directly on the File. You must create a RandomAccessFile object first with new RandomAccessFile( file, "rw" ); |
File.setReadOnly |
Lets you make a file read-only. There is no way to undo this in Java. |
File.setlastModified |
set last modified timestamp. This happens anyway automatically when you modify and close a file. You cannot read or access the last access date or the create date without using the FileTimes class, jni or exec. |
The easiest way to deal with this is to use an SQL database manager which can deal with concurrent updates.
| Windows | 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 |
|
| 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 |
![]() |
and suggestions to improve this page to Roedy Green : | ||
| Canadian Mind Products | |||
| mindprod.com IP:[65.110.21.43] | |||
| Your face IP:[38.103.63.17] | The information on this page is for non-military use only. | ||
| You are visitor number 204,728. | Military use includes use by defence contractors. | ||
| You can get a fresh copy of this page from: | or possibly from your local J: drive (Java virtual drive/Mindprod website mirror) | ||
| http://mindprod.com/jgloss/file.html | J:\mindprod\jgloss\file.html | ||