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.通过文件句 ...
随机推荐
- bzoj 3624: [Apio2008]免费道路【生成树+贪心】
先把水泥路建生成树,然后加鹅卵石路,这里加的鹅卵石路是一定要用的(连接各个联通块),然后初始化并查集,先把必需的鹅卵石路加进去,然后随便加鹅卵石路直到k条,然后加水泥路即可. 注意判断无解 #incl ...
- Luogu P1137 旅行计划 【拓扑排序+Dp】By cellur925
题目传送门 由于满足游览先后顺序从西到东的性质,我们很自然的想到用拓扑排序处理出一个合理的游览顺序. 然鹅,之后呢? 事实上,拓扑排序常与Dp相结合,解决后效性.我们就可以在每次拓扑入队的时候更新答案 ...
- 【CSS】少年,你想拥有写轮眼么?
最近笔者在公司内部开展了一次CSS讲座,由于授课经验不太足,授课效果自我感觉并不太好,不过课中有一个笔者用CSS写的一个小效果,其中还是包含了蛮多CSS的常见知识点的,正好也有部分同学很感兴趣如何实现 ...
- Errors running builder 'JavaScript Validator'错误处理
MyEclipse2014编辑代码时,只要保存就会报出如下错误信息: Errors occurred during the build. Errors running builder 'JavaScr ...
- SpringCloud+MyBatis+Redis整合—— 超详细实例(二)
2.SpringCloud+MyBatis+Redis redis①是一种nosql数据库,以键值对<key,value>的形式存储数据,其速度相比于MySQL之类的数据库,相当于内存读写 ...
- linux下常用网络操作汇总 专题
centos 更改主机名,需要更改的几个地方: (1) /etc/sysconfig/network //更改主机名(2)/etc/hostname //更改主机名(3) /etc/hosts ...
- windwsform登录页面
简单登录设计: 读取用户名密码 数据库表 实体类 数据访问类: 隐藏登录页面: 回车快捷键: 传值到main窗口:
- core\stm32f10x.h(244): error: #67: expected a "}"
- 最全的Java面试宝典
一. 前言部分 从享受生活的角度上来说:“程序员并不是一种最好的职业,我认为两种人可以做程序员,第一,你不做程序员,你就没有什么工作可做,或者说是即使有可以做的工作但是你非常不愿意去做:第二,你非常痴 ...
- MAC加域重复跳出---"talagent"想使用“本地项目” 的钥匙串
很简单的解决办法,就是把以前的钥匙串给删掉就好 (重要提示:这个方法,以前所有程序自动记录密码都会丢掉,safari的自动填充,QQ自动登录,imessages 的等等) 1.打开Finder -&g ...