文件操作
来源:互联网 发布:开源云计算平台数据库 编辑:程序博客网 时间:2024/06/07 04:44
import java.io.*;import java.util.zip.ZipEntry;import java.util.zip.ZipOutputStream;/** * Created by zzr on 2017/5/15. */public class FileUtil { public static void main(String[] args) throws Exception { File file = new File("F:\\zzr-bak"); createZip("F:\\ccc.zip",file.listFiles()); } /** * 移动 文件或者文件夹 * @param oldPath * @param newPath * @throws IOException */ public static void moveTo(String oldPath,String newPath) throws IOException { copyFile(oldPath,newPath); deleteFile(oldPath); } /** * 删除文件 * @param filePath */ public static void deleteFile(String filePath){ File file = new File(filePath); if(!file.exists()){ return; } if(file.isDirectory()){ File[] files = file.listFiles(); for (File f : files){ deleteFile(f.getAbsolutePath()); } } file.delete(); } /** * copy复制文件或者文件夹 * @param oldPath * @param newPath */ public static void copyFile(String oldPath,String newPath) throws IOException { System.out.println("copy file from["+oldPath+"] to["+newPath+"]"); File oldFile = new File(oldPath); if(oldFile.exists()){ if(oldFile.isDirectory()){//如果是文件夹 File newPathDir = new File(newPath); newPathDir.mkdirs(); File[] oldFiles = oldFile.listFiles(); if(oldFiles != null && oldFiles.length > 0){ for (File file : oldFiles){ copyFile(file.getAbsolutePath(),newPath.endsWith(File.separator) ? newPath+file.getName() : newPath+File.separator+file.getName()); } } }else{ InputStream inputStream = new FileInputStream(oldFile); OutputStream outputStream = new FileOutputStream(newPath); write2Out(inputStream,outputStream); inputStream.close(); outputStream.close(); } } } /** * 重命名文件 * @param file * @param name * @return */ public static File renameFile(File file , String name ){ String fileName = file.getParent() + File.separator + name ; File dest = new File(fileName); file.renameTo(dest) ; return dest ; } /** * 压缩多个文件。 * @param zipFileName 压缩输出文件名 * @param files 需要压缩的文件 * @return * @throws Exception */ public static File createZip(String zipFileName, File... files) throws Exception { File outFile = new File(zipFileName) ; ZipOutputStream out = null; BufferedOutputStream bo = null; try { out = new ZipOutputStream(new FileOutputStream(outFile)); bo = new BufferedOutputStream(out); for (File file : files) { zip(out, file, file.getName(), bo); } } catch (Exception e) { e.printStackTrace(); }finally { try { bo.close(); } finally { out.close(); // 输出流关闭 } } return outFile; } /** * 压缩单个文件 * @param zipFileName 压缩输出文件名 * @param inputFile 需要压缩的文件 * @return * @throws Exception */ public static File createZip(String zipFileName, File inputFile) throws Exception { File outFile = new File(zipFileName) ; ZipOutputStream out = null; BufferedOutputStream bo = null; try { out = new ZipOutputStream(new FileOutputStream(outFile)); bo = new BufferedOutputStream(out); zip(out, inputFile, inputFile.getName(), bo); } catch (Exception e) { e.printStackTrace(); }finally { try { bo.close(); } finally { out.close(); // 输出流关闭 } } return outFile; } private static void zip(ZipOutputStream out, File f, String base,BufferedOutputStream bo) throws Exception { // 方法重载 if (f.isDirectory()) { File[] fl = f.listFiles(); if ( fl == null || fl.length == 0) { out.putNextEntry(new ZipEntry(base + "/")); // 创建创建一个空的文件夹 }else{ for (int i = 0; i < fl.length; i++) { zip(out, fl[i], base + "/" + fl[i].getName(), bo); // 递归遍历子文件夹 } } } else { out.putNextEntry(new ZipEntry(base)); // 创建zip压缩进入 base 文件 System.out.println(base); BufferedInputStream bi = new BufferedInputStream(new FileInputStream(f)); try { write2Out(bi,out) ; } catch (IOException e) { //Ignore }finally { bi.close();// 输入流关闭 } } } /** * 写流 * @param inputStream * @param outputStream * @throws IOException */ public static void write2Out(InputStream inputStream, OutputStream outputStream) throws IOException { byte[] b = new byte[1024]; int c = 0; while ((c = inputStream.read(b)) != -1){ outputStream.write(b,0,c); outputStream.flush(); } outputStream.flush(); }}
0 0
- 文件操作
- 文件操作
- 文件操作
- 文件操作
- 文件操作
- 文件操作
- 文件操作
- 文件操作
- 文件操作
- 文件操作
- 文件操作
- 文件操作
- 文件操作
- 文件操作
- 文件操作
- 文件操作
- 文件操作
- 文件操作
- SourceTree图谱颜色区别
- log4js用法
- 学习笔记:get与post的区别,请求转发与请求重定向的区别
- 最小生成树模板整理
- JDBC&mysql添加数据的简单例子
- 文件操作
- nginx+redis+tomcat7集群session共享
- windows10 安装 MySqldb
- 读取mysql文件,写入csv,带字段名
- Modern Automake
- mysql查询语句中对字段内容补位(补零为例)length()函数获取某个字段数据长度
- JavaScript单元测试框架JsUnit基本介绍和使用
- 华为Eudemon防火墙inbound入方向和outbound出方向
- 动态内存开辟malloc,calloc,realloc简述