IO流(SequenceInputStream序列流--文件拆分与合并)
一、文件拆分
1、将一个文件按照规定的大小进行分割成多个文件并将被分割详细信息保存至配置信息中
Properties 类表示了一个持久的属性集。Properties 可保存在流中或从流中加载,属性列表中每个键及其对应值都是一个字符串。
package ioDemo; import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties; /**
* 文件切割
*将一个文件按照规定的大小进行分割成多个文件并将被分割详细信息保存至配置信息中
*
* Created by lcj on 2017/11/14.
*/
public class partFileDemo {
//定义全局变量 SIZE缓存大小
private static final int SIZE = 1048576; public static void main(String[] args) throws IOException{
//获取目标源文件
File file = new File("D:\\IMG_0859.JPG");
// cutPartFile(file);
cutPartFile02(file);
}
//重写分割文件方法
public static void cutPartFile02(File file)throws IOException{
//获取目标源文件
FileInputStream fis = new FileInputStream(file);
//定义一个1M缓冲区
byte[] buf= new byte[SIZE]; //创建目的地文件
FileOutputStream fos = null; int len = 0 ;
int count = 1 ;
/**
* 切割文件时,必须记住被切割文件的名称及切割出来的碎片文件的个数,以方便合并
* 信息为了进行描述,使用键值对的文件,用到了properties对象
* 信息是按照流对象存储在硬盘,故用properties对象
* */
// Properties 类表示了一个持久的属性集。Properties 可保存在流中或从流中加载。
// 属性列表中每个键及其对应值都是一个字符串。
Properties prop = new Properties();
//存放分割文件路径
File dir = new File("D:\\partFiles");
if (!dir.exists())
{
dir.mkdirs();//可创建多级目录
}
while ((len=fis.read(buf))!=-1)
{
fos = new FileOutputStream(new File(dir,(count++)+".part"));
fos.write(buf,0,len);//每次写1M文件 写完即可以关闭改文件
fos.close();
}
//将被分割文件信息保存到prop集合中
prop.setProperty("partcount",count+"");
prop.setProperty("filename",file.getName());
//在file文件当前目录下 保存配置文件信息
fos = new FileOutputStream(new File(dir,(count++) +".properties"));
//将prop集合中的数据保存至文件中
prop.store(fos,"save file info "); fos.close();
fis.close();
} public static void cutPartFile(File file) throws IOException{ //获取目标源文件
FileInputStream fis = new FileInputStream(file);
//定义一个1M缓冲区
byte[] buf= new byte[SIZE]; //创建目的地文件
FileOutputStream fos = null; int len = 0 ;
int count = 0 ;
//存放分割文件路径
File dir = new File("D:\\partFiles");
if (!dir.exists())
{
dir.mkdirs();//可创建多级目录
}
while ((len=fis.read(buf))!=-1)
{
fos = new FileOutputStream(new File(dir,(count++)+".part"));
fos.write(buf,0,len);
}
fos.close();
fis.close();
} }
二、文件合并
getProperty:用指定的键在属性列表中搜索属性。如果在属性列表中未找到该键,则接着递归检查默认属性列表及其默认值。如果未找到属性,则此方法返回默认值变量
load:从输入流中读取属性列表(键和元素对)
elements() 返回此向量的组件的枚举。
package ioDemo; import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.Properties; /**
* 合并文件
* 将多个已经拆分的文件合并成一个文件中
* Created by lcj on 2017/11/14.
*/
public class partFileDemo2 {
public static void main(String[] args) throws IOException{
//获取目标源文件
File dir = new File("D:\\partFiles");
// megeFile(dir);
megeFile2(dir);
} public static void megeFile2(File dir) throws IOException{
//获取指定目录下配合文件对象,通过过滤器
// File[] files = dir.listFiles(new SuffixFilter(".properties"));
File[] files = dir.listFiles(new SuffixFilter(".properties")); if (files.length !=1)
throw new RuntimeException(dir + "该目录下没有properties扩展名或者不唯一"); //记录配置文件对象
File confile = files[0];
//获取该文件中的信息
Properties prop = new Properties();
FileInputStream fis = new FileInputStream(confile);
///load:从输入流中读取属性列表(键和元素对)
prop.load(fis);
//getProperty:用指定的键在属性列表中搜索属性。如果在属性列表中未找到该键,则接着递归检查默认属性列表及其默认值。如果未找到属性,则此方法返回默认值变量。
String filename = prop.getProperty("filename");
int count = Integer.parseInt(prop.getProperty("partcount")); //获取该目录下的所有锁片文件
File[] partFile = dir.listFiles(new SuffixFilter(".part")); if (partFile.length !=(count-1))
{
throw new RuntimeException("碎片文件不符合要求,个数不对!应该" + count+"个");
}
///将碎片文件和流对象关联并存储到集合中
ArrayList<FileInputStream> al = new ArrayList<FileInputStream>();
for (int x = 0 ; x<partFile.length;x++)
{
al.add(new FileInputStream(partFile[x]));
}
//将多个流合并成一个序列流
//elements() 返回此向量的组件的枚举。
Enumeration<FileInputStream> en = Collections.enumeration(al);
//序列流,字节读取流
SequenceInputStream sis = new SequenceInputStream(en);
//将合并文件放在File目下
FileOutputStream fos = new FileOutputStream(new File(dir,filename));
byte[] buf = new byte[1024];
int len = 0;
while ((len=sis.read(buf))!=-1)
{
fos.write(buf,0,len);
}
fos.close();
sis.close();
} public static void megeFile(File dir) throws IOException{ ArrayList<FileInputStream> al = new ArrayList<FileInputStream>();
for (int x=1;x<=3;x++)
{
al.add(new FileInputStream(new File(dir,x+".txt")));
}
//elements() 返回此向量的组件的枚举。
Enumeration<FileInputStream> en = Collections.enumeration(al);
//序列流,字节读取流
SequenceInputStream sis = new SequenceInputStream(en);
//将合并文件放在File目下
FileOutputStream fos = new FileOutputStream(new File(dir,"FileDemo.txt"));
byte[] buf = new byte[1024];
int len = 0;
while ((len=sis.read(buf))!=-1)
{
fos.write(buf,0,len);
}
fos.close();
sis.close();
}
}
SuffixFilter 类实现过滤文件
package ioDemo; import java.io.File;
import java.io.FilenameFilter; /**
* Created by lcj on 2017/11/15.
*/
public class SuffixFilter implements FilenameFilter {
private String suffix; public SuffixFilter(String suffix) {
super();
this.suffix = suffix;
} @Override
public boolean accept(File dir, String name) {
return name.endsWith(suffix);
}
}
三、SequenceInputStream序列流实现多个文件合并成一个文件
Vector 类可以实现可增长的对象数组,
FileInputStream(输入流):输入流是用来读入数据的
OutputStream(输出流):输出流是用来写出数据的,
elements() 返回此向量的组件的枚举。
package ioDemo; import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.SequenceInputStream;
import java.util.Enumeration;
import java.util.Vector; /**
* SequenceInputStream 序列流
*将多个文件中数据合并到一个文件中
* Created by lcj on 2017/11/13.
*/
public class SequenceInputStreamDemo {
public static void main(String[] args) throws IOException{
/**
* 将多个文件中数据合并到一个文件中
* */
//Vector 类可以实现可增长的对象数组,FileInputStream(输入流):输入流是用来读入数据的
Vector<FileInputStream> v = new Vector<FileInputStream>();
v.add(new FileInputStream("D:\\Project\\IdeaProjects\\test01_time\\src\\main\\java\\ioDemo\\1.txt"));
v.add(new FileInputStream("D:\\Project\\IdeaProjects\\test01_time\\src\\main\\java\\ioDemo\\2.txt"));
v.add(new FileInputStream("D:\\Project\\IdeaProjects\\test01_time\\src\\main\\java\\ioDemo\\3.txt"));
//elements() 返回此向量的组件的枚举。
Enumeration<FileInputStream> en = v.elements();
//序列流,字节读取流
SequenceInputStream sis = new SequenceInputStream(en);
//OutputStream(输出流):输出流是用来写出数据的,
FileOutputStream fos = new FileOutputStream("D:\\Project\\IdeaProjects\\test01_time\\src\\main\\java\\ioDemo\\04.txt");
byte[] buf = new byte[1024];
int len = 0;
while ((len = sis.read(buf))!=-1)
{
fos.write(buf,0,len);
}
fos.close();
sis.close();
}
}
IO流(SequenceInputStream序列流--文件拆分与合并)的更多相关文章
- IO流_SequenceInputStream(序列流)
SequenceInputStream(序列流):就是将多个流合成一个有序的流 需求:将三个文件中的数据合并到一个文件中 import java.io.FileInputStream; import ...
- (20)IO流之SequenceInputStream 序列流
序列流,对多个流进行合并. SequenceInputStream 表示其他输入流的逻辑串联.它从输入流的有序集合开始,并从第一个输入流开始读取,直到到达文件末尾,接着从第二个输入流读取,依次类推,直 ...
- Java基础---IO(二)--File类、Properties类、打印流、序列流(合并流)
第一讲 File类 一.概述 1.File类:文件和目录路径名的抽象表现形式 2.特点: 1)用来将文件或文件夹封装成对象 2)方便于对文件与文件夹的属性信息进行操作 3)File类的实例是不 ...
- JAVA学习第五十四课 — IO流(八)打印流 & 序列流
一.综合练习-文件清单列表 获取指定文件夹下,指定扩展名的文件(含子文件夹),并将这些文件的绝对路径写到一个文本文件里.也就是建立一个指定扩展名的文件列表 1.深度遍历 2.过滤器->容器 3. ...
- fasta文件拆分与合并
Linux中fasta文件的拆分与合并 FASTA文件的拆分: (1)如果从一个文件a提取第11至20个序列存到另一个文件b: awk -v RS='>' 'NR>1{i++}i>= ...
- RedHat/CentOS 大文件拆分及合并与md5验证
[root@tdh55 mnt]# cd /opt/[root@tdh55 opt]# ll -h-rw-r--r--. 1 root root 7.5G May 12 11:19 TDH-Image ...
- Java之序列流SequenceInputStream
序列流:作用就是将多个读取流合并成一个读取流,实现数据的合并 序列流表示其他输入流的逻辑串联.它从输入流的有序集合开始,并从第一个输入流开始读取,直到文件的末尾,接着从第二个输入流读取,以此类推:这样 ...
- java基础48 IO流技术(序列流)
本文知识点目录: 1.SequenceInputStream序列流的步骤 2.实例 3.附录(音乐的切割与合并) 1.SequenceInputStream序列流的步骤 1.找到目标文件 ...
- JAVA IO分析三:IO总结&文件分割与合并实例
时间飞逝,马上就要到2018年了,今天我们将要学习的是IO流学习的最后一节,即总结回顾前面所学,并学习一个案例用于前面所学的实际操作,下面我们就开始本节的学习: 一.原理与概念 一.概念流:流动 .流 ...
随机推荐
- docker-compose文件语法解析(v3.x)
文件配置 compose文件是一个定义服务(service).网络(network)和卷(volume)的YAML文件 .Compose 文件的默认路径是 ./docker-compose.yml 提 ...
- redis:哨兵集群配置
最少配置1主2从3哨兵 一.引言 上一篇文章我们详细的讲解了Redis的主从集群模式,其实这个集群模式配置很简单,只需要在Slave的节点上进行配置,Master主节点的配置不需要做任何更改,但是有一 ...
- Turtle库学习
Python Turtle (Python绘图工具) 导入库 import turtle as t ps:为了方便调用我们这里给这个模块在本程序内重命名为 t 1. 画布 顾名思义就是用于绘图的区域 ...
- Highlights in a Journal
** Highlights **** example- b huang, 2016, Design and performance enhancement of a bi-directional co ...
- 04004_使用JavaScript完成注册表单数据校验
1.需求分析 (1)用户在进行注册的时候会输入一些内容,但是有些用户会输入一些不合法的内容,这样会导致服务器的压力过大,此时我们需要对用户输入的内容进行一个校验(前端校验和后台校验): (2)前端校验 ...
- 大数据学习——hadoop安装
上传centOS6.7-hadoop-2.6.4.tar.gz 解压 tar -zxvf centOS6.7-hadoop-2.6.4.tar.gz hadoop相关修改配置 1 修改 /root/a ...
- 【ITOO 1】SQLBulkCopy实现不同数据库服务器之间的批量导入
导读:在做项目的时候,当实现了动态建库后,需要实现从本地服务器上获取数据,批量导入到新建库的服务器中的一个表中去.之前是用了一个SQL脚本文件实现,但那时候没能实现不同的数据库服务器,现在用了SqlB ...
- PTA 04-树6 Complete Binary Search Tree (30分)
题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/669 5-7 Complete Binary Search Tree (30分) A ...
- 在Ignite中使用线性回归算法
在本系列前面的文章中,简单介绍了一下Ignite的机器学习网格,下面会趁热打铁,结合一些示例,深入介绍Ignite支持的一些机器学习算法. 如果要找合适的数据集,会发现可用的有很多,但是对于线性回归来 ...
- Linux虚拟机fdisk分区
以下操作全部基于win7 64位系统上的Linux虚拟机(CentOS6.6). 当Linux虚拟机的硬盘空间不够用时,可以手动添加硬盘块,流程如下: 右键虚拟机,点击“Add”按钮: 选择“Hard ...