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 1306: [CQOI2009]match循环赛【dfs+剪枝】
大力剪枝,最后洛谷上还开了o2才过-- 大概这样剪枝: 1.搜索中,一个队当前得分超过要求或者一个队剩下的比赛场数全赢也达不到要求则return: 2.注意到如果平局,最总分的贡献是2,否则是3,所以 ...
- net 视图的调用和开发
1.调用视图 声明实体类 string sql = "select * from View_ExchangeDetails order by CreateTime desc"; v ...
- Windows下安装Ubuntu16.04双系统
ROS需要在Ubuntu系统上开发,虚拟机跑Ubuntu开发ROS容易出现各种各样的问题,所以需要安装Ubuntu16.04双系统.笔者也是一步步按着网上的帖子来,由于网上的教程都不是最新的而且有的也 ...
- 输入一个秒数,要求转换为XX小时XX分XX秒的格式输出出来;
package arithmetic; import java.util.Scanner; import org.junit.Test; public class Test02 { /** * 输入一 ...
- 通过API文档查询Math类的方法,打印出近似圆,只要给定不同半径,圆的大小就会随之发生改变
package question; import java.util.Scanner; import java.lang.Math; public class MathTest { /** * 未搞懂 ...
- SpringCloud开发学习总结(三)—— 服务治理Eureka
在最初开始构建微服务系统的时候可能服务并不多,我们可以通过做一些静态配置来完成服务的调用.比如,有两个服务A和B,其中服务A需要调用服务B来完成一个业务操作时,为了实现服务B的高可用,不论采用服务端负 ...
- 转】MYSQL性能调优与架构设计之select count(*)的思考
原博文出自于: http://blog.fens.me/category/%E6%95%B0%E6%8D%AE%E5%BA%93/page/5/ 感谢! Posted: Feb 7, 2013 Tag ...
- Neither BindingResult nor plain target object for bean name 'user' available as request attribute
这个异常是因为jsp页面写错了. 把<form:form></form:form>标签改成普通的标签即可. 应该是第一次访问的时候,user是空的.但springmvc不能是空 ...
- MyEclipse开启Jquery智能提示
myeclipse 增加javascript提示和jquery提示等不用安装插件自带功能 (对着需要提示的项目右键,点击properties) 不行的话就得安装插件: http://www.spket ...
- 好用的SqlParamterList
public class SqlParameterList : List<SqlParameter> { #region Properties /// <summary> // ...