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.通过文件句 ...
随机推荐
- sql server 分组排序
环境: sql server 2012 语法 select ROW_NUMBER() over(partition BY 分组字段 order by 排序字段),* as rowNums from 表 ...
- Codeforces Round #259 (Div. 1)A(公式)
传送门 题意 给出m个面的骰子扔n次,取最大值,求期望 分析 暴力算会有重复,而且复杂度不对. 考虑m个面扔n次得到m的概率,发现只要减去(m-1)个面扔n次得到m-1的概率即可,给出example说 ...
- bzoj 4585: [Apio2016]烟火表演【左偏树】
参考:https://blog.csdn.net/wxh010910/article/details/55806735 以下课件,可并堆部分写的左偏树 #include<iostream> ...
- 洛谷 P3254 圆桌问题【最大流】
s向所有单位连流量为人数的边,所有饭桌向t连流量为饭桌容量的边,每个单位向每个饭桌连容量为1的边表示这个饭桌只能坐这个单位的一个人.跑dinic如果小于总人数则无解,否则对于每个单位for与它相连.满 ...
- Ocelot(一)- .Net Core开源网关
Ocelot - .Net Core开源网关 作者:markjiang7m2 原文地址:https://www.cnblogs.com/markjiang7m2/p/10857688.html 源码地 ...
- CAD中的文本编排操作
AutoCAD中导入超文本实现方法 除了AutoCAD内含的文本输入命令外,向AutoCAD中输入多行文本即超文本的方法还有:通过Windows提供的剪贴板,利用AutoCAD提供的多行文本命 ...
- 题解报告:poj 2823 Sliding Window(单调队列)
Description An array of size n ≤ 106 is given to you. There is a sliding window of size k which is m ...
- html下的图片链接有边框的解决方法
使用dreamweaver创建网页后,上传到网站发现网页的图片链接有非常难看的蓝色边框,而在dw下是没有的 后来查看了一下网上的资料,发现加一个border="0"即可,默认是有边 ...
- 442 Find All Duplicates in an Array 数组中重复的数据
给定一个整数数组 a,其中1 ≤ a[i] ≤ n (n为数组长度), 其中有些元素出现两次而其他元素出现一次.找到所有出现两次的元素.你可以不用到任何额外空间并在O(n)时间复杂度内解决这个问题吗? ...
- 多功能Markdown编辑器MarkdownPad 2的下载、安装和初步使用步骤(图文详解)(博主推荐)
不多说,直接上干货! MarkdownPad 是什么? 一.MarkdownPad 2的下载 http://markdownpad.com/download/markdownpad2-setup. ...