Folgende Klasse soll eine Erweiterung zu java.io.File sein. Mit einer Methode zum Kopieren. Die Methode versuche ich wie folgt in meinem Programm zu nutzen. Die Fehlermeldung ist: cannot find symbol: method: copyTo...
Java:
import java.io.*;
import java.nio.channels.FileChannel;
public class CopyableFile extends File
{
public CopyableFile(String pathname)
{
super(pathname);
}
public void copyTo(File destination)
{
enableSourceAndDestinationFilesToWriting(destination);
copyThisChannelContentIntoDestinationChannel(destination);
}
private void enableSourceAndDestinationFilesToWriting(File destination)
{
setWritable(true);
destination.setWritable(true);
}
private void copyThisChannelContentIntoDestinationChannel(File destination)
{
FileChannel sourceChannel = null, destinationChannel = null;
try
{
sourceChannel = new FileInputStream(this).getChannel();
destinationChannel = new FileOutputStream(destination).getChannel();
destinationChannel.transferFrom(sourceChannel, 0, sourceChannel.size());
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
closeChannels(sourceChannel, destinationChannel);
}
}
private void closeChannels(FileChannel... fileChannels)
{
for (FileChannel channel : fileChannels)
{
try
{
channel.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
}
Java:
File datei = new File( "xyz"),
kopie = new File( "abc");
datei.copyTo(kopie);