文件的拷贝和转移

package com.wepull.demo;



import java.io.*;

import java.util.zip.*;



/** *//**

* 对文件或者目录操作的类

* @version 1.0

* @author leno

*/

public class FileUtil {



private static void copy(File source, File target) throws IOException {

File tar = new File(target, source.getName());

if (source.isDirectory()) {

System.out.println("开始创建目录:" + tar.getPath());

tar.mkdir();

File[] fs = source.listFiles();

for (int i = 0; i < fs.length; i++) {

copy(fs[i], tar);

}

} else {

System.out.println("开始从" + source + "拷贝文件到" + tar.getPath());

InputStream is = new FileInputStream(source);

OutputStream os = new FileOutputStream(tar);

byte[] buf = new byte[1024];

int len = 0;

while ((len = is.read(buf)) != -1) {

os.write(buf, 0, len);

}

is.close();

os.close();

}

}



/** *//**

* 拷贝文件或者目录到某个指定的路径

*

* @param source

* 源文件或者目录

* @param target

* 目标路径

* @throws IOException

*/

public static void copy(String source, String target) {

File sour = new File(source);

File tar = new File(target);

try {

copy(sour, tar);

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}



private static void delete(File file) {

if (file.isDirectory()) {

File[] fs = file.listFiles();

for (int i = 0; i < fs.length; i++) {

delete(fs[i]);

}

file.delete();

} else {

file.delete();

}

}



/** *//**

* 删除一个文件或者目录

*

* @param file

*/

public static void delete(String path) {

File file = new File(path);

delete(file);

}



/** *//**

* 压缩文件或者目录到指定的路径

*

* @param zipFileName

* 目标路径

* @param inputPath

* 被压缩的文件或者目录

*/

public static void zip(String zipFileName, String inputPath) {

File inputFile = new File(inputPath);

ZipOutputStream out;

try {

out = new ZipOutputStream(new FileOutputStream(zipFileName));

zip(out, inputFile, inputFile.getName());

System.out.println("压缩完成!");

out.close();

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}



private static void zip(ZipOutputStream out, File f, String base)

throws Exception {

System.out.println("正在压缩:" + f.getName() + " ");

if (f.isDirectory()) {

File[] fs = f.listFiles();

base += "/";

System.out.println("新建目录条目:" + f.getName());

out.putNextEntry(new ZipEntry(base)); // 生成相应的目录

for (int i = 0; i < fs.length; i++) {

// 对本目录下的所有文件对象递归调用本方法

zip(out, fs[i], base + fs[i].getName());

}

} else {

System.out.println("新增文件条目:" + f.getName());

out.putNextEntry(new ZipEntry(base));

InputStream is = new FileInputStream(f);

byte[] buf = new byte[1024];

int len = 0;

while ((len = is.read(buf)) != -1) {

out.write(buf, 0, len);

}

is.close();

}

}



/** *//**

* 解压缩zip文件到指定的路径

*

* @param zipfile

* zip格式压缩文件

* @param desPath

* 目标路径

*/

public static void unzip(String zipFile, String desPath) {

// 建立输出流,用于将从压缩文件中读出的文件流写入到磁盘

OutputStream out = null;

// 建立输入流,用于从压缩文件中读出文件

ZipInputStream is;

try {

is = new ZipInputStream(new FileInputStream(zipFile));

ZipEntry entry = null;

while ((entry = is.getNextEntry()) != null) {

System.out.println("正在解压缩:" + entry.getName() + " ");

File f = new File(desPath + "\\" + entry.getName());

if (entry.isDirectory()) {

System.out.println("新建目录:" + f.getName());

f.mkdir();

} else {

System.out.println("新增文件:" + f.getName());

// 根据压缩文件中读出的文件名称新建文件

out = new FileOutputStream(f);

byte[] buf = new byte[1024];

int len = 0;

while ((len = is.read(buf)) != -1) {

out.write(buf, 0, len);

}

out.close();

}

}

is.close();

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}



/** *//**

* 创建新文件

*

* @param path

*/

public static void createFile(String path) {

File file = new File(path);

try {

file.createNewFile();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}



}



/** *//**

* 创建新目录

*

* @param path

*/

public static void createDir(String path) {

File file = new File(path);

file.mkdirs();

}



/** *//**

* 剪切文件或者目录到某个指定的路径

*

* @param source

* 源文件或者目录

* @param target

* 目标路径

*

*/

public static void cutTo(String source, String target) {

File sourFile = new File(source);

File tarFile = new File(target);

if (sourFile.isFile()) {

if (tarFile.isDirectory()) {

sourFile.renameTo(tarFile);

}

} else {

copy(source, target);

delete(source);

}

}



public static void main(String[] args) {

// copy("E:\\w.txt", "E:\\a");

// delete("E:\\a");

// zip("E:\\a.zip", "E:\\b");

// unzip("E:\\a.zip", "E:\\b");

// createFile("E:\\a.txt");

// createDir("E:\\bb");

// cutTo("E:\\b", "D:\\");

}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值