java基础—文件的切割与合并
2023-09-11 14:14:53 时间
文件的切割与合并
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.SequenceInputStream;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Vector;
public class FileSplieDemos {
public static void main(String[] args) throws Exception
{
File theReadPath = new File("C:\\Users\\Administrator\\Desktop\\测试文件夹\\凤舞九天.mp3");
String theOutPath ="C:\\Users\\Administrator\\Desktop\\测试文件夹\\abc\\";
// 调用自定义方法将一个图片文件进行切割
SplieFile(theReadPath,theOutPath,new File(".mp3"));
//调用自定义方法将这些被分割的文件合并起
String theMergerpathout = "C:\\Users\\Administrator\\Desktop\\测试文件夹\\abCOPY\\";
String themergerInputpath="C:\\Users\\Administrator\\Desktop\\测试文件夹\\abc\\";
String theoutformat = "MergerCopy.mp3";
MergerFile(themergerInputpath,theMergerpathout,theoutformat);
}
private static void MergerFile(String themergerInputpath,
String theMergerpathout, String theoutformat) throws Exception {
// TODO Auto-generated method stub
File fil = new File(theMergerpathout);
if(!(fil.isDirectory()))
{
fil.mkdirs();
}
//将文件来源封装对象
File file = new File(themergerInputpath);
//获取所有块片的文件
File[] names = file.listFiles();
//创建一个容器,将所有的文件名字储存
Vector<FileInputStream> v = new Vector<FileInputStream>();
for(int x=0;x<names.length;x++)
{
v.add(new FileInputStream(names[x]));
}
Enumeration<FileInputStream> en = v.elements();
//合并多个流对象
SequenceInputStream sis = new SequenceInputStream(en);
//创建输出流对象
BufferedOutputStream bos =
new BufferedOutputStream(new FileOutputStream(theMergerpathout+theoutformat));
int len = 0;
byte[] b = new byte[1024*1024];
while((len=sis.read(b))!=-1)
{
bos.write(b);
bos.flush();
}
}
private static void SplieFile(File file,String thepath,File format)
{
// 通过字节流并联需要切割的文件对象,并加入缓冲技术提高效率
System.out.println("切割文件功能启动");
File fil = new File(thepath);
if(!(fil.isDirectory()))
{
fil.mkdirs();
}
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
int number = 0;
try {
bis = new BufferedInputStream(new FileInputStream(file));
//初使化输出路径
byte[] b = new byte[1024*1024];
int len = 0;
while((len = bis.read(b))!=-1)
{
number++;
bos = new BufferedOutputStream(new FileOutputStream(thepath+number+format));
bos.write(b);
bos.flush();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
相关文章
- Java renameTo()重新命名此抽象路径名表示的文件
- java基础—IO流——复制一个文件到当前文件夹中
- java基础IO流使用读取一个文件中的文字输出到控制台上
- Java反编译生成java文件
- Java核心技术卷I基础知识3.3.4 Unicode和char类型
- 【Java】java基本知识
- 第十节:详细讲解一下Java多线程,随机文件
- 聊聊Java happens-before原则
- Java消息队列
- Java线程锁,synchronized、wait、notify详解--java 管程
- Java学习-022-Properties 文件数据写入
- Java高效读取大文件
- Java 追加写入文件
- Java之旅(2)—反射
- Java小白入门200例57之日期Date类详解
- java 下载文件
- java下载文件
- Java 中对象传入方法内赋值后,为何执行完方法后对象的值未改变呢?
- 【Java I/O 流】文件的操作——java.io.File 类详解