자바 로 파일 분할 및 병합
7575 단어 자바
package com.io.splitfile.demo;
import java.io.File;
import java.io.FileInputStream;
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;
// : , , partcount, filename。
public class SplitMergeFile {
private static final int SIZE = 1024*1024;
public static void main(String[] args) throws IOException {
File file = new File("C:\\Users\\Administrator\\Desktop\\test\\a.avi");
File dir = new File("C:\\Users\\Administrator\\Desktop\\test\\parties");
// splitFile(file);
String fileFormat = "avi";
mergeFile(dir,fileFormat);
}
public static void splitFile(File file) throws IOException{
// ,
String fileFormat = file.getName().substring( file.getName().lastIndexOf('.'));
//
File parentDir = file.getParentFile();
//
File destFile = new File(parentDir,"parties");
if(!destFile.exists()){
destFile.mkdirs();
}
FileInputStream fis = new FileInputStream( file);
byte[] buf = new byte[SIZE];
int len = 0;
int count = 1;
FileOutputStream fos = null;
while((len = fis.read(buf)) != -1){
fos = new FileOutputStream(new File(destFile,(count++) + fileFormat));
fos.write(buf, 0, len);
fos.close();
}
fis.close();
Properties prop = new Properties();
prop.setProperty("partcount",(count-1)+"");
prop.setProperty("filename", file.getName());
fos = new FileOutputStream(new File(destFile,count+".properties"));
prop.store(fos, "FileInformation");
fos.close();
}
public static void mergeFile(File dir,String fileFormat) throws IOException{
File[] files = dir.listFiles(new SuffixFilter(".properties"));
if(files.length != 1)
throw new NoFileException(" ");
File confile = files[0];
Properties prop = new Properties();
FileInputStream fis = new FileInputStream(confile);
prop.load(fis);
int count = Integer.parseInt(prop.getProperty("partcount"));
String filename = prop.getProperty("filename");
ArrayList al = new ArrayList();
//
File[] partFiles = dir.listFiles(new SuffixFilter("." + fileFormat));
if(count != partFiles.length)
throw new FileCountException(" , "+count+" ");
for(int i = 0; i < partFiles.length; i++ ){
al.add(new FileInputStream(partFiles[i]));
}
Enumeration 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();
}
}
class NoFileException extends RuntimeException{
NoFileException(){
super();
}
NoFileException(String msg){
super(msg);
}
}
class FileCountException extends RuntimeException{
FileCountException(){
super();
}
FileCountException(String msg){
super(msg);
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Is Eclipse IDE dying?In 2014 the Eclipse IDE is the leading development environment for Java with a market share of approximately 65%. but ac...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.