黑马程序员————IO流简单应用:分隔文件、合并文件
来源:互联网 发布:淘宝助手批量上传图片 编辑:程序博客网 时间:2024/06/04 00:33
------www.itheima.com Java培训、Android培训、iOS培训、.Net培训期待与您交流! -------
本文主要应用javaIO中字节流,将源文件分割成每份1M,也可以反过来将分割后的文件合并成新的文件(与源文件相同)。
源码如下:
/** * 分割、合并文件 * @author Smilexs */public class DivideFile {private static final int SIZE = 1024*1024;//每部分的文件大小==1Mpublic static void main(String[] args) {String oriFilePath="F:\\Home.mp3";//原文件String distDirPath="F:\\Part";//分割文件存放的文件夹divideFile(oriFilePath,distDirPath);mergeFile(distDirPath, "F:\\home2.mp3");//合并后的新文件}/** * 对文件进行分割 * @param oriFilePath--原文件位置 * @param distDirPath--目的文件夹位置 */public static void divideFile(String oriFilePath,String distDirPath){/**1、简单异常处理*/if(oriFilePath==null || distDirPath==null){System.err.println("参数异常");return;}File oriFile = new File(oriFilePath);File distDir = new File(distDirPath);if(!oriFile.exists() || oriFile.length() < 1024 || distDir.isFile()){return;//文件不存在,文件大小过小,目的不是文件夹都直接返回}if(!distDir.exists()){distDir.mkdir();//创建文件夹}try {/**2、读取文件*/FileInputStream fis = new FileInputStream(oriFile);byte[] data = new byte[SIZE];//每部分的文件大小==1Mint count =1;//标记第几部分的文件int len = 0;while((len=fis.read(data))!=-1){/**3、写文件*/FileOutputStream fos = new FileOutputStream(distDirPath+"\\part_"+count+".txt");fos.write(data,0,len);fos.flush();fos.close();count++;}System.out.println("分割完成");fis.close();} catch (Exception e) {e.printStackTrace();}}/** * 合并文件 * @param oriDirPath--源 分割的文件所在的文件夹路径 * @param distFilePath --合成的目的文件 */public static void mergeFile(String oriDirPath,String distFilePath){/**1、简单异常处理*///略!!try {/**2、读文件*/int count=1;File tempFile =new File(oriDirPath+"\\part_"+count+".txt");//有文件存在就合并进去while(tempFile.exists()){FileInputStream fis = new FileInputStream(tempFile);byte[] data = new byte[SIZE];//每部分的文件大小==1Mint len =fis.read(data);/**3、写文件*/FileOutputStream fos = new FileOutputStream(distFilePath,true);fos.write(data, 0, len);fos.flush();fos.close();fis.close();count++;tempFile =new File(oriDirPath+"\\part_"+count+".txt");}System.out.println("合并完成");} catch (Exception e) {e.printStackTrace();}}}
0 0
- 黑马程序员————IO流简单应用:分隔文件、合并文件
- 黑马程序员——>第二十天<io流(File-Proterties-合并流-切割文件)>
- 黑马程序员——IO流复制文件
- 黑马程序员——IO流复制文件
- 黑马程序员-java IO的文件合并
- 黑马程序员——文件的切割,与合并
- 黑马程序员——文件的切割与合并
- 黑马程序员—文件的切割与合并
- 黑马程序员 IO流 文件的分割与合并
- 黑马程序员------IO中合并流和切割文件
- 黑马程序员——————文件IO
- 黑马程序员——AWT+IO获取文件列表
- 黑马程序员——java IO实现文件拷贝
- 黑马程序员—IO文件操作(2)
- 黑马程序员——第20天——IO输入与输出(File类,Properties,打印流,合并流,切割文件)
- 黑马程序员—异常+IO流File类+Properties+PrinWriter+合并流+切割文件+对象序列化+管道流+RandomAccessFile+操作基本数据类型的DataStre
- 黑马程序员—IO基础应用
- 黑马程序员—IO基础应用II
- signal(SIGPIPE, SIG_IGN)
- 设计模式心得小结(一)
- S3C2440 中断控制寄存器
- 集成测试
- linux 网络配置
- 黑马程序员————IO流简单应用:分隔文件、合并文件
- Code Forces 581 A. Vasya the Hipster(水~)
- select、poll、epoll之间的区别总结[整理]
- 查找
- notepad 记事本 calc 计算器等命令行常用命令
- C语言中内存对齐方式摘录
- Form表单中的action提交路径问题
- hdoj 连连看 1175 (bfs)
- strcpy,memset,memcpy区别