文件的切割:
import java.io.*;
class SplitFilesDemo
{
public static void main(String[] args) throws Exception
{
File file = new File("a.avi");
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
FileOutputStream fos = null;
byte[] buffer = new byte[1024*1024*10];
int length= 0,count = 1;
while((length=bis.read(buffer))!=-1)
{
fos = new FileOutputStream("split\\"+count+++".part");
fos.write(buffer,0,length);
fos.close();
}
bis.close();
}
}
class SplitFilesDemo
{
public static void main(String[] args) throws Exception
{
File file = new File("a.avi");
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
FileOutputStream fos = null;
byte[] buffer = new byte[1024*1024*10];
int length= 0,count = 1;
while((length=bis.read(buffer))!=-1)
{
fos = new FileOutputStream("split\\"+count+++".part");
fos.write(buffer,0,length);
fos.close();
}
bis.close();
}
}
使用合并流对象SequenceInputStream对文件进行合并
import java.io.*;
import java.util.*;
class MergeFilesDemo
{
public static void main(String[] args) throws Exception
{
List<FileInputStream> files = new ArrayList<FileInputStream>();
getFiles(new File("split"),".part",files);
final Iterator<FileInputStream> it = files.iterator();
Enumeration<FileInputStream> em = new Enumeration<FileInputStream>(){
public boolean hasMoreElements()
{
return it.hasNext();
}
public FileInputStream nextElement()
{
return it.next();
}
};
SequenceInputStream sis = new SequenceInputStream(em);
FileOutputStream fos = new FileOutputStream(new File("b.avi"));
int length = 0;
byte[] buffer = new byte[1024*1024*10];
while((length=sis.read(buffer))!=-1)
{
fos.write(buffer,0,length);
}
fos.close();
sis.close();
}
public static void getFiles(File dir,String suffix,List<FileInputStream> listFiles) throws Exception
{
File[] files = dir.listFiles();
for(File file : files)
{
if(file.isDirectory())
{
getFiles(file,suffix,listFiles);
}
else
{
if(file.getName().endsWith(suffix))
listFiles.add(new FileInputStream(file));
}
}
}
}
import java.util.*;
class MergeFilesDemo
{
public static void main(String[] args) throws Exception
{
List<FileInputStream> files = new ArrayList<FileInputStream>();
getFiles(new File("split"),".part",files);
final Iterator<FileInputStream> it = files.iterator();
Enumeration<FileInputStream> em = new Enumeration<FileInputStream>(){
public boolean hasMoreElements()
{
return it.hasNext();
}
public FileInputStream nextElement()
{
return it.next();
}
};
SequenceInputStream sis = new SequenceInputStream(em);
FileOutputStream fos = new FileOutputStream(new File("b.avi"));
int length = 0;
byte[] buffer = new byte[1024*1024*10];
while((length=sis.read(buffer))!=-1)
{
fos.write(buffer,0,length);
}
fos.close();
sis.close();
}
public static void getFiles(File dir,String suffix,List<FileInputStream> listFiles) throws Exception
{
File[] files = dir.listFiles();
for(File file : files)
{
if(file.isDirectory())
{
getFiles(file,suffix,listFiles);
}
else
{
if(file.getName().endsWith(suffix))
listFiles.add(new FileInputStream(file));
}
}
}
}