import java.io.*;
import java.nio.channels.FileChannel;
public class FileCopy
{
public static void main( String[] args ) throws IOException
{
if( 2 != args.length )
System.out.println( "Error: Parameter missing:\n java FileCopy InputFile OutputFile\n" );
else
copy( args[0], args[1], false, false );
}
// Seit Java JDK 1.4 bevorzugte und schnellere Methode
public static void copySinceJava14( File fin, File fout, boolean append ) throws IOException
{
FileChannel inChannel = new FileInputStream( fin ).getChannel();
FileChannel outChannel = new FileOutputStream( fout, append ).getChannel();
try {
inChannel.transferTo( 0, inChannel.size(), outChannel );
} finally {
if( inChannel != null ) try { inChannel.close(); } catch( IOException ex ) {}
if( outChannel != null ) try { outChannel.close(); } catch( IOException ex ) {}
}
}
// Bis Java JDK 1.3 zu verwendende Methode
public static void copyUptoJava13( File fin, File fout, boolean append ) throws IOException
{
int len = 32768;
byte[] buff = new byte[(int) Math.min( len, fin.length() )];
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream( fin );
fos = new FileOutputStream( fout, append );
while( 0 < (len = fis.read( buff )) )
fos.write( buff, 0, len );
} finally {
try { if( fos != null ) fos.close(); } catch( IOException ex ) {}
try { if( fis != null ) fis.close(); } catch( IOException ex ) {}
}
}
// Zusaetzliche Fehlerueberpruefungen
public static void copy( String fromFileName, String toFileName,
boolean overwriteIfExists, boolean append ) throws IOException
{
File fromFile = new File( fromFileName );
File toFile = new File( toFileName );
if( !fromFile.exists() )
throw new IOException( "Fehler: Quelldatei fehlt: " + fromFileName );
if( !fromFile.isFile() )
throw new IOException( "Fehler: Quelle ist ein Verzeichnis: " + fromFileName );
if( !fromFile.canRead() )
throw new IOException( "Fehler: Keine Leseberechtigung fuer Quelldatei: " + fromFileName );
if( toFile.isDirectory() )
toFile = new File( toFile, fromFile.getName() );
if( toFile.exists() ) {
if( !overwriteIfExists && !append )
throw new IOException( "Fehler: Zieldatei existiert bereits: " + toFileName );
if( !toFile.canWrite() )
throw new IOException( "Fehler: Keine Schreibberechtigung fuer Zieldatei: " + toFileName );
} else {
String parent = toFile.getParent();
if( parent == null )
parent = System.getProperty( "user.dir" );
File dir = new File( parent );
if( !dir.exists() )
throw new IOException( "Fehler: Zielverzeichnis existiert nicht: " + parent );
if( dir.isFile() )
throw new IOException( "Fehler: Ziel ist kein Verzeichnis: " + parent );
if( !dir.canWrite() )
throw new IOException( "Fehler: Keine Schreibberechtigung fuer Zielverzeichnis: " + parent );
}
copySinceJava14( fromFile, toFile, append );
}
}
public class GetFilesInDir
{
public static void main( String[] args )
{
System.out.println( "Show files in directory.\r\nPut directory path as commandline parameter.\r\n" );
if( null == args || 1 != args.length ) return;
System.out.println( GetFilesInDir.getFilesInDirToString( args[0], null, null, null ) );
}
public static String getFilesInDirToString(
String sDir, String sSpacesStart, String sSpacesAdd, String sLineSeparator )
{
if( null == sLineSeparator ) sLineSeparator = System.getProperty( "line.separator" );
if( null == sSpacesStart ) sSpacesStart = "";
if( null == sSpacesAdd ) sSpacesAdd = " ";
if( null == sDir || 0 >= sDir.trim().length() )
return sLineSeparator + "Error: Invalid function parameter!" + sLineSeparator;
java.io.File dir = new java.io.File( sDir );
java.io.File[] entries = dir.listFiles();
if( null == entries )
return sLineSeparator + "'" + sDir + "' is not a valid directory path!" + sLineSeparator;
if( 0 >= entries.length )
return sSpacesStart + sDir + " does not contain any files or directories!" + sLineSeparator;
StringBuffer sb = new StringBuffer( "" );
for( int i=0; i<entries.length; i++ )
if( !entries[i].isDirectory() )
sb.append( sSpacesStart ).append( entries[i] ).append( sLineSeparator );
for( int i=0; i<entries.length; i++ )
if( entries[i].isDirectory() )
sb.append( sLineSeparator )
.append( sSpacesStart )
.append( "Verzeichnis " )
.append( entries[i].toString() )
.append( sLineSeparator )
.append( getFilesInDirToString( entries[i].toString(),
sSpacesStart + sSpacesAdd, sSpacesAdd, sLineSeparator ) );
return sb.toString();
}
}
Ab Java 7 gibt es weitere File-I/O-Klassen und -Methoden, die vieles vereinfachen, zum Beispiel: FileSystems.getDefault(), Path.getFileSystem(), Path.deleteIfExists(), Path.copyTo(), Path.moveTo(), Path.newDirectoryStream(), Files.walkFileTree(), FileVisitor, BasicFileAttributes, WatchService, Path.register(), Path.checkAccess(), AsynchronousFileChannel (siehe bei OpenJDK oder Oracle).
Beachten Sie, dass es die Klasse FileSystem ab Java 7 doppelt gibt: als abstrakte (normalerweise nicht sichtbare) java.io.FileSystem und als java.nio.file.FileSystem.