Java Reference
In-Depth Information
// Ensure that a filename (or something) was given in argv[0]
iif ( argv . length == 0 ) {
System . err . println ( "Usage: Creat filename" );
System . exit ( 1 );
}
for
for ( String a : argv ) {
// Constructing a File object doesn't affect the disk, but
// the createNewFile() method does.
new
new File ( a ). createNewFile ();
}
}
}
Renaming a File
Problem
You need to change a file's name on disk.
Solution
Use a java.io.File object's renameTo() method.
Discussion
For reasons best left to the gods of Java, the renameTo() method requires not the name you
want the file renamed to, but another File object referring to the new name. So to rename a
file you must create two File objects, one for the existing name and another for the new
name. Then call the renameTo method of the existing name's File object, passing in the
second File object. This is easier to see than to explain, so here goes:
public
public class
class Rename
Rename {
public
public static
static void
void main ( String [] argv ) throws
throws IOException {
// Construct the file object. Does NOT create a file on disk!
File f = new
new File ( "Rename.java~" ); // backup of this source file.
Search WWH ::




Custom Search