67,513
社区成员
发帖
与我相关
我的任务
分享
public static boolean mergeFiles(String folder, String filename, int chunkNum, IUser user){
synchronized(user) {
String targetFile = folder + "/" + filename;
RandomAccessFile raf = null;
try {
//申明随机读取文件RandomAccessFile
raf = new RandomAccessFile(new File(targetFile), "rw");
//开始合并文件,对应切片的二进制文件
for (int i = 1; i <= chunkNum; i++) {
File chunkFile = new File(targetFile + "-" + i);
//读取切片文件
RandomAccessFile reader = new RandomAccessFile(chunkFile, "r");
byte[] b = new byte[10 * 1024 * 1024];
int n = 0;
try {
//先读后写
while ((n = reader.read(b)) != -1) {//读
raf.write(b, 0, n);//写
}
} catch(IOException e) {
e.printStackTrace();
return false;
} finally {
try {
//关闭流
reader.close();
//合并后删除分片文件
chunkFile.delete();
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
return false;
} finally {
try {
raf.close();
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
return true;
}
}
delete():
public static boolean deletefile(String delpath,IUser user){
synchronized(user) {
try {
System.gc();
File file = new File(delpath);
if(file.exists()) {
// 当且仅当此抽象路径名表示的文件存在且 是一个目录时,返回 true
if (!file.isDirectory()) {
file.delete();
} else if (file.isDirectory()) {
String[] filelist = file.list();
for (int i = 0; i < filelist.length; i++) {
File delfile = new File(delpath + "\\" + filelist[i]);
if (!delfile.isDirectory()) {
delfile.delete();
System.out.println(delfile.getAbsolutePath() + "删除文件成功");
} else if (delfile.isDirectory()) {
deletefile(delpath + "\\" + filelist[i]);
}
}
System.out.println(file.getAbsolutePath() + "删除成功");
file.delete();
}
} else {
System.out.println("文件不存在");
}
} catch (Exception e) {
System.out.println("deletefile() Exception:" + e.getMessage());
return false;
}
return true;
}
}
[/quote]
是滴[/quote]
明白了,感谢上面回复的各位大神啦
public static boolean mergeFiles(String folder, String filename, int chunkNum, IUser user){
synchronized(user) {
String targetFile = folder + "/" + filename;
RandomAccessFile raf = null;
try {
//申明随机读取文件RandomAccessFile
raf = new RandomAccessFile(new File(targetFile), "rw");
//开始合并文件,对应切片的二进制文件
for (int i = 1; i <= chunkNum; i++) {
File chunkFile = new File(targetFile + "-" + i);
//读取切片文件
RandomAccessFile reader = new RandomAccessFile(chunkFile, "r");
byte[] b = new byte[10 * 1024 * 1024];
int n = 0;
try {
//先读后写
while ((n = reader.read(b)) != -1) {//读
raf.write(b, 0, n);//写
}
} catch(IOException e) {
e.printStackTrace();
return false;
} finally {
try {
//关闭流
reader.close();
//合并后删除分片文件
chunkFile.delete();
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
return false;
} finally {
try {
raf.close();
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
return true;
}
}
delete():
public static boolean deletefile(String delpath,IUser user){
synchronized(user) {
try {
System.gc();
File file = new File(delpath);
if(file.exists()) {
// 当且仅当此抽象路径名表示的文件存在且 是一个目录时,返回 true
if (!file.isDirectory()) {
file.delete();
} else if (file.isDirectory()) {
String[] filelist = file.list();
for (int i = 0; i < filelist.length; i++) {
File delfile = new File(delpath + "\\" + filelist[i]);
if (!delfile.isDirectory()) {
delfile.delete();
System.out.println(delfile.getAbsolutePath() + "删除文件成功");
} else if (delfile.isDirectory()) {
deletefile(delpath + "\\" + filelist[i]);
}
}
System.out.println(file.getAbsolutePath() + "删除成功");
file.delete();
}
} else {
System.out.println("文件不存在");
}
} catch (Exception e) {
System.out.println("deletefile() Exception:" + e.getMessage());
return false;
}
return true;
}
}
[/quote]
是滴
public static boolean mergeFiles(String folder, String filename, int chunkNum, IUser user){
synchronized(user) {
String targetFile = folder + "/" + filename;
RandomAccessFile raf = null;
try {
//申明随机读取文件RandomAccessFile
raf = new RandomAccessFile(new File(targetFile), "rw");
//开始合并文件,对应切片的二进制文件
for (int i = 1; i <= chunkNum; i++) {
File chunkFile = new File(targetFile + "-" + i);
//读取切片文件
RandomAccessFile reader = new RandomAccessFile(chunkFile, "r");
byte[] b = new byte[10 * 1024 * 1024];
int n = 0;
try {
//先读后写
while ((n = reader.read(b)) != -1) {//读
raf.write(b, 0, n);//写
}
} catch(IOException e) {
e.printStackTrace();
return false;
} finally {
try {
//关闭流
reader.close();
//合并后删除分片文件
chunkFile.delete();
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
return false;
} finally {
try {
raf.close();
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
return true;
}
}
delete():
public static boolean deletefile(String delpath,IUser user){
synchronized(user) {
try {
System.gc();
File file = new File(delpath);
if(file.exists()) {
// 当且仅当此抽象路径名表示的文件存在且 是一个目录时,返回 true
if (!file.isDirectory()) {
file.delete();
} else if (file.isDirectory()) {
String[] filelist = file.list();
for (int i = 0; i < filelist.length; i++) {
File delfile = new File(delpath + "\\" + filelist[i]);
if (!delfile.isDirectory()) {
delfile.delete();
System.out.println(delfile.getAbsolutePath() + "删除文件成功");
} else if (delfile.isDirectory()) {
deletefile(delpath + "\\" + filelist[i]);
}
}
System.out.println(file.getAbsolutePath() + "删除成功");
file.delete();
}
} else {
System.out.println("文件不存在");
}
} catch (Exception e) {
System.out.println("deletefile() Exception:" + e.getMessage());
return false;
}
return true;
}
}