[Java] 纯文本查看 复制代码
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.SequenceInputStream;
import java.util.Enumeration;
import java.util.Vector;
/**
*
*
* 有如下文件: 1.mp3 需求:把1.mp3,切割成10份小文件 提示:读取一个文件,写到10个目的地去即可!
*
*/
public class Demo05 {
public static void main(String[] args) throws IOException {
//========= 分割 ======================================/
File file = new File("1.mp3");
int partCount = 10; // 切割成10份
splitFile(file, partCount);
//========= 合并 ======================================/
String[] files = new String[partCount];
for (int i = 1; i <= partCount; i++) {
String pathname = String.format("part/part%02d", i);
files[i - 1] = pathname;
}
String outPath = "1_copy.mp3";
mergeFile(outPath,files);
}
private static void splitFile(File file, int partCount) throws IOException {
if (file.isFile()) {
//获取文件的总长度
long length = file.length();
InputStream fileInputStream = new FileInputStream(file);
File dir = new File("part");
dir.mkdir();
for (int i = 1; i <= partCount; i++) {
String child = String.format("part%02d", i);
File outFile = new File(dir, child);
byte[] buff = new byte[1024 * 8];
FileOutputStream fileOutputStream = new FileOutputStream(outFile);
for (int len = -1; (len = fileInputStream.read(buff)) != -1;) {
fileOutputStream.write(buff, 0, len);
if (outFile.length() > (length / partCount)) {
break;
}
}
fileOutputStream.close();
}
fileInputStream.close();
}
}
private static void mergeFile(String outPath,String... files) throws IOException {
Vector<FileInputStream> vector = new Vector<FileInputStream>();
for (int i = 0; i < files.length; i++) {
vector.add(new FileInputStream(files));
}
Enumeration<FileInputStream> elements = vector.elements();
SequenceInputStream sequenceInputStream = new SequenceInputStream(elements);
FileOutputStream fileOutputStream = new FileOutputStream(outPath, true);
byte[] buff = new byte[1024 * 8];
for (int len = 0; (len = sequenceInputStream.read(buff)) != -1;) {
fileOutputStream.write(buff, 0, len);
}
fileOutputStream.close();
sequenceInputStream.close();
System.out.println("文件合并完成!");
}
}
[AppleScript] 纯文本查看 复制代码
package myexam;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
/*
* 有如下文件: 1.mp3 需求:把1.mp3,切割成10份小文件 提示:读取
* 一个文件,写到10个目的地去即可!
*
*/
public class Test17 {
public static void main(String[] args) throws IOException {
/*
* 思路:一个读取流,对应多个输出流,生成的碎片文件要有有序的序号。
* 前提:1.mp3文件要存在
*/
File srcFile=new File("1.mp3");
File dirFile=new File("filesPlit");
fileSplit(srcFile, dirFile);
}
public static void fileSplit(File srcFile,File dirFile) throws IOException {
if(!dirFile.exists()){
dirFile.mkdir();
}
if(!srcFile.exists()){
throw new RuntimeException(srcFile.getAbsolutePath()+",源文件不存在!");
}
//1.读取源文件,定义流跟原文件相关联
FileInputStream fis=new FileInputStream(srcFile);
//2.创建一个目的引用,应为目的文件有多个,所以目的引用不确定
FileOutputStream fos=null;
//3.创建一个缓冲区。
//3.1 获取源文件大小
long fileLength=srcFile.length();
byte[] buf=new byte[(int)(fileLength/10)+1];//分成10份,这里切割文件个数不太准确,可以改动10
int count=0;
int len=0;
//4.往缓冲区存储数据
while((len=fis.read(buf))!=-1){
//5.创建输出流,明确要写入的文件对象
File partFile=new File(dirFile,(++count)+".part");
fos=new FileOutputStream(partFile);
fos.write(buf, 0, len);
fos.close();
}
//用Properties集合创建一个配置文件,用于存储源文件,和分割文件的信息
Properties prop=new Properties();
prop.setProperty("partcount",Integer.toString(count));
prop.setProperty("filename",srcFile.getName());
File configFile=new File(dirFile,(++count)+".properties");
fos=new FileOutputStream(configFile);
prop.store(fos, "save part file info");
fos.close();
fis.close();
}
}