代码如下:
A.文件内容拷贝
package demo;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Writer;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
public class FileCopyExample ...{
public static void main(String[] args) ...{
try ...{
File src = new File("test.dat");
File dest = new File("test.dat.bak");
FileUtils.copyFile(src, dest);
} catch (IOException ioe) ...{
System.out.println("Problem copying file.");
}
try ...{
File src = new File("test.dat");
File dir = new File("./temp");
FileUtils.copyFileToDirectory(src, dir);
} catch (IOException ioe) ...{
System.out.println("Problem copying file to dir.");
}
try ...{
String string = "Blah blah blah";
File dest = new File("test.tmp");
FileUtils.writeStringToFile(dest, string, "ISO-8859-1");
} catch (IOException ioe) ...{
System.out.println("Error writing out a String.");
}
try ...{
Writer writer = new FileWriter("test.dat");
InputStream inputStream = FileCopyExample.class.getClass()
.getResourceAsStream("/demo/web.xml");
IOUtils.copy(inputStream, writer);
writer.close();
inputStream.close();
} catch (IOException e) ...{
System.out.println("Error copying data");
}
try ...{
File src = new File("test.txt");
OutputStream output = new FileOutputStream(src);
InputStream inputStream = FileCopyExample.class.getClass()
.getResourceAsStream("/demo/web.xml");
IOUtils.copy(inputStream, output);
// writer.close();
inputStream.close();
output.close();
} catch (IOException e) ...{
System.out.println("Error copying data");
}
}
}