首页 技术 正文
技术 2022年11月16日
0 收藏 992 点赞 4,360 浏览 2416 个字
package cn.stat.p1.file;import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.SequenceInputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.Properties;public class qiefiledemo {private static int SIZE=1024*1024;
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException { //文件切割
File file=new File("D:\\3.avi");
splitFile(file); //文件组合
File file2=new File("D:\\cc");
mergeFile(file2); }
//文件组合
public static void mergeFile(File dir) throws IOException
{
//读取配置文件
File[] files =dir.listFiles(new sufFilter(".properties")); //判断文件是否存在
if(files.length!=1)
throw new RuntimeException("文件不存在"); //创建文件流
File fl=files[0];
//获取文件信息
Properties prot=new Properties();
FileInputStream fis=new FileInputStream(fl);
prot.load(fis); String filename=prot.getProperty("filename");
int count=Integer.parseInt(prot.getProperty("patconut")); //获取目录下所有的碎片文件
File[] patfile=dir.listFiles(new sufFilter(".pat")); if(patfile.length!=count)
{
throw new RuntimeException("数目不对");
} ArrayList<FileInputStream> al=new ArrayList<FileInputStream>();
for(int i=0;i<patfile.length;i++)
{
al.add(new FileInputStream(new File(dir,i+".pat")));
} Enumeration<FileInputStream> en=Collections.enumeration(al);
SequenceInputStream sis=new SequenceInputStream(en);
FileOutputStream fos=new FileOutputStream(new File(dir,filename));
byte[] buf=new byte[SIZE];
int len=0;
while((len=sis.read(buf))!=-1)
{
fos.write(buf,0,len);
}
fos.close();
sis.close(); } //文件切割
public static void splitFile(File file) throws IOException
{
//用于读取流的关联文件
FileInputStream fis=new FileInputStream(file);
//定义一个1M的缓冲区
byte[] buf=new byte[SIZE]; //创建目地
FileOutputStream fos=null; //创建文件切割配置文件信息
Properties prop=new Properties(); int conut=0;
int len=0; File dir=new File("D:\\cc");
if(!dir.exists())
{
dir.mkdir();
} while((len=fis.read(buf))!=-1)
{
fos=new FileOutputStream(new File(dir,(conut++)+".pat"));
fos.write(buf,0,len);
} //创建配置文件
fos=new FileOutputStream(new File(dir,conut+".properties"));
prop.setProperty("patconut",conut+"");
prop.setProperty("filename",file.getName());
prop.store(fos,""); fos.close();
fis.close(); }}
package cn.stat.p1.file;import java.io.File;
import java.io.FilenameFilter;public class sufFilter implements FilenameFilter { private String suffix; @Override
public boolean accept(File dir, String name) {
// TODO Auto-generated method stub
return name.endsWith(suffix);
} public sufFilter(String suffix) {
super();
this.suffix = suffix;
}}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,088
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,564
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,413
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,186
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,822
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,905