1、java的包是不能导错的,导错基本就凉了,这点需要注意
2、new 出的流都要关闭,不然删不了。
package text;
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;
public class GH {
public static void main(String[] args) throws IOException, InterruptedException {
File sFile = new File("E:\\test\\放到");
if (!sFile.exists()) {
sFile.mkdirs();
}
gefile("F:\\图片哦/38449e9e78bc30000006.jpg", "E:\\test\\放到");
merge("E:\\test\\\\放到", "E:\\test\\新建文件夹");
Thread.sleep(1000);
ddelete("E:\\test\\放到");
sFile.deleteOnExit();
}
/**
* path1是要割的文件路径,path2是文件割完要放入的路径
*/
public static void gefile(String path1, String path2) throws IOException {
File file = new File(path1);
FileOutputStream fileOutputStream = null;
FileInputStream fileInputStream = new FileInputStream(file);
byte[] d = new byte[1024 * 10];
int len = 0;
int count = 1;
while ((len = fileInputStream.read(d)) != -1) {
/**
* 重点:new 出来的FileOutoutStream都要关闭
*/
fileOutputStream = new FileOutputStream(new File(path2, count++ + ".part"));
fileOutputStream.write(d);
fileOutputStream.close();
}
fileOutputStream.flush();
Properties properties = new Properties();
properties.setProperty("num", count + "");// 因为要字符串所以count要+""
properties.setProperty("filename", file.getName());
fileOutputStream = new FileOutputStream(new File(path2, count + ".properties"));
properties.store(fileOutputStream, "name");
fileInputStream.close();
fileOutputStream.close();
}
public static void merge(String path1, String path2) throws IOException, InterruptedException {
/**
* path1是已割文件的路径 path2是合完文件要放入的路径
*/
File p1 = new File(path1);
File[] part = p1.listFiles(new suffixfilter(".properties"));
File pFile = part[0];
Properties properties = new Properties();
FileInputStream fileInputStream = new FileInputStream(pFile);
properties.load(fileInputStream);// 把配置文件存到集合============
int k = Integer.parseInt(properties.getProperty("num"));
String string = properties.getProperty("filename");
//获取碎片文件
File[] parts = p1.listFiles(new suffixfilter(".part"));
ArrayList<FileInputStream> a1 = new ArrayList<FileInputStream>();
for (int i = 0; i < k - 1; i++) {
a1.add(new FileInputStream(parts[i]));
}
Enumeration<FileInputStream> enumeration = Collections.enumeration(a1);
SequenceInputStream sis = new SequenceInputStream(enumeration);
FileOutputStream fileOutputStream = new FileOutputStream((new File(path2, string)));
int len = -1;
byte[] s = new byte[1024 * 10];
int o = 0;
while ((len = sis.read(s)) != -1) {
System.out.println(++o);
fileOutputStream.write(s, 0, len);
}
fileOutputStream.flush();
fileInputStream.close();
fileOutputStream.close();
sis.close();
/**
* 删除合好的文件(图片)
*/
File d = new File(path2, string);
Thread.sleep(2000);// 设置图片存在的时间
d.delete();
}
//删除新建的文件(放到)
public static void ddelete(String path) throws IOException {
File p1 = new File(path);
File[] part = p1.listFiles(new suffixfilter(".properties"));
File pFile = part[0];
Properties properties = new Properties();
FileInputStream fileInputStream = new FileInputStream(pFile);
properties.load(fileInputStream);// 把配置文件存到集合============
int k = Integer.parseInt(properties.getProperty("num"));
String string = properties.getProperty("filename");
//获取碎片文件
File[] parts = p1.listFiles(new suffixfilter(".part"));
fileInputStream.close();
/**
* 删除part文件
*/
for (int i = 0; i < k - 1; i++) {
parts[i].delete();
}
/**
* 删除properties文件
*/
pFile.delete();
}
}
基本上就是这样子。
补充:过滤器
package text;
import java.io.File;
import java.io.FilenameFilter;
public class suffixfilter implements FilenameFilter {
private String cString;
public suffixfilter(String suffix) {
// TODO Auto-generated constructor stub
this.cString=suffix;
}
@Override
public boolean accept(File dir, String name) {
// TODO Auto-generated method stub
return name.endsWith(cString);
}
}