Java 文件操作大集合
package com.sess.hny.sys.fileserver;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public final class FileOperateUtils {
文件夹判断,当没有时创建
public static void directoryExistsAndCreate(String filePath) throws Exception
{
File file = new File(filePath);
if( !file.exists() )
{
if (!file.getParentFile().exists()) {// 判断目标文件所在的目录是否存在
if (!file.getParentFile().mkdirs()) {// 判断创建目录是否成功
throw new Exception("创建文件路径失败!");
}}}
}
判断文件及文件夹是否存在,存在就删除
public static void directoryExistsAndRemove(String filePath) throws Exception
{
File file = new File(filePath);
if( file.exists() )
{
if( file.isDirectory() )
{
for( File fl : file.listFiles() )
{
directoryExistsAndRemove(fl);
}
file.delete();
}
else
{
file.delete();
}
}
}
拷贝文件
public static void copyFile(String srcPath, String dstPath) throws Exception
{
if( null != srcPath && srcPath.trim().length() > 0 && null != dstPath && dstPath.trim().length() > 0)
{
if( !srcPath.equals(dstPath) )
{
if( directoryExists(srcPath) )
{
if( directoryExists(dstPath) ) throw new Exception("目标文件已经存在");
else
{
BufferedInputStream srcBuff = null;
BufferedOutputStream dstBuff = null;
try
{
//源文件流
FileInputStream src = new FileInputStream(srcPath);
srcBuff = new BufferedInputStream(src);
//目标文件流
FileOutputStream dst = new FileOutputStream(dstPath);
dstBuff = new BufferedOutputStream(dst);
int len = 0;
while( (len = srcBuff.read()) != -1 )
{
dstBuff.write(len);
}
}
catch (Exception e)
{
throw e;
}
finally
{
if( null != srcBuff) srcBuff.close();
if( null != dstBuff) dstBuff.close();
}
}
}
else
{
throw new Exception("源目录文件不存在");
}
}
}
}
压缩zip文件
public static String fileToZip(String sourceFilePath, String zipParentName, boolean verifyExists, IFileServerConfig fileConfig) throws Exception
{
if( !directoryExists(sourceFilePath) ) throw new Exception("压缩文件目录:" + sourceFilePath + "文件或文件夹不存在!!!");
String zipFilePath = null;
String fileName = null;
int sourceIndex1 = sourceFilePath.lastIndexOf("/");
int sourceIndex2 = sourceFilePath.lastIndexOf("\\");
if( sourceFilePath.endsWith("/") )
{
sourceFilePath = sourceFilePath.substring(0, sourceIndex1);
}
else if( sourceFilePath.endsWith("\\") )
{
sourceFilePath = sourceFilePath.substring(0, sourceIndex2);
}
// 获取文件夹压缩名
if( sourceIndex1 != -1 && sourceIndex1 > sourceIndex2 )
{
zipFilePath = sourceFilePath.substring(0, sourceIndex1);
fileName = sourceFilePath.substring(sourceIndex1 + 1);
}
else if( sourceIndex2 != -1 && sourceIndex1 < sourceIndex2 )
{
zipFilePath = sourceFilePath.substring(0, sourceIndex2);
fileName = sourceFilePath.substring(sourceIndex2 + 1);
}
else throw new Exception("文件目录不正确!!!");
if( null == zipParentName || zipParentName.trim().length() == 0) zipParentName = fileName;
return fileToZip(sourceFilePath, zipFilePath, fileName, zipParentName, verifyExists, fileConfig);
}
// 文件压缩成zip
private static String fileToZip(String sourceFilePath, String zipFilePath, String fileName, String parentName, boolean verifyExists, IFileServerConfig fileConfig) throws Exception
{
File sourceFile = new File(sourceFilePath);
String filePath = null;
FileOutputStream fos = null;
ZipOutputStream zos = null;
try
{
File zipFile = new File(zipFilePath + "/" + fileName + ".zip");
//文件夹是否存在处理
if( zipFile.exists() )
{
if( verifyExists ) throw new Exception(zipFilePath + "目录下存在名字为:" + fileName +".zip" +"打包文件.");
else zipFile.delete();
}
filePath = zipFile.getPath();
File[] sourceFiles = sourceFile.listFiles();
if( null == sourceFiles || sourceFiles.length < 0 ) throw new Exception("待压缩的文件目录:" + sourceFilePath + "里面不存在文件,无需压缩.");
fos = new FileOutputStream(zipFile);
zos= new ZipOutputStream( new BufferedOutputStream(fos) );
for(int i = 0; i < sourceFiles.length; i++)
{
addZipFile(sourceFiles[i], parentName, zos, fileConfig);
}
return filePath;
}
catch (Exception e)
{
throw e;
}
finally
{
try
{
if( null != zos ) zos.close();
}
catch (Exception e2)
{
throw e2;
}
}
}
// 添加压缩文件目录
private static void addZipFile(File file, String parentPath, ZipOutputStream zos, IFileServerConfig fileConfig) throws Exception
{
if( null == file ) throw new Exception("file不能为空!!!");
if( null == zos ) throw new Exception("zos不能为空!!!");
FileInputStream fis = null;
BufferedInputStream bis = null;
int len = 1024 * 10;
byte[] bufs = new byte[len];
String fileName = null;
int read = 0;
try
{
if( file.isDirectory() )
{
parentPath += "/" + file.getName() + "/";
// 读取待压缩的文件并写进压缩包里
File[] files = file.listFiles();
if( null != files && files.length > 0 )
{
for(int i = 0; i < files.length; i++)
{
addZipFile(files[i], parentPath, zos, fileConfig);
}
}
else
{
ZipEntry zipEntry = new ZipEntry(parentPath);
zos.putNextEntry(zipEntry);
}
}
else
{
// 创建ZIP实体,并添加进压缩包
if( fileConfig == null ) fileName = file.getName();
else fileName = fileConfig.getZipEntry(file);
ZipEntry zipEntry = new ZipEntry(parentPath + "/" + fileName);
zos.putNextEntry(zipEntry);
// 读取待压缩的文件并写进压缩包里
fis = new FileInputStream(file);
bis = new BufferedInputStream(fis, len);
read = 0;
while( ( read = bis.read(bufs, 0, len) ) != -1)
{
zos.write(bufs, 0, read);
}
}
}
catch (Exception e)
{
throw e;
}
finally
{
try
{
if( null != bis ) bis.close();
}
catch (Exception e2)
{
throw e2;
}
}
}
public static void main(String[] args)
{
try
{
FileOperateUtils.fileToZip("F:\\lmm\\learn", null, false, null);
System.out.println("打包完成");
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
Java 文件操作大集合的更多相关文章
- Java文件操作源码大全
Java文件操作源码大全 1.创建文件夹 52.创建文件 53.删除文件 54.删除文件夹 65.删除一个文件下夹所有的文件夹 76.清空文件夹 87.读取文件 88.写入文件 99.写入随机文件 9 ...
- Java 文件操作大全
Java 文件操作大全 //1.创建文件夹 //import java.io.*; File myFolderPath = new File(str1); try { if (!myFolderPat ...
- JAVA文件操作类和文件夹的操作代码示例
JAVA文件操作类和文件夹的操作代码实例,包括读取文本文件内容, 新建目录,多级目录创建,新建文件,有编码方式的文件创建, 删除文件,删除文件夹,删除指定文件夹下所有文件, 复制单个文件,复制整个文件 ...
- java文件操作(普通文件以及配置文件的读写操作)
转自:java文件操作(普通文件以及配置文件的读写操作) 读取普通文件 : /** * xiangqiao123欢迎你 如果对代码有疑问可以加qq群咨询:151648295 * * 读取MyFile文 ...
- 14、Java文件操作stream、File、IO
1.文件操作涉及到的基本概念 File File类 是文件操作的主要对象中文意义就是 文件 顾名思意 万物皆文件,在计算上看到的所有东西都是文件保存,不管是你的图片.视频.数据库数据等等都是按照基本的 ...
- Java文件操作API功能与Windows DOS命令和Linux Shell 命令类比
Java文件操作API功能与Windows DOS命令和Linux Shell 命令类比: Unix/Linux (Bash) Windows(MS-DOS) Java 进入目录 cd cd - 创建 ...
- 拼多多、饿了么、蚂蚁金服Java面试题大集合
自己当初找工作时参加过众多一线互联网公司的Java研发面试,这段时间处于寒冬,然而前几天跳槽找工作,两天面了3家,已经拿了两个offer,觉得可以和大家分享下: 下面为拼多多.饿了么.蚂蚁金服.哈啰出 ...
- Java文件操作类效率对比
前言 众所周知,Java中有多种针对文件的操作类,以面向字节流和字符流可分为两大类,这里以写入为例: 面向字节流的:FileOutputStream 和 BufferedOutputStream 面向 ...
- python学习笔记:文件操作和集合(转)
转自:http://www.nnzhp.cn/article/16/ 这篇博客来说一下python对文件的操作. 对文件的操作分三步: 1.打开文件获取文件的句柄,句柄就理解为这个文件 2.通过文件句 ...
随机推荐
- Luogu P1273 有限电视网【树形Dp/树形背包】
题目描述 某收费有线电视网计划转播一场重要的足球比赛.他们的转播网和用户终端构成一棵树状结构,这棵树的根结点位于足球比赛的现场,树叶为各个用户终端,其他中转站为该树的内部节点. 从转播站到转播站以及从 ...
- shiro 登录
@Controllerpublic class LoginController { @RequestMapping(value="/login") public @Response ...
- redis问题集结
redis和memcached比较? redis中数据库默认是多少个db 及作用? python操作redis的模块? 如果redis中的某个列表中的数据量非常大,如果实现循环显示每一个值? redi ...
- _bzoj1051 [HAOI2006]受欢迎的牛【强联通】
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1051 保存tarjan模版. 求强联通分量,缩点. #include <cstdio& ...
- ACboy needs your help HDU - 1712
ACboy needs your help HDU - 1712 ans[i][j]表示前i门课共花j时间最大收益.对于第i门课,可以花k(0<=k<=j)时间,那么之前i-1门课共花j- ...
- 转-iOS 动画总结----UIView动画
来自:http://blog.csdn.net/huifeidexin_1/article/details/7597868/ 1.概述 UIKit直接将动画集成到UIView类中,实现简单动画的创建过 ...
- 转】Mahout构建图书推荐系统
原博文出自于: http://blog.fens.me/hadoop-mahout-recommend-book/ 感谢! Mahout构建图书推荐系统 Hadoop家族系列文章,主要介绍Hadoop ...
- 通过 DBCA 工具创建Oracle数据库
DBCA 是 Oracle 提供的一款图形化界面工具,用来帮助数据库管理员快速.直观地创建数据库,避免了繁琐复杂的 SQL命令操作. 使用 DBCA创建数据库的过程如下. (1)依次 单击“开始 ”→ ...
- jqueryUI插件
<link rel="stylesheet" href="~/Content/themes/base/jquery-ui.css" /> <s ...
- php debug/phpstorm调试
apache+phpstorm调试php代码,修改php.ini配置文件开启调试,没有以下代码加上即可, [XDebug]zend_extension="C:\php\php-7.0.12- ...