黑马程序员_Java实现文件的切割和合并文件
2015-10-07 16:49
645 查看
------- <a href="http://www.itheima.com" target="blank">android培训</a>、<a href="http://www.itheima.com" target="blank">java培训</a>、期待与您交流! ----------
package Demo;
import java.io.*;
import java.util.*;
public class SplitFile {
/**
* 文件的切割和合并文件
* 枚举的使用方法
* arraylist和vector的区别
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//splitFile();
combineFile1();
}
public static void combineFile1()
{
//这种方法的效率比较高
ArrayList<FileInputStream> al = new ArrayList<FileInputStream>();
FileOutputStream fos = null;
SequenceInputStream sis = null;
try {
for (int i=1; i<5; i++)
{
al.add(new FileInputStream("F:\\zp\\"+i+".part"));
}
final Iterator it = al.iterator();
Enumeration en = new Enumeration() {
@Override
public boolean hasMoreElements() {
// TODO Auto-generated method stub
return it.hasNext();
}
@Override
public FileInputStream nextElement() {
// TODO Auto-generated method stub
return (FileInputStream) it.next();
}
};
sis = new SequenceInputStream(en);
fos = new FileOutputStream("F:\\zp\\3.jpg");
byte []buff = new byte[1024];
int len = 0;
while ((len=sis.read(buff)) != -1)
{
fos.write(buff,0,len);
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
finally
{
try {
sis.close();
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void combineFile()
{
//安全性比较高
Vector<FileInputStream> v = new Vector<FileInputStream>();
FileOutputStream fos = null;
SequenceInputStream sis=null;
try {
v.add(new FileInputStream("F:\\zp\\1.part"));
v.add(new FileInputStream("F:\\zp\\2.part"));
v.add(new FileInputStream("F:\\zp\\3.part"));
v.add(new FileInputStream("F:\\zp\\4.part"));
Enumeration e = v.elements();
sis = new SequenceInputStream(e);
fos = new FileOutputStream("F:\\zp\\2.jpg");
byte []buff = new byte[1024];
int len = 0;
while ((len=sis.read(buff)) != -1)
{
fos.write(buff,0,len);
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
finally
{
try {
sis.close();
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//对文件进行分割
public static void splitFile()
{
FileOutputStream fos=null;
FileInputStream fis = null;
try {
fis = new FileInputStream("F:\\zp\\1.jpg");
//分割的每一小块都是2M,不建议分割大的文件,会导致内存溢出
byte []buff = new byte[1024*1024*2];
int len = 0;
int count = 1;
while ((len=fis.read(buff)) != -1)
{
fos = new FileOutputStream("F:\\zp\\"+(count++)+".part");
fos.write(buff,0,len);
fos.close();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
try {
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
package Demo;
import java.io.*;
import java.util.*;
public class SplitFile {
/**
* 文件的切割和合并文件
* 枚举的使用方法
* arraylist和vector的区别
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//splitFile();
combineFile1();
}
public static void combineFile1()
{
//这种方法的效率比较高
ArrayList<FileInputStream> al = new ArrayList<FileInputStream>();
FileOutputStream fos = null;
SequenceInputStream sis = null;
try {
for (int i=1; i<5; i++)
{
al.add(new FileInputStream("F:\\zp\\"+i+".part"));
}
final Iterator it = al.iterator();
Enumeration en = new Enumeration() {
@Override
public boolean hasMoreElements() {
// TODO Auto-generated method stub
return it.hasNext();
}
@Override
public FileInputStream nextElement() {
// TODO Auto-generated method stub
return (FileInputStream) it.next();
}
};
sis = new SequenceInputStream(en);
fos = new FileOutputStream("F:\\zp\\3.jpg");
byte []buff = new byte[1024];
int len = 0;
while ((len=sis.read(buff)) != -1)
{
fos.write(buff,0,len);
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
finally
{
try {
sis.close();
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void combineFile()
{
//安全性比较高
Vector<FileInputStream> v = new Vector<FileInputStream>();
FileOutputStream fos = null;
SequenceInputStream sis=null;
try {
v.add(new FileInputStream("F:\\zp\\1.part"));
v.add(new FileInputStream("F:\\zp\\2.part"));
v.add(new FileInputStream("F:\\zp\\3.part"));
v.add(new FileInputStream("F:\\zp\\4.part"));
Enumeration e = v.elements();
sis = new SequenceInputStream(e);
fos = new FileOutputStream("F:\\zp\\2.jpg");
byte []buff = new byte[1024];
int len = 0;
while ((len=sis.read(buff)) != -1)
{
fos.write(buff,0,len);
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
finally
{
try {
sis.close();
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//对文件进行分割
public static void splitFile()
{
FileOutputStream fos=null;
FileInputStream fis = null;
try {
fis = new FileInputStream("F:\\zp\\1.jpg");
//分割的每一小块都是2M,不建议分割大的文件,会导致内存溢出
byte []buff = new byte[1024*1024*2];
int len = 0;
int count = 1;
while ((len=fis.read(buff)) != -1)
{
fos = new FileOutputStream("F:\\zp\\"+(count++)+".part");
fos.write(buff,0,len);
fos.close();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
try {
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
相关文章推荐
- java对世界各个时区(TimeZone)的通用转换处理方法(转载)
- java-注解annotation
- java-模拟tomcat服务器
- java-用HttpURLConnection发送Http请求.
- java-WEB中的监听器Lisener
- Android IPC进程间通讯机制
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- 介绍一款信息管理系统的开源框架---jeecg
- 聚类算法之kmeans算法java版本
- java实现 PageRank算法
- PropertyChangeListener简单理解
- 插入排序
- 冒泡排序
- 堆排序
- 快速排序
- 二叉查找树
- [原创]java局域网聊天系统