java 语言编写文件复制粘贴,主要是IO流操作,以下都是我自己认知写的,如果有错误请谅解(java码龄一年)
文件复制过程有很多种,通过编写的难度进行分层(这只是我个人习惯分层)
- 一个文件或压缩包的复制粘贴(注意,不是文件夹)
- 将一个文件打包成压缩包
- 单级目录的复制粘贴(就是复制一个文件夹,该文件夹内没有其他的文件夹只有一个或多个文件)
- 将单击目录复制出来并打包成压缩包
- 多级目录的复制粘贴(文件夹内有其他文件夹)
- 将多级目录复制出来并打包成压缩包
我直接上代码并解释我的理解
一、先是第一个简易复制粘贴
import java.io.*;
/**
* 文件复制粘贴过程,一般是某某文件复制到某某文件夹内,所以复制文件路径是包括该文件的(要加上后缀),而粘贴文件路径是只到某目录
* @param copyPath :复制文件的绝对路径,一定要有后缀 如: "C:\\text\\新建文件夹\\文件名.txt" 不一定是txt格式的其他格式也可以,包括音频,视频,照片,只要它们可以被记事本打开就行
* @param pastePath :粘贴位置的绝对路径 如:"C:\\dev\\新建文件夹"
* @throws IOException
* return
*/
public String copyFile(String copyPath , String pastePath) throws IOException{
//新建文件类,就是获取该路径的文件或文件夹
File copyfile = new File(copyPath);
//exists()方法判断目标文件是否存在,如果文件本身就不存在那就没必要复制了
//isFile()方法判断目标文件是否是一个标准文件(意思就是除文件夹的其他文件)
if(!copyfile.exists() && copyfile.isFile()){
return "未找到该文件";
}
//获取该文件的文件名
String filename = copyfile.getName();
//File.separator和“\\”一个意思
pastePath += File.separator + filename;
//新建文件类,并不是新建一个文件,新建文件会在读写过程之前建
File pastefile = new File(pastePath);
//判断粘贴路径上的目录是否存在,如果不存在就新建目录mkdirs()方法是创建多级目录
if(!pastefile.getParentFile().exists()){
pastefile.getParentFile().mkdirs();
}
//判断粘贴路径上是否已存在命名相同的文件,如果存在就将它删除
if(pastefile.exists()){
pastefile.delete();
}
//文件输入字节流
InputStream in = new FileInputStream(copyfile);
//文件输出字节流
OutputStream out = new FileOutputStream(pastefile);
//缓冲流输入输出流,加快读写速度
BufferedInputStream bin = new BufferedInputStream(in);
BufferedOutputStream bout = new BufferedOutputStream(out);
//将内容转换成流的形式,已字节的大小,存放在字节数组中,容易进行读写操作
byte[] byt = new byte[1024];
int len = 0;
//边读边写将复制文件内的内容读到字节数组中,再写入粘贴路径的文件中
while((len = bin.read(byt)) != -1){
bout.write(byt,0,len);
}
//关闭缓冲流,一定要关闭流,没关闭表示读写操作没有完成,会影响结果或其他流
//关闭流的顺序,一定是先关闭缓存流,再关闭字节流
bin.close();
bout.close();
in.close();
out.close();
return "文件复制完成";
}
我测试过文本.txt .md 图片 .jpg .png 音频 .m4a .mp4 都能成功
二、将一个文件复制到另一个位置并打包成压缩包
import java.io.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
/**
* 文件复制粘贴过程,一般是某某文件复制到某某文件夹内成压缩包,所以复制文件路径是包括该文件的(要加上后缀),而粘贴文件路径是只到某目录,该方法只能成为.zip的压缩包
* @param copyPath :复制文件的绝对路径,一定要有后缀 如: "C:\\text\\新建文件夹\\文件名.txt" 不一定是txt格式的其他格式也可以,包括音频,视频,照片,只要它们可以被记事本打开就行
* @param pastePath :粘贴位置的绝对路径 如:"C:\\dev\\新建文件夹"
* @throws IOException
* return
*/
public String copyFileZip(String copyPath,String pastePath) throws IOException{
//前面操作基本和上一个的一致:新建文件类,判断文件是否存在
File copyfile = new File(copyPath);
if(!copyfile.exists() && copyfile.isFile()){
return "未找到该文件";
}
//获取父级目录的名称充当压缩包的名称
String filename = copyfile.getParentFile().getName();
//完整的文件路径,只能压缩成.zip类型的包
pastePath += File.separator + filename + ".zip";
//操作与前面相同
File pastefilezip = new File(pastePath);
if(!pastefilezip.getParentFile().exists()){
pastefilezip.getParentFile().mkdirs();
}
if(pastefilezip.exists()){
pastefilezip.delete();
}
InputStream in = new FileInputStream(copyfile);
OutputStream out = new FileOutputStream(pastefilezip);
BufferedInputStream bin = new BufferedInputStream(in);
//压缩流,将文件写入压缩包中
ZipOutputStream zout = new ZipOutputStream(out);
BufferedOutputStream bout = new BufferedOutputStream(zout);
//获取复制文件的名称
String copyfilename = copyfile.getName();
//创建一个Zip文件条目,就是压缩文件内的文件名称,一般都是复制文件名称
ZipEntry entry = new ZipEntry(copyfilename);
//将文件条目放入该压缩流中
zout.putNextEntry(entry);
//读写操作,与第一个操作一致
byte[] byt = new byte[1024];
int len = 0;
while((len = bin.read(byt)) != -1){
bout.write(byt,0,len);
}
//关闭流
bin.close();
bout.close();
in.close();
zout.close();
out.close();
return "压缩成功";
}
三、单级目录多文件的复制粘贴
import java.io.*;
/**
* 因为复制的是多个文件,所以路径应该是一个目录,复制的是该目录的文件夹和该目录下的所有标准文件,过程是将某某目录下的文件复制到某某目录下,该文件夹下如果还有文件夹,那么内部的文件夹就不会进行复制粘贴,只对文件进行复制粘贴
* @param copyPath :复制路径 如:C:\\text\\kkk\\pp ;
* @param pastePath :粘贴路径 如:C:\\dev\\ddd
* @return
* @throws IOException
*/
public String copyFiles(String copyPath,String pastePath)throws IOException{
//同上新建文件类
File copyfile = new File(copyPath);
if(!copyfile.exists()){
return "未找到该文件";
}
//判断路径上是否是文件夹,如果是一个文件
if(!copyfile.isDirectory()){
return "该路径不是文件夹";
}
//判断粘贴路径下是否有该文件的文件目录,如果没有就创建
pastePath += File.separator + copyfile.getName();
File pastefiles = new File(pastePath);
if(!pastefiles.exists()){
pastefiles.mkdirs();
}
//获取复制文件路径目录下的所有文件
File[] files = copyfile.listFiles();
//将每个文件进行都单独进行复制粘贴操作
for(int i = 0; i < files.length ; i++){
if(files[i].isDirectory()){
continue;
}
//获取复制目录下每个文件名称
String pastes = pastePath + File.separator + files[i].getName();
File pastesfile = new File(pastes);
//新建输入流,输出流
InputStream in = new FileInputStream(files[i]);
BufferedInputStream bin = new BufferedInputStream(in);
OutputStream out = new FileOutputStream(pastesfile);
BufferedOutputStream bout = new BufferedOutputStream(out);
//边读边写操作
byte[] byt = new byte[1024];
int len = 0;
while((len = bin.read(byt)) != -1){
bout.write(byt,0,len);
}
bin.close();
bout.close();
in.close();
out.close();
}
return "复制成功";
}
通过测试,能复制目录下的文本文件,音频,图片,压缩包等
四、单级目录多文件复制并打包压缩
import java.io.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
/**
* 同上一样,文件压缩包名称,就是文件目录的名称
* @param copyPath :复制目录的路径 如: C:\\text\\kkk
* @param pastePath :压缩包存放的路径 如:C:\\dou\\File
* @return
* @throws IOException
*/
public String copyFilesZip(String copyPath,String pastePath)throws IOException{
//同上新建文件类
File copyfile = new File(copyPath);
//判断该文件夹是否存在
if(!copyfile.exists()){
return "该文件不存在";
}
//该路径下不是文件夹,因为压缩的是整个文件夹,所以必须是文件夹
if(!copyfile.isDirectory()){
return "该路径不是文件夹";
}
//新建文件类
File pastefiles = new File(pastePath);
//保持该路径下的完整性
if(!pastefiles.exists()){
pastefiles.mkdirs();
}
String paste =pastePath + File.separator + copyfile.getName()+".zip";
//输出流
OutputStream out = new FileOutputStream(paste);
//缓冲流
BufferedOutputStream bzip = new BufferedOutputStream(out);
//压缩流
ZipOutputStream zipout = new ZipOutputStream(bzip);
File[] files = copyfile.listFiles();
//判断该文件夹下是否有文件
if(files.length == 0){
return "该文件夹内没有文件";
}
for(int i = 0; i<files.length; i++){
if(files[i].isDirectory()){
continue;
}
InputStream in = new FileInputStream(files[i]);
BufferedInputStream bin = new BufferedInputStream(in);
String copyname = files[i].getName();
//创建新的文件条目
ZipEntry entry = new ZipEntry(copyname);
zipout.putNextEntry(entry);
byte[] byt = new byte[1024];
int len = 0;
while((len = bin.read(byt)) != -1){
zipout.write(byt,0,len);
}
//关闭当前的ZIP条目,并定位流以写入下一个条目。
zipout.closeEntry();
bin.close();
in.close();
}
zipout.close();
bzip.close();
out.close();
return "压缩成功";
}
五、多级目录的复制粘贴
多级目录的复制粘贴无非就多了一个递归。
import java.io.*;
/**
* 多级目录的文件复制多加了一个递归操作
* @param copyPath :复制目录的路径 如:C:\\text\\kkk
* @param pastePath :粘贴的路径 如:C:\\dou\\File
* @return
* @throws IOException
*/
public String copyFilesLayer(String copyPath,String pastePath) throws IOException{
File copyfile = new File(copyPath);
if(!copyfile.exists()){
return "该文件不存在";
}
if(!copyfile.isDirectory()){
return "该路径不是文件夹";
}
String paste = pastePath + File.separator + copyfile.getName();
File pastefile = new File(paste);
if(!pastefile.exists()){
pastefile.mkdirs();
}
File[] files = copyfile.listFiles();
for(int i = 0; i<files.length; i++){
//其他的操作和单级目录的复制粘贴过程一样,只有这里,当文件夹内存在文件夹,那么就再执行此方法,即递归此方法
if(files[i].isDirectory()){
String copyPaths = copyPath + File.separator + files[i].getName();
String pastePaths = paste;
copyFilesLayer(copyPaths,pastePaths);
continue;
}
File pastefilea = new File(paste + File.separator + files[i].getName());
InputStream in = new FileInputStream(files[i]);
OutputStream out = new FileOutputStream(pastefilea);
BufferedInputStream bin = new BufferedInputStream(in);
BufferedOutputStream bout = new BufferedOutputStream(out);
byte[] byt = new byte[1024];
int len = 0;
while((len = bin.read(byt)) != -1){
bout.write(byt,0,len);
}
bout.close();
bin.close();
out.close();
in.close();
}
return "复制成功";
}
六、多级目录的复制并压缩
多级目录的复制并压缩是比较难处理的
/**
* 该方法是获取该文件夹下,所有标准文件的绝对路径,意思就是除了空的文件夹获取其他文件的绝对路径
* @param copyPath :该文件夹的路径
* @return
*/
public File[] getAllFiles(String copyPath){
File copyfile = new File(copyPath);
List<File> list = new ArrayList<File>();
File[] files = copyfile.listFiles();
for(int i = 0; i<files.length ; i++){
if(files[i].isDirectory()){
File[] filesa = getAllFiles(copyPath+File.separator+files[i].getName());
for(File f : filesa){
list.add(f);
}
continue;
}
list.add(files[i]);
}
File[] filesb = new File[list.size()];
for(int i = 0 ; i< filesb.length ; i++){
filesb[i] = list.get(i);
}
return filesb;
}
/**
* 参数意义同上一样
* @param copyPath : 复制文件的路径 如:C:\\text\\kkk
* @param pastePath : 粘贴位置的路径 如:C:\\dou\\File
* @return
* @throws IOException
*/
public String copyFilesLayerZip(String copyPath,String pastePath)throws IOException{
File copyfile = new File(copyPath);
if(!copyfile.exists()){
return "文件不存在";
}
if(!copyfile.isDirectory()){
return "该路径下不是文件夹";
}
File pastefile = new File(pastePath);
if(!pastefile.exists()){
pastefile.mkdirs();
}
//定义好粘贴位置的压缩包的绝对路径
String paste = pastePath + File.separator + copyfile.getName()+".zip";
//路径充当参数新建压缩流
OutputStream out = new FileOutputStream(paste);
BufferedOutputStream bot = new BufferedOutputStream(out);
ZipOutputStream zipout = new ZipOutputStream(bot);
//获取该复制文件内的所有标准文件
File[] files = getAllFiles(copyPath);
for(int i = 0; i<files.length ; i++){
InputStream in = new FileInputStream(files[i]);
BufferedInputStream bin = new BufferedInputStream(in);
//该处是获取所有标准文件在复制文件夹下的相对路径,即就是截取复制文件的绝对路径加1到最后长度,(加1是还有一个“\”)
String filesPath = files[i].getPath().substring(copyPath.length()+1,files[i].getPath().length());
//将每个压缩文件都变成文件条目
ZipEntry entry = new ZipEntry(filesPath);
zipout.putNextEntry(entry);
byte[] byt = new byte[1024];
int len = 0;
while((len = bin.read(byt)) != -1){
zipout.write(byt,0,len);
}
zipout.closeEntry();
bin.close();
in.close();
}
zipout.close();
bot.close();
out.close();
return "压缩成功";
}
该方法的缺点就是空文件夹不会出现在压缩包里