分享知识 传递快乐
文件操作工具实现类(接口方式)
package rename;
import java.io.IOException;
import java.util.ArrayList;
public interface FilesOperationInterface {
public abstract boolean copyFils(String sorFile, String toFile) throws IOException;
public abstract boolean deleteFiles(String delFile) throws IOException;
public abstract boolean renameFile(String oldFile, String newName) throws IOException;
public abstract boolean renameFile(String files, String type, String name) throws IOException;
public abstract boolean clipFile(String file, String toFiles) throws IOException;
public abstract ArrayList<String> findFileType(String findFiles, String type) throws IOException;
public abstract ArrayList<String> findFile(String findFiles, String file) throws IOException;
}
package rename;
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
public class FilesOperationTools implements FilesOperationInterface {
private static ArrayList<String> list =
null;
private int sum =
0;
public FilesOperationTools() {
super();
list =
new ArrayList<>();
list.add(
0,
"-1");
}
@Override
public boolean copyFils(String sorFile, String toFile) throws IOException {
boolean flag =
false;
File sorfile =
new File(sorFile);
File tofile =
new File(toFile);
FileInputStream fileInputStream =
null;
FileOutputStream fileOutputStream =
null;
if (sorfile.isFile()) {
fileInputStream =
new FileInputStream(sorfile);
File temp =
new File(tofile.getAbsolutePath().concat(File.separator).concat(sorfile.getName()));
fileOutputStream =
new FileOutputStream(temp);
byte[] by =
new byte[
1024 *
1000 *
2];
int len =
0;
while ((len = fileInputStream.read(by)) != -
1) {
fileOutputStream.write(by,
0, len);
}
fileInputStream.close();
fileOutputStream.close();
return true;
}
else {
File[] files = sorfile.listFiles();
if (files !=
null) {
File temp =
new File(tofile.getAbsolutePath().concat(File.separator).concat(sorfile.getName()));
temp.mkdir();
for (File file : files) {
flag = copyFils(file.getAbsolutePath(), temp.getAbsolutePath());
}
}
else {
System.out.println(
"文件夹下有特殊文件夹!可能是隐藏的垃圾桶文件。 " + sorfile.getAbsolutePath());
}
}
return flag;
}
@Override
public boolean deleteFiles(String delFile) throws IOException {
boolean flag =
false;
File delfile =
new File(delFile);
if (delfile.isFile()) {
return delfile.delete();
}
else {
File[] files = delfile.listFiles();
if (files !=
null) {
for (File file : files) {
flag = deleteFiles(file.getAbsolutePath());
}
delfile.delete();
}
else {
System.out.println(
"文件夹下有特殊文件夹!可能是隐藏的垃圾桶文件,不能删除! " + delfile.getAbsolutePath());
}
}
return flag;
}
@Override
public boolean renameFile(String oldFile, String newName) throws IOException {
boolean flag =
false;
File oldfile =
new File(oldFile);
File newfile =
new File(oldfile.getParent().concat(File.separator).concat(newName));
if (oldfile.isFile()) {
flag = oldfile.renameTo(newfile);
}
else {
System.out.println(
"指定的文件绝对路径有误! " + oldFile);
}
return flag;
}
@Override
public boolean renameFile(String files, String type, String name) throws IOException {
boolean flag =
false;
final String typ = type.contains(
".") ? type :
".".concat(type);
File file =
new File(files);
File[] fil = file.listFiles(
new FileFilter() {
@Override
public boolean accept(File path) {
return path.isFile() && path.getName().toLowerCase().endsWith((typ.toLowerCase()));
}
});
int i =
1;
for (File fi : fil) {
flag = fi.renameTo(
new File(fi.getParent().concat(File.separator).concat(name).concat(
"_") + (i++) + typ));
}
return flag;
}
@Override
public boolean clipFile(String file, String toFiles) throws IOException {
boolean flag =
false;
File sfile =
new File(file);
File tofile =
new File(toFiles);
if (sfile.isFile()) {
flag = sfile.renameTo(
new File(toFiles.concat(File.separator).concat(sfile.getName())));
}
else {
flag = copyFils(sfile.getAbsolutePath(), tofile.getAbsolutePath());
if (flag) {
flag = deleteFiles(sfile.getAbsolutePath());
}
}
return flag;
}
@Override
public ArrayList<String> findFileType(String findFiles, String type) throws IOException {
final String typ = type.contains(
".") ? type :
".".concat(type);
File findfiles =
new File(findFiles);
File[] findfile = findfiles.listFiles();
if (findfiles !=
null) {
for (File find : findfile) {
if (find.isDirectory()) {
findFileType(find.getAbsolutePath(), typ);
}
else {
if (find.getName().toLowerCase().endsWith(typ.toLowerCase())) {
list.add(find.getAbsolutePath());
sum++;
}
}
}
}
list.set(
0, String.valueOf(sum));
return list;
}
@Override
public ArrayList<String> findFile(String findFiles, String file) throws IOException {
File findfiles =
new File(findFiles);
if (
"*.".equals(file.substring(
0,
2))) {
findFileType(findFiles, file.substring(
1));
}
else {
getFile(findfiles, file);
}
list.remove(
0);
return list;
}
private void getFile(File findFiles, String file) {
File[] files = findFiles.listFiles();
if (files !=
null) {
for (File f : files) {
if (f.isFile() && f.getName().toLowerCase().contains(file.toLowerCase())) {
list.add(f.getAbsolutePath());
}
else {
if (f.getName().toLowerCase().contains(file.toLowerCase())) {
list.add(f.getAbsolutePath());
}
getFile(f, file);
}
}
}
}
}
package rename;
import java.io.IOException;
import java.util.ArrayList;
public class FileToolsTest {
public static void main(String[] args) throws IOException {
FilesOperationTools fileOperation =
new FilesOperationTools();
boolean f =
false;
ArrayList<String> lis = fileOperation.findFile(
"D:\\temp",
"*.mp3");
System.out.println(lis);
}
}