package com.mindprod.example;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.Date;
import static java.lang.System.*;
/**
* example use of java.io.File.
*
* @author Roedy Green, Canadian Mind Products
* @version 1.0 2009-01-01 initial version
* @since 2009-01-01
*/
public final class TestFile
{
/**
* Test the various ways you can get the file name.
*
* @throws java.io.IOException on I/O failure.
*/
private static void testFileFileManipulationMethods() throws IOException
{
out.println( "Testing File name-getting methods." );
out.println(
"There must exist a TEST file called E:\\genus\\species\\crocodile.html" );
final File file = new File( "E:\\genus\\species\\crocodile.html" );
if ( file.exists() )
{
out.println( "file exists" );
}
out.println( "file is " + file.length() + " bytes long" );
File dir = new File( "E:\\genus\\species" );
String[] files = dir.list();
out.println( "Files in this directory are:" );
for ( String afile : files )
{
out.println( afile );
}
out.println( "file "
+ ( file.canRead() ? "can" : "cannot" )
+ " be read" );
out.println( "file "
+ ( file.canWrite() ? "can" : "cannot" )
+ " be written" );
out.println( "last modified: " + new Date( file.lastModified() ) );
out.println( "file "
+ ( file.isDirectory() ? "is" : "is not" )
+ " a valid existing directory" );
out.println( "file "
+ ( file.isFile() ? "is" : "is not" )
+ " a valid existing file" );
File[] roots = File.listRoots();
for ( File root : roots )
{
out.println( root );
}
File dirs = new File( "C:\\temp\\silly\\preposterous" );
out.println( "success in creating directory tree: "
+ dirs.mkdirs() );
File dirOneLevel = new File( "C:\\temp\\absurd" );
out.println( "success in creating one directory level: "
+ dirOneLevel.mkdirs() );
RandomAccessFile raf =
new RandomAccessFile( new File(
"E:\\genus\\species\\rabbit.html" ), "rw" );
raf.setLength( 50 );
file.setReadOnly();
file.setLastModified( System.currentTimeMillis() - ( 24
* 60
* 60
* 1000 ) );
}
/**
* Test the various ways you can get the file name.
*
* @throws java.io.IOException on I/O failure.
*/
private static void testFileNameGettingMethods() throws IOException
{
out.println( "Testing File name-getting methods." );
out.println(
"There must exist a TEST file called E:\\genus\\species\\crocdile.html" );
out.println( "The current directory must be E:\\genus" );
File currentDirectory = new File( "." );
out.println( "dir: " + currentDirectory.getCanonicalPath() );
out.println( "Using relative name" );
File f = new File( "species/CROCODILE.HTML" );
out.println( "toString: " + f.toString() );
out.println( "absolutePath: " + f.getAbsolutePath() );
out.println( "name: " + f.getName() );
out.println( "path: " + f.getPath() );
out.println( "parent: " + f.getParent() );
out.println( "canonicalPath: " + f.getCanonicalPath() );
out.println( "Using absolute name" );
f = new File( "E:\\genus\\species\\CROCODILE.HTML" );
out.println( "toString: " + f.toString() );
out.println( "absolutePath: " + f.getAbsolutePath() );
out.println( "name: " + f.getName() );
out.println( "path: " + f.getPath() );
out.println( "parent: " + f.getParent() );
out.println( "canonicalPath: " + f.getCanonicalPath() );
out.println( "Using relative name with drive" );
f = new File( "e:species\\CROCODILE.HTML" );
out.println( "absolutePath: " + f.getAbsolutePath() );
out.println( "name: " + f.getName() );
out.println( "parent: " + f.getParent() );
out.println( "path: " + f.getPath() );
out.println( "canonicalPath: " + f.getCanonicalPath() );
}
/**
* Test temporary files
*
* @throws java.io.IOException on I/O failure.
*/
private static void testTemporaryFiles() throws IOException
{
File temp = File
.createTempFile( "temp_", ".tmp", new File( "C:\\temp" ) );
temp.deleteOnExit();
out.println( "Success of delete: " + temp.delete() );
}
/**
* Exercise various File methods
*
* @param args not used
*
* @throws java.io.IOException on I/O failure.
*/
public static void main( String[] args ) throws IOException
{
testFileFileManipulationMethods();
testFileNameGettingMethods();
testTemporaryFiles();
}
}