标签:
public static void SequenceDemo() throws IOException {
List<FileInputStream> list = new ArrayList<FileInputStream>();
list.add(new FileInputStream( new File("temp\\partfiles\\char1.txt" )));
list.add(new FileInputStream( new File("temp\\partfiles\\char2.txt" )));
list.add(new FileInputStream( new File("temp\\partfiles\\char3.txt" )));
SequenceInputStream sis = new SequenceInputStream(Collections.enumeration( list));
BufferedReader br = new BufferedReader( new InputStreamReader(sis));
String str = null;
while (( str = br. readLine()) != null) {
System.out.println( str);
}
br.close();
}
public static void printWrite() throws IOException {
BufferedReader br = new BufferedReader( new FileReader("temp\\char.txt" ));
PrintStream ps = new PrintStream(System.out, true );
String line = null;
while (( line = br.readLine()) != null) {
ps.println(line.toUpperCase());
}
br.close();
}
package io;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
public class KeyPrintDemo {
public static void main(String[] args) throws IOException {
// 需求:读取键盘上录入的字符,显示并以 utf-8编码保存成文件
BufferedReader br = new BufferedReader( new InputStreamReader(System.in ));
BufferedWriter bw =
new BufferedWriter( new OutputStreamWriter( new FileOutputStream("temp\\key.txt" ), "utf-8" ));
String str = null;
while (( str = br.readLine()) != null) {
if ("exit".equals( str)) {
break;
}
System.out.println( str);
bw.write(str);
bw.newLine();
bw.flush();
}
bw.close();
}
}
package bean;
public class Employee extends Person {
private static final long serialVersionUID = 201411261L;
/*
* transient: 瞬态,被该修饰符修饰的变量将不被序列化。同样地,静态变量也不被序列化。
*/
private transient int salary ;
public Employee(String name, int age, int salary) {
super(name, age);
this.salary = salary;
}
public Employee() {
super();
}
public int getSalary() {
return salary;
}
public void setSalary( int salary) {
this.salary = salary;
}
@Override
public int hashCode() {
final int prime = 31;
int result = super.hashCode();
result = prime * result + salary;
return result;
}
@Override
public boolean equals(Object obj ) {
if (this == obj) return true ;
if (!super.equals( obj)) return false ;
if (getClass() != obj.getClass()) return false ;
Employee other = (Employee) obj;
if (salary != other.salary) return false;
return true;
}
@Override
public String toString() {
return "Employee [Name=" + getName() + ", Age=" + getAge() + ", Salary=" + salary + "]" ;
}
public static void staticPrint() {
System.out.println( "Static print run!");
}
}
public static Object objectRead() throws IOException, ClassNotFoundException {
ObjectInputStream ois = new ObjectInputStream( new FileInputStream("temp\\os.txt" ));
Object obj = ois.readObject();
ois.close();
return obj;
}
public static void objectWrite() throws IOException {
Employee employee = new Employee( "Java", 25, 25000);
ObjectOutputStream oos = new ObjectOutputStream( new FileOutputStream("temp\\os.txt" ));
oos.writeObject( employee);
oos.close();
}
public static int dataRead() throws IOException {
DataInputStream dis = new DataInputStream( new FileInputStream("temp\\data.txt" ));
int num = dis.readInt();
dis.close();
return num;
}
public static void dataWrite() throws IOException {
DataOutputStream dos = new DataOutputStream( new FileOutputStream("temp\\data.txt" ));
dos.writeInt(97); // 写入4字节:00000000 00000000 00000000 01100001
dos.write(97); // 写入1字节
dos.close();
}
private static byte[] byteArray(byte[] b) {
ByteArrayInputStream bis = new ByteArrayInputStream(b);
ByteArrayOutputStream bos = new ByteArrayOutputStream(); // 内部有一个可自动增长的数组。
int ch = 0;
while(( ch= bis.read())!=-1){
bos.write(ch);
}
//因为没有调用底层资源,所以不要关闭,即使调用了close,也没有任何效果,不会抛出IOException。
return bos.toByteArray();
}
public static void readFile() throws IOException {
RandomAccessFile raf = new RandomAccessFile("temp\\random.txt" , "r" );
raf.seek(4);
int i = raf.readInt();
System.out.println( i);
raf.close();
}
public static void writeFile() throws IOException {
RandomAccessFile raf = new RandomAccessFile("temp\\random.txt" , "rw" );
raf.seek(4);
raf.writeInt(102);
System.out.println( raf.getFilePointer());
raf.close();
}
package io;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.SequenceInputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Properties;
/**
* 使用配置文件的方法分割合并文件
*/
public class TSplitJoinByConfig {
private static final int BUFFER_SIZE = 4096;
/**
* 以配置文件的方法分割文件
* @param srcFile 需要分割的文件
* @param destPath 分割文件的保存目录
* @param len 分割文件的大小
* @throws IOException
*/
public void split(File srcFile, File destPath, long len) throws IOException {
// 健壮性判断,源文件目的目录是否存在
if (!srcFile.exists() || srcFile.isDirectory()) {
throw new RuntimeException( "源文件不存在!" );
}
if (len <= 1024) {
throw new RuntimeException( "分卷大小设置太小或错误!" );
}
if (!destPath.exists() || destPath.isFile()) {
destPath.mkdirs();
}
// 定义缓冲区
byte[] buf = new byte[ BUFFER_SIZE];
// 定义分割文件序号
int num = 0;
// 定义缓冲区数据大小
int bufLen = 0;
// 定义单个分割文件剩余的写入字节数
long splitLen = len;
// 源文件名
String fileName = srcFile.getName();
FileInputStream fis = new FileInputStream( srcFile);
FileOutputStream fos = null;
while ( bufLen != -1) {
// 单趟外层循环完成一个分割文件的写入
splitLen = len;
// 创建输出流,确定分割文件名
fos = new FileOutputStream( new File( destPath, fileName + ".part" + String.format( "%02d", ++num )));
while ( splitLen > 0) {
// splitLen为该单个文件剩余的写入字节数,以此为依据来从输入流读取数据到缓冲区
if ( splitLen >= BUFFER_SIZE) {
bufLen = fis.read( buf);
} else {
bufLen = fis.read( buf, 0, ( int) splitLen);
}
if ( bufLen == -1) {
break;
}
fos.write( buf, 0, bufLen);
splitLen -= ( long) bufLen;
}
fos.close();
}
fis.close();
// 将文件名和分割文件数写入配置文件
Properties prop = new Properties();
prop.setProperty( "FileName", srcFile .getName());
prop.setProperty( "PartNo.", String.valueOf( num));
FileWriter fw = new FileWriter( new File( destPath, srcFile.getName() + ".properties" ));
prop.store(fw, "Split File Config");
fw.close();
}
public void join(File splitFirFile) throws IOException {
// 健壮性判断
if (!splitFirFile.exists() || splitFirFile.isDirectory()) {
throw new RuntimeException( "分割文件不存在!" );
}
// 分割文件名(不包括扩展名)
String splitFileName = splitFirFile.getName().substring(0, splitFirFile.getName().length() - 7);
// 分割文件目录
File splitFilePath = splitFirFile.getParentFile();
// 配置文件
File propFile = new File( splitFilePath, splitFileName + ".properties");
if (!propFile.exists() || propFile.isDirectory()) {
throw new RuntimeException( "配置文件不存在!" );
}
// 读取配置文件
Properties prop = new Properties();
FileReader fr = new FileReader( propFile);
prop.load(fr);
fr.close();
// 合并文件名
String fileName = prop.getProperty( "FileName");
int num = Integer.parseInt(prop.getProperty( "PartNo."));
// 判断分割文件是否完整
File[] splitFiles = new File[ num];
for (int i = 1; i <= num; i++) {
splitFiles[i - 1] = new File( splitFilePath, splitFileName + ".part" + String.format( "%02d", i ));
if (!splitFiles[i - 1].exists() || splitFiles[ i - 1].isDirectory()) {
throw new RuntimeException( "缺少分割文件:" + splitFiles[i - 1].getName());
}
}
// 将流加入集合,并使用序列输入流完成合并
List<FileInputStream> list = new ArrayList<FileInputStream>();
for (int i = 1; i <= num; i++) {
list.add(new FileInputStream( splitFiles[ i - 1]));
}
byte[] buf = new byte[ BUFFER_SIZE];
int len = -1;
SequenceInputStream sis = new SequenceInputStream(Collections.enumeration( list));
FileOutputStream fos = new FileOutputStream( new File(splitFilePath, fileName ));
while (( len = sis.read( buf)) != -1) {
fos.write(buf, 0, len);
}
fos.close();
sis.close();
}
}
package io;
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.List;
public class TSplitJoin {
private static final int BUFFER_SIZE = 4096;
private static final long PROPERTIES_SIZE = 1024;
/**
* 分割文件,必要信息存储到第一个文件中
* @param srcFile 需要分割的文件
* @param destPath 分割文件的保存目录
* @param len 分割文件的大小
* @throws IOException
*/
public void split(File srcFile, File destPath, long len) throws IOException {
// 健壮性判断,源文件目的目录是否存在
if (!srcFile.exists() || srcFile.isDirectory()) {
throw new RuntimeException( "源文件不存在!" );
}
if (len <= 4096) {
throw new RuntimeException( "分卷大小设置太小或者错误!" );
}
if (!destPath.exists() || destPath.isFile()) {
destPath.mkdirs();
}
// 定义缓冲区
byte[] buf = new byte[ BUFFER_SIZE];
// 定义分割文件序号
int num = 0;
// 定义写入缓冲区数据大小
int bufLen = 0;
// 定义单个分割文件剩余的写入字节数
long splitLen = len - PROPERTIES_SIZE;
// 源文件名
String fileName = srcFile.getName();
FileInputStream fis = new FileInputStream( srcFile);
FileOutputStream fos = new FileOutputStream( new File(destPath, fileName + ".part01" ));
String content = fileName + "\n" + (int)Math.ceil(( double)srcFile .length() / (double) len) + "\n";
System.arraycopy(content .getBytes("utf-8" ), 0, buf , 0, content.getBytes().length );
fos.write(buf, 0, (int)PROPERTIES_SIZE);
while ( bufLen != -1) {
// 单趟外层循环完成一个分割文件的写入
// 创建输出流,确定分割文件名
if (num != 0) {
fos = new FileOutputStream( new File( destPath, fileName + ".part" + String.format( "%02d", ++num )));
} else {
num++;
}
while ( splitLen > 0) {
// splitLen为该单个文件剩余的写入字节数,以此为依据来从输入流读取数据到缓冲区
if ( splitLen >= BUFFER_SIZE) {
bufLen = fis.read( buf);
} else {
bufLen = fis.read( buf, 0, ( int) splitLen);
}
if ( bufLen == -1) {
break;
}
fos.write( buf, 0, bufLen);
splitLen -= ( long) bufLen;
}
fos.close();
splitLen = len;
}
fis.close();
}
public void join(File splitFirFile) throws IOException {
// 健壮性判断
if (!splitFirFile.exists() || splitFirFile.isDirectory()) {
throw new RuntimeException( "分割文件不存在!" );
}
// 分割文件名(不包括扩展名)
String splitFileName = splitFirFile.getName().substring(0, splitFirFile.getName().length() - 7);
// 分割文件目录
File splitFilePath = splitFirFile.getParentFile();
// 读取配置信息
byte[] buf = new byte[ BUFFER_SIZE];
FileInputStream fis = new FileInputStream(splitFirFile );
fis.read(buf, 0, (int)PROPERTIES_SIZE);
fis.close();
String[] prop = new String( buf, "utf-8").split("\n" );
String fileName = prop[0];
int num = Integer.parseInt(prop [1]);
// 判断分割文件是否完整
File[] splitFiles = new File[ num];
for (int i = 1; i <= num; i++) {
splitFiles[i - 1] = new File( splitFilePath, splitFileName + ".part" + String.format( "%02d", i ));
if (!splitFiles[i - 1].exists() || splitFiles[ i - 1].isDirectory()) {
throw new RuntimeException( "缺少分割文件:" + splitFiles[i - 1].getName());
}
}
// 将流加入集合,并使用序列输入流完成合并
List<FileInputStream> list = new ArrayList<FileInputStream>();
for (int i = 1; i <= num; i++) {
list.add(new FileInputStream( splitFiles[ i - 1]));
}
int len = -1;
SequenceInputStream sis = new SequenceInputStream(Collections.enumeration( list));
FileOutputStream fos = new FileOutputStream( new File(splitFilePath, fileName ));
sis.skip(PROPERTIES_SIZE);
while (( len = sis.read( buf)) != -1) {
fos.write(buf, 0, len);
}
fos.close();
sis.close();
}
}
public static void SequenceDemo() throws IOException {
List<FileInputStream> list = new ArrayList<FileInputStream>();
list.add(new FileInputStream( new File("temp\\partfiles\\char1.txt" )));
list.add(new FileInputStream( new File("temp\\partfiles\\char2.txt" )));
list.add(new FileInputStream( new File("temp\\partfiles\\char3.txt" )));
SequenceInputStream sis = new SequenceInputStream(Collections.enumeration( list));
BufferedReader br = new BufferedReader( new InputStreamReader(sis));
String str = null;
while (( str = br. readLine()) != null) {
System.out.println( str);
}
br.close();
}
public static void printWrite() throws IOException {
BufferedReader br = new BufferedReader( new FileReader("temp\\char.txt" ));
PrintStream ps = new PrintStream(System.out, true );
String line = null;
while (( line = br.readLine()) != null) {
ps.println(line.toUpperCase());
}
br.close();
}
package io;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
public class KeyPrintDemo {
public static void main(String[] args) throws IOException {
// 需求:读取键盘上录入的字符,显示并以 utf-8编码保存成文件
BufferedReader br = new BufferedReader( new InputStreamReader(System.in ));
BufferedWriter bw =
new BufferedWriter( new OutputStreamWriter( new FileOutputStream("temp\\key.txt" ), "utf-8" ));
String str = null;
while (( str = br.readLine()) != null) {
if ("exit".equals( str)) {
break;
}
System.out.println( str);
bw.write(str);
bw.newLine();
bw.flush();
}
bw.close();
}
}
package bean;
public class Employee extends Person {
private static final long serialVersionUID = 201411261L;
/*
* transient: 瞬态,被该修饰符修饰的变量将不被序列化。同样地,静态变量也不被序列化。
*/
private transient int salary ;
public Employee(String name, int age, int salary) {
super(name, age);
this.salary = salary;
}
public Employee() {
super();
}
public int getSalary() {
return salary;
}
public void setSalary( int salary) {
this.salary = salary;
}
@Override
public int hashCode() {
final int prime = 31;
int result = super.hashCode();
result = prime * result + salary;
return result;
}
@Override
public boolean equals(Object obj ) {
if (this == obj) return true ;
if (!super.equals( obj)) return false ;
if (getClass() != obj.getClass()) return false ;
Employee other = (Employee) obj;
if (salary != other.salary) return false;
return true;
}
@Override
public String toString() {
return "Employee [Name=" + getName() + ", Age=" + getAge() + ", Salary=" + salary + "]" ;
}
public static void staticPrint() {
System.out.println( "Static print run!");
}
}
public static Object objectRead() throws IOException, ClassNotFoundException {
ObjectInputStream ois = new ObjectInputStream( new FileInputStream("temp\\os.txt" ));
Object obj = ois.readObject();
ois.close();
return obj;
}
public static void objectWrite() throws IOException {
Employee employee = new Employee( "Java", 25, 25000);
ObjectOutputStream oos = new ObjectOutputStream( new FileOutputStream("temp\\os.txt" ));
oos.writeObject( employee);
oos.close();
}
public static int dataRead() throws IOException {
DataInputStream dis = new DataInputStream( new FileInputStream("temp\\data.txt" ));
int num = dis.readInt();
dis.close();
return num;
}
public static void dataWrite() throws IOException {
DataOutputStream dos = new DataOutputStream( new FileOutputStream("temp\\data.txt" ));
dos.writeInt(97); // 写入4字节:00000000 00000000 00000000 01100001
dos.write(97); // 写入1字节
dos.close();
}
private static byte[] byteArray(byte[] b) {
ByteArrayInputStream bis = new ByteArrayInputStream(b);
ByteArrayOutputStream bos = new ByteArrayOutputStream(); // 内部有一个可自动增长的数组。
int ch = 0;
while(( ch= bis.read())!=-1){
bos.write(ch);
}
//因为没有调用底层资源,所以不要关闭,即使调用了close,也没有任何效果,不会抛出IOException。
return bos.toByteArray();
}
public static void readFile() throws IOException {
RandomAccessFile raf = new RandomAccessFile("temp\\random.txt" , "r" );
raf.seek(4);
int i = raf.readInt();
System.out.println( i);
raf.close();
}
public static void writeFile() throws IOException {
RandomAccessFile raf = new RandomAccessFile("temp\\random.txt" , "rw" );
raf.seek(4);
raf.writeInt(102);
System.out.println( raf.getFilePointer());
raf.close();
}
package io;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.SequenceInputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Properties;
/**
* 使用配置文件的方法分割合并文件
*/
public class TSplitJoinByConfig {
private static final int BUFFER_SIZE = 4096;
/**
* 以配置文件的方法分割文件
* @param srcFile 需要分割的文件
* @param destPath 分割文件的保存目录
* @param len 分割文件的大小
* @throws IOException
*/
public void split(File srcFile, File destPath, long len) throws IOException {
// 健壮性判断,源文件目的目录是否存在
if (!srcFile.exists() || srcFile.isDirectory()) {
throw new RuntimeException( "源文件不存在!" );
}
if (len <= 1024) {
throw new RuntimeException( "分卷大小设置太小或错误!" );
}
if (!destPath.exists() || destPath.isFile()) {
destPath.mkdirs();
}
// 定义缓冲区
byte[] buf = new byte[ BUFFER_SIZE];
// 定义分割文件序号
int num = 0;
// 定义缓冲区数据大小
int bufLen = 0;
// 定义单个分割文件剩余的写入字节数
long splitLen = len;
// 源文件名
String fileName = srcFile.getName();
FileInputStream fis = new FileInputStream( srcFile);
FileOutputStream fos = null;
while ( bufLen != -1) {
// 单趟外层循环完成一个分割文件的写入
splitLen = len;
// 创建输出流,确定分割文件名
fos = new FileOutputStream( new File( destPath, fileName + ".part" + String.format( "%02d", ++num )));
while ( splitLen > 0) {
// splitLen为该单个文件剩余的写入字节数,以此为依据来从输入流读取数据到缓冲区
if ( splitLen >= BUFFER_SIZE) {
bufLen = fis.read( buf);
} else {
bufLen = fis.read( buf, 0, ( int) splitLen);
}
if ( bufLen == -1) {
break;
}
fos.write( buf, 0, bufLen);
splitLen -= ( long) bufLen;
}
fos.close();
}
fis.close();
// 将文件名和分割文件数写入配置文件
Properties prop = new Properties();
prop.setProperty( "FileName", srcFile .getName());
prop.setProperty( "PartNo.", String.valueOf( num));
FileWriter fw = new FileWriter( new File( destPath, srcFile.getName() + ".properties" ));
prop.store(fw, "Split File Config");
fw.close();
}
public void join(File splitFirFile) throws IOException {
// 健壮性判断
if (!splitFirFile.exists() || splitFirFile.isDirectory()) {
throw new RuntimeException( "分割文件不存在!" );
}
// 分割文件名(不包括扩展名)
String splitFileName = splitFirFile.getName().substring(0, splitFirFile.getName().length() - 7);
// 分割文件目录
File splitFilePath = splitFirFile.getParentFile();
// 配置文件
File propFile = new File( splitFilePath, splitFileName + ".properties");
if (!propFile.exists() || propFile.isDirectory()) {
throw new RuntimeException( "配置文件不存在!" );
}
// 读取配置文件
Properties prop = new Properties();
FileReader fr = new FileReader( propFile);
prop.load(fr);
fr.close();
// 合并文件名
String fileName = prop.getProperty( "FileName");
int num = Integer.parseInt(prop.getProperty( "PartNo."));
// 判断分割文件是否完整
File[] splitFiles = new File[ num];
for (int i = 1; i <= num; i++) {
splitFiles[i - 1] = new File( splitFilePath, splitFileName + ".part" + String.format( "%02d", i ));
if (!splitFiles[i - 1].exists() || splitFiles[ i - 1].isDirectory()) {
throw new RuntimeException( "缺少分割文件:" + splitFiles[i - 1].getName());
}
}
// 将流加入集合,并使用序列输入流完成合并
List<FileInputStream> list = new ArrayList<FileInputStream>();
for (int i = 1; i <= num; i++) {
list.add(new FileInputStream( splitFiles[ i - 1]));
}
byte[] buf = new byte[ BUFFER_SIZE];
int len = -1;
SequenceInputStream sis = new SequenceInputStream(Collections.enumeration( list));
FileOutputStream fos = new FileOutputStream( new File(splitFilePath, fileName ));
while (( len = sis.read( buf)) != -1) {
fos.write(buf, 0, len);
}
fos.close();
sis.close();
}
}
package io;
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.List;
public class TSplitJoin {
private static final int BUFFER_SIZE = 4096;
private static final long PROPERTIES_SIZE = 1024;
/**
* 分割文件,必要信息存储到第一个文件中
* @param srcFile 需要分割的文件
* @param destPath 分割文件的保存目录
* @param len 分割文件的大小
* @throws IOException
*/
public void split(File srcFile, File destPath, long len) throws IOException {
// 健壮性判断,源文件目的目录是否存在
if (!srcFile.exists() || srcFile.isDirectory()) {
throw new RuntimeException( "源文件不存在!" );
}
if (len <= 4096) {
throw new RuntimeException( "分卷大小设置太小或者错误!" );
}
if (!destPath.exists() || destPath.isFile()) {
destPath.mkdirs();
}
// 定义缓冲区
byte[] buf = new byte[ BUFFER_SIZE];
// 定义分割文件序号
int num = 0;
// 定义写入缓冲区数据大小
int bufLen = 0;
// 定义单个分割文件剩余的写入字节数
long splitLen = len - PROPERTIES_SIZE;
// 源文件名
String fileName = srcFile.getName();
FileInputStream fis = new FileInputStream( srcFile);
FileOutputStream fos = new FileOutputStream( new File(destPath, fileName + ".part01" ));
String content = fileName + "\n" + (int)Math.ceil(( double)srcFile .length() / (double) len) + "\n";
System.arraycopy(content .getBytes("utf-8" ), 0, buf , 0, content.getBytes().length );
fos.write(buf, 0, (int)PROPERTIES_SIZE);
while ( bufLen != -1) {
// 单趟外层循环完成一个分割文件的写入
// 创建输出流,确定分割文件名
if (num != 0) {
fos = new FileOutputStream( new File( destPath, fileName + ".part" + String.format( "%02d", ++num )));
} else {
num++;
}
while ( splitLen > 0) {
// splitLen为该单个文件剩余的写入字节数,以此为依据来从输入流读取数据到缓冲区
if ( splitLen >= BUFFER_SIZE) {
bufLen = fis.read( buf);
} else {
bufLen = fis.read( buf, 0, ( int) splitLen);
}
if ( bufLen == -1) {
break;
}
fos.write( buf, 0, bufLen);
splitLen -= ( long) bufLen;
}
fos.close();
splitLen = len;
}
fis.close();
}
public void join(File splitFirFile) throws IOException {
// 健壮性判断
if (!splitFirFile.exists() || splitFirFile.isDirectory()) {
throw new RuntimeException( "分割文件不存在!" );
}
// 分割文件名(不包括扩展名)
String splitFileName = splitFirFile.getName().substring(0, splitFirFile.getName().length() - 7);
// 分割文件目录
File splitFilePath = splitFirFile.getParentFile();
// 读取配置信息
byte[] buf = new byte[ BUFFER_SIZE];
FileInputStream fis = new FileInputStream(splitFirFile );
fis.read(buf, 0, (int)PROPERTIES_SIZE);
fis.close();
String[] prop = new String( buf, "utf-8").split("\n" );
String fileName = prop[0];
int num = Integer.parseInt(prop [1]);
// 判断分割文件是否完整
File[] splitFiles = new File[ num];
for (int i = 1; i <= num; i++) {
splitFiles[i - 1] = new File( splitFilePath, splitFileName + ".part" + String.format( "%02d", i ));
if (!splitFiles[i - 1].exists() || splitFiles[ i - 1].isDirectory()) {
throw new RuntimeException( "缺少分割文件:" + splitFiles[i - 1].getName());
}
}
// 将流加入集合,并使用序列输入流完成合并
List<FileInputStream> list = new ArrayList<FileInputStream>();
for (int i = 1; i <= num; i++) {
list.add(new FileInputStream( splitFiles[ i - 1]));
}
int len = -1;
SequenceInputStream sis = new SequenceInputStream(Collections.enumeration( list));
FileOutputStream fos = new FileOutputStream( new File(splitFilePath, fileName ));
sis.skip(PROPERTIES_SIZE);
while (( len = sis.read( buf)) != -1) {
fos.write(buf, 0, len);
}
fos.close();
sis.close();
}
}
标签:
原文地址:http://blog.csdn.net/u010388781/article/details/51167752