当前位置: 代码迷 >> Web前端 >> 复制文件到工程指定目录上
  详细解决方案

复制文件到工程指定目录上

热度:343   发布时间:2012-11-22 00:16:41.0
复制文件到工程指定目录下

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class CopyPicture {
private static boolean copyFileUnit(String aimfile, String path) {
File file = new File(aimfile);
if (file.exists()) {
String fileName = file.getName();
File myfile = new File(path);
if (myfile.exists()) {
try {
InputStream is = new FileInputStream(file);
OutputStream os = new FileOutputStream(new File(path,
fileName));
byte[] buffer = new byte[600];
int length = 0;
try {
while ((length = is.read(buffer)) != -1) {
os.write(buffer, 0, length);
}
os.close();
is.close();
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
} else {
myfile.mkdir();
System.out.println("创建目录,开始复制.");
try {
InputStream is = new FileInputStream(file);
OutputStream os = new FileOutputStream(new File(path,
fileName));
byte[] buffer = new byte[600];
int length = 0;
try {
while ((length = is.read(buffer)) != -1) {
os.write(buffer, 0, length);
}
os.close();
is.close();
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
} else {
System.out.println("该文件不存在,复制失败!");
return false;
}
return true;
}



public static void main(String[] args) {
String str = "D:/REC/4/resource/images/start.jpg";
String path = "F:/project/teach/WebRoot/images/3";
if (CopyPicture.copyFileUnit(str, path)) {
System.out.println("复制成功!!");
}
}
}
  相关解决方案