import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;
public class MyFiles {
private MyFiles() {
}
//获取文件夹路径
public static File getDir() {
Scanner sc = new Scanner(System.in);
System.out.println("input path:");
while (true) {
File dir = new File(sc.nextLine());
if (!dir.exists()) {
System.out.println("not exist!");
} else if (dir.isFile()) {
System.out.println("is file!");
} else {
return dir;
}
}
}
//获取文件大小
public static String getSize() {
String sum = null;
double k = (double)getSizeFun(getDir())/ 1024;
double m = k / 1024;
double g = m / 1024;
if (k >= 1 && k < 1024) {
sum = k + ".k";
} else if (m >= 1 && m < 1024) {
sum = m + ".m";
} else if (g >= 1 && g < 1024) {
sum = g + ".g";
}
String[] arr = sum.split("\\.");
return arr[0] + "." + arr[1].substring(0, 2) + arr[2].toUpperCase();
}
private static long getSizeFun(File dir) {
if (dir == null) {
System.out.println("null");
return 0;
}
File[] subFiles = dir.listFiles();
long sum = 0;
for (File subFile : subFiles) {
if (subFile.isFile()) {
sum += subFile.length();
} else {
sum += getSizeFun(subFile);
}
}
return sum;
}
//删除
public static void delete() {
deleteFun(getDir());
System.out.println("delete successful!");
}
private static void deleteFun(File dir) {
if (dir == null) {
System.out.println("null");
return;
}
File[] subFiles = dir.listFiles();
for (File subFile : subFiles) {
if (subFile.isFile()) {
subFile.delete();
} else {
deleteFun(subFile);
}
}
dir.delete();
}
//复制
public static void copy() throws IOException {
System.out.println("input source:");
File src = getDir();
System.out.println("input destination:");
File dest = getDir();
copyFun(src,dest);
}
private static void copyFun(File src, File dest) throws IOException {
if (src == null || dest == null) {
System.out.println("null");
return;
}
File subDest = new File(dest,src.getName());
subDest.mkdir();
File[] subSrcs = src.listFiles();
for (File subSrc : subSrcs) {
if (subSrc.isFile()) {
fileCopy(subSrc, subDest);
} else {
copyFun(subSrc, subDest);
}
}
}
private static void fileCopy(File src,File dest) throws IOException {
if (src == null || dest == null) {
System.out.println("null");
return;
}
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(src));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File(dest, src.getName())));
int b;
while ((b = bis.read()) != -1) {
bos.write(b);
}
bis.close();
bos.close();
}
//分级打印目录
public static void print() {
printFun(getDir(),0);
}
private static void printFun(File dir,int lev) {
if (dir == null) {
System.out.println("null");
return;
}
File[] subFiles = dir.listFiles();
for (File subFile : subFiles) {
if (subFile.isFile()) {
for(int i = 0; i <= lev; i++) {
System.out.print("\t");
}
System.out.println(subFile.getName());
}
}
for (File subFile : subFiles) {
if (subFile.isDirectory()) {
for(int i = 0; i <= lev; i++) {
System.out.print("\t");
}
System.out.println(subFile.getName());
printFun(subFile,lev + 1);
}
}
}
}
|
|