io文件复制读写以及打gz和zip压缩包,递归删除文件以及文件夹

package com.my.io;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.nio.channels.FileChannel;
import java.util.Date;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
* 文件复制以缓冲区方式
* @author Mengyao
*
*/
public class FileCopyTest {


public static void main(String[] args) {
//这个镜像文件4.05GB,本地复制耗时92秒
//copyFile_buffered(new File("D:/mengyao/资料/hadoop/linux/CentOS-6.4-x86_64-bin-DVD1.iso"),"D:/CentOS-6.4-x86_64-bin-DVD1.iso");
//这个镜像文件4.05GB,本地复制耗时76秒
//copyFile_buffered(new File("D:/mengyao/资料/hadoop/linux/CentOS-6.4-x86_64-bin-DVD1.iso"),"D:/CentOS-6.4-x86_64-bin-DVD1.iso");
//读取文件内容
//System.out.println(copyFile_text(new File("D:/new fields/1.txt")));
//将文件压缩成test.gz,用于单个文件
//getZip(new File("D:/new fields/1.txt"), "D:/new fields/test.gz");
//将多个文件打成压缩包.zip
//getZips(new File[]{new File("D:/new fields/1.txt"),new File("D:/new fields/2.txt"),new File("D:/new fields/3.txt")}, "D:/new fields/files.zip");
//创建文本文件
//createFile_Text(new File("D:/new fields/textEncoding.txt"));
//读取文本文件
//System.out.println(readFile_text(new File("D:/new fields/textEncoding.txt")));
//递归删除某个文件夹下所有文件以及文件夹
//deleteAllFile(new File("D:/new fields"));



}

/**
* 复制文件
* @param file 源文件
* @param TargetPath 目标路径
*/
public static void copyFile_buffered(File file,String TargetPath) {
if (file.exists()&&!TargetPath.isEmpty()) {
BufferedInputStream in=null;
BufferedOutputStream out=null;
try {
System.out.println(new Date());
in=new BufferedInputStream(new FileInputStream(file));
out=new BufferedOutputStream(new FileOutputStream(TargetPath));
byte[] bytes=new byte[1024];
int len=0;
while (-1!=(len=in.read(bytes))) {
out.write(bytes, 0, len);
}
System.out.println(new Date());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
in.close();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
} else {
System.out.println("文件不存在或者未指定目标路径!");
}
//System.out.println("文件名:"+file.getName()+"\n文件所在路径:"+file.getParent()+"\n文件全路径:"+file.getPath()+file.getTotalSpace()+"\n文件是否存在:"+file.exists()+"\n文件是否路径:"+file.isDirectory());
}

/**
*
* 复制文件
* @param file 源文件
* @param TargetPath 目标路径
*/
public static void copyFile_channel(File file,String TargetPath){
FileInputStream fi = null;
FileOutputStream fo = null;
FileChannel in = null;
FileChannel out = null;
try {
fi=new FileInputStream(file);
fo=new FileOutputStream(TargetPath);
in=fi.getChannel();//得到对应的文件通道
out=fo.getChannel();//得到对应的文件通道
in.transferTo(0, in.size(), out);//连接两个通道,并且从in通道读取,然后写入out通道
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fi.close();
in.close();
fo.close();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

/**
* 获取文本文件中的内容
* @param file
* @return
*/
public static String copyFile_text(File file){
BufferedReader in=null;
StringBuilder sb=null;
try {
in=new BufferedReader(new FileReader(file));
String str;
sb=new StringBuilder();
while ((str=in.readLine())!=null) {
sb.append(str+"\n");
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}

return sb.toString();
}

/**
* 生成一个gz文件:java.util.zip.Adler32 //速度快;java.util.zip.CRC32 //慢一些,但更准确
* @param file 源文件
* @param targetPath 新文件路径
*/
public static void getZip(File file,String targetPath){
BufferedReader in=null;
BufferedOutputStream out=null;
BufferedReader in2=null;
try {
//生成test.zip文件
in=new BufferedReader(new FileReader(file));
out=new BufferedOutputStream(new GZIPOutputStream(new FileOutputStream(targetPath)));
int c;
while ((c=in.read())!=-1) {
out.write(c);
}
in.close();
out.close();
//读取test.zip文件中的内容
in2=new BufferedReader(new InputStreamReader(new GZIPInputStream(new FileInputStream(targetPath))));
String str;
while ((str=in2.readLine())!=null) {
System.out.println(str);
}
in2.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally{
try {
in.close();
out.close();
in2.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

/**
* 生成多个zip文件:java.util.zip.Adler32 //速度快;java.util.zip.CRC32 //慢一些,但更准确
* @param file 源文件
* @param targetPath 新文件路径
*/
public static void getZips(File[] files,String targetPath){
FileInputStream in=null;
ZipOutputStream out=null;
try {
out= new ZipOutputStream(new FileOutputStream(targetPath));
byte[] buffer = new byte[1024];
//需要同时下载的两个文件result.txt ,source.txt
//File[] file1 = {new File("e:/a.txt"),new File("e:/b.txt"),new File("e:/aa.txt"),new File("e:/bb.txt")};
for(int i=0;i<files.length;i++) {
in = new FileInputStream(files[i]);
out.putNextEntry(new ZipEntry(files[i].getName()));
int len;
//读入需要下载的文件的内容,打包到zip文件
while((len = in.read(buffer))>0) {
out.write(buffer,0,len);
}

}
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally{
try {
out.closeEntry();
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

public static void createFile_Text(File file){
//获取到JVM中所设置的字符集编码, JVM是从系统变量file.encoding中读取操作系统的默认编码的字符集,来设置JVM的字符集编码的。
final String encoding = System.getProperty("file.encoding");
Writer out=null;
try {
if (!file.exists())
{
new File(file.getParent()).mkdirs();
file.createNewFile(); //文件不存在,建立
}
out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), encoding));
out.write("大量文字内容,例如" +
"日志文件" +
"文本追加");

} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally{
try {
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

public static String readFile_text(File file) {
final String encoding = System.getProperty("file.encoding");
InputStreamReader in=null;
BufferedReader out=null;
StringBuffer sb=null;

try {
//文本文件编码是UTF-8,如果是其它,请修改下面
in = new InputStreamReader(new FileInputStream(file), encoding);
out = new BufferedReader(in);
String str=out.readLine();
sb=new StringBuffer();
while(str!=null){
sb.append(str+"\n");
str=out.readLine();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally{
try {
in.close();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}

return sb.toString();
}

/**
* 递归删除某个文件夹下所有文件以及文件夹
* @param file
*/
public static void deleteAllFile(File file){
//如果是文件或者是空文件夹则直接删除
if (file.isFile()||file.list().length==0) {
file.delete();
}else {
//获取到该文件夹下的子文件夹下所有的文件
File[] files=file.listFiles();
//循环得到子文件的所在路径
for (File f : files) {
//调用自身实现递归删除
deleteAllFile(f);
//当所有的空文件夹及非空文件夹下的文件被删除后则删除自身
f.delete();
}
}
}

}

以上代码复制可用
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值