java中将文件复制到另一个路径,实际上就是通过io流把文件读取到另一路径下。而剪切则是复制完成后删除原文件,可用以下代码实现。
<pre name="code" class="java">/**
* @param origPath 需复制的文件路径
* @param newPath 复制到相应位置的文件路径
* @description 复制文件
*/
public static boolean copyFile(String origPath, String newPath){
File o = new File(origPath);
try{
if(o.isFile()){
copy(o, newPath,false);
}
}catch(Exception e){
e.printStackTrace();
}
return false;
}
/**
*
* @param origPath 需复制的文件夹名
* @param newPath 复制到文件夹名
* @return boolean 是否复制成功
* @description 复制文件夹
*/
public static boolean copyDirectiory(String origPath, String newPath){
File o = new File(origPath);
try{
if(o.isDirectory()){
copy(o, newPath,false);
}
}catch(Exception e){
e.printStackTrace();
}
return false;
}
/**
* @param origPath 需剪切的文件路径
* @param newPath 剪切到相应位置的文件路径
* @description 剪切文件
*/
public static boolean cutFile(String origPath, String newPath){
File o = new File(origPath);
try{
if(o.isFile()){
copy(o, newPath,true);
}
}catch(Exception e){
e.printStackTrace();
}
return false;
}
/**
*
* @param origPath 剪切制的文件夹名
* @param newPath 剪切到文件夹名
* @return boolean 剪切复制成功
* @description 剪切文件夹
*/
public static boolean cutDirectiory(String origPath, String newPath){
File o = new File(origPath);
try{
if(o.isDirectory()){
copy(o, newPath,true);
}
}catch(Exception e){
e.printStackTrace();
}
return false;
}
/**
* @param origPath 需复或剪切制的文件File对象
* @param newPath 需复或剪切到路径
* @param isCut 是否剪切
* @throws IOException
* @description 需复或剪切
*/
public static void copy(File o, String newPath,boolean isCut) throws IOException{
InputStream is = null;
OutputStream os = null;
try{
File n = new File(newPath);
File p = n.getParentFile();
//复制位置父目录是否存在,不存在则创建
if(!p.exists())
p.mkdirs();
//复制对象为文件夹
if(o.isDirectory()){
if(!n.exists())
n.mkdir();
//获取文件下所有子对象
File[] files = o.listFiles();
for (File file : files) {
String path = newPath;
if(!path.endsWith(File.separator))
path += File.separator;
path += file.getName();
copy(file, path,isCut);
}
}
//复制对象为文件
else{
if(!n.exists())
n.createNewFile();
is = new FileInputStream(o);
os = new FileOutputStream(n);
byte[] b = new byte[1024];
while(is.read(b) != -1){
os.write(b);
}
os.flush();
}
}finally{
try {
if(is != null){
is.close();
}
if(os != null){
os.close();
}
//若isCut为true,复制完成对象后删除原对象
if(isCut){
o.delete();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* @param origPath 需复或剪切制的文件路径
* @param newPath 需复或剪切到路径
* @param isCut 是否剪切
* @throws IOException
* @description 需复或剪切
*/
public static void copy(String origPath, String newPath,boolean isCut) throws IOException{
InputStream is = null;
OutputStream os = null;
File o = new File(origPath);
try{
File n = new File(newPath);
File p = n.getParentFile();
//复制位置父目录是否存在,不存在则创建
if(!p.exists())
p.mkdirs();
//复制对象为文件夹
if(o.isDirectory()){
if(!n.exists())
n.mkdir();
//获取文件下所有子对象
File[] files = o.listFiles();
for (File file : files) {
String path = newPath;
if(!path.endsWith(File.separator))
path += File.separator;
path += file.getName();
copy(file.getPath(), path,isCut);
}
}
//复制对象为文件
else{
if(!n.exists())
n.createNewFile();
is = new FileInputStream(o);
os = new FileOutputStream(n);
byte[] b = new byte[1024];
while(is.read(b) != -1){
os.write(b);
}
os.flush();
}
//若isCut为true,复制完成对象后删除原对象
if(isCut)
o.delete();
}finally{
try {
if(is != null){
is.close();
}
if(os != null){
os.close();
}
//若isCut为true,复制完成对象后删除原对象
if(isCut){
o.delete();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
以上代码实现多种方式复制或剪切文件或文件夹。