方法一:
public void copy(String source,String destination){
InputStream in = null;
OutputStream out = null;
try {
in = new FileInputStream(source);
out = new FileOutputStream(destination);
int length = in.available();
int len = (length % 1024 == 0) ? (length / 1024) : (length / 1024 + 1);
byte[] temp = new byte[1024];
for (int i = 0; i < len; i++) {
length = in.read(temp);
out.write(temp,0,length);
}
}catch(Exception e){
e.printStackTrace();
} finally {
try{
if (in != null){
in.close();
}
if (out != null){
out.close();
}
}catch(Exception e){
}
}
}
方法二:
public void copy(String source,String destination){
Runtime.getRuntime().exec("cmd.exe /c copy " + source + "," + destination);
}
方法三:
public static void copy(String src,String dist){
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream(src);
fos = new FileOutputStream(dist);
byte[] buf = new byte[1024];
int len = 0;
while((len = fis.read(buf)) != -1){
fos.write(buf,0,len);
}
} catch (Exception e) {
e.printStackTrace();
}finally{
if(fis != null){
try {
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(fos != null){
try {
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
java文件拷贝
最新推荐文章于 2024-07-24 19:35:58 发布