A
Andreas.K
Gast
tag,
kann ich in Java mit einem art "copy" befhel beliebige Datei-Typen kopieren??
Creez Andreas.
kann ich in Java mit einem art "copy" befhel beliebige Datei-Typen kopieren??
Creez Andreas.
public void copy(String src, String dest) throws IOException{
File inputFile = new File(src);
File outputFile = new File(dest);
FileInputStream in = new FileInputStream(inputFile);
FileOutputStream out = new FileOutputStream(outputFile);
FileChannel inc=in.getChannel();
FileChannel outc=out.getChannel();
inc.transferTo(0,inc.size(),outc);
inc.close();
outc.close();
in.close();
out.close();
}
public void Copy() throws IOException{
String src="C:\temp\text.txt";
String dest="C:\temp\test\text.txt";
File inputFile = new File(src);
File outputFile = new File(dest);
FileInputStream in = new FileInputStream(inputFile);
FileOutputStream out = new FileOutputStream(outputFile);
FileChannel inc=in.getChannel();
FileChannel outc=out.getChannel();
inc.transferTo(0,inc.size(),outc);
inc.close();
outc.close();
in.close();
out.close();
}
import java.io.*;
public class CopyFile{
public static void main(String[] args) throws IOException{
File file = new File("C:/Temp/test.txt");
File directory = new File("C:/Temp/output");
copyFile(file, directory);
}
public static void copyFile(File file, File dir)throws IOException{
File destFile = new File(dir, file.getName());
//4K Blöcke
byte[] b = new byte[4096];
FileInputStream fis = null;
FileOutputStream fos = null;
try{
fis = new FileInputStream(file);
fos = new FileOutputStream(destFile);
int l = -1;
while((l = fis.read(b)) != -1){
fos.write(b, 0, l);
}
}finally{
if(fis != null)
fis.close();
if(fos != null){
fos.flush();
fos.close();
}
}
}
public void copy() throws IOException {
// mein code
}
// aufrufende Methode
try {
copy();
}
catch (IOException e) {
e.printStackTrace();
}
public void jButton1ActionPerformed(ActionEvent evt)
{
File file = new File("C:/Temp/text.txt");
File directory = new File("C:/Temp/test");
copyFile(file, directory);
}
public void copyFile(File file, File dir)throws IOException
{
...
}
public static void copyFile(File file, File dir)throws IOException
{
...
}
public void copyFile(File file, File dir)throws IOException
{
...
}
Niki hat gesagt.:Kann sein dass es nicht so gut performt, aber es tut das was es tun soll.
Andreas.K hat gesagt.:Wenn ich so so verwende:
public static void copyFileToDirectory(java.io.File C:\temp\text.txt,
java.io.File C:\temp\test)
throws java.io.IOException
kommt folgender Fehler:
CopyFileToDirectory.java:26:54: ')' expected
public static void copyFileToDirectory(java.io.File C:\temp\text.txt,
try {
FileUtils.copyFileToDirectory(new File("C:\\temp\\test.txt"), new File("C:\\temp\\test"));
} catch (IOException e) {
e.printStackTrace();
}