1.从键盘接收一个文件夹路径,统计该文件夹大小
//第一种
public class Temp1_tj {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个文件夹路径:");
String path = sc.nextLine();
File file = new File(path);
long sum = getLength(file);
System.out.println("该文件夹大小为"+sum+"字节");
}
public static long getLength(File file){
if(file.isDirectory()){
File[] files = file.listFiles();
System.out.println(files.length);
long len = 0;
for (File file2 : files) {
len += getLength(file2);
}
return len;
}else if(file.isFile()){
return file.length();
}
return 0;
}
}
//第二种
public class Demo1
{
public static void main(String[] args)
{
//需求:1,从键盘接收一个文件夹路径,统计该文件夹大小
Scanner sc = new Scanner(System.in);
String dir = sc.nextLine();
/**
* 1. 首先接收进来的是一个路径(文件件) - 判断是否是文件夹-判断文件是否存在
* 2. 列出所有的子文件
* a. 如果子文件是一个文件,那么我们就直接统计他的大小
* b. 如果子文件是一个文件夹,那么我们应该接着列出他的所有的子文件
*/
long count = countFile(dir);
System.out.println(count);
}
static long count = 0;//统计所有文件的字节大小
private static long countFile(String dir)
{
File[] files = new File(dir).listFiles();
for(int i=0; i< files.length; i++)
{
if(files[i].isFile())
{
count += files[i].length();
}else
{
countFile(files[i].getPath());
}
}
return count;
}
}
2.从键盘接收一个文件夹路径,删除该文件夹
//第一种
public class Temp_delete {
//从键盘接收一个文件夹路径,删除该文件夹
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个文件夹路径:");
String path = sc.nextLine();
File file = new File(path);
del(file);
}
public static void del(File file){
if(file.isDirectory()){
File[] files = file.listFiles();
if(files.length==0){
file.delete();
}else{
for (File file2 : files) {
del(file2);
}
if(file.listFiles().length==0){
file.delete();
}
}
}else if(file.isFile()){
file.delete();
}
}
}
//第二种
public class Demo2
{
public static void main(String[] args)
{
//需求:2,从键盘接收一个文件夹路径,删除该文件夹
Scanner sc = new Scanner(System.in);
String dir = sc.nextLine();
File d = new File(dir);
deleteAll(d);
}
private static void deleteAll(File d)
{
if(d.isFile())
{
d.delete();
}else
{
File[] files = d.listFiles();
for(int i=0; i< files.length; i++)
{
if(files[i].isFile())
{
files[i].delete();
}else
{
deleteAll(files[i]);
}
}
//删除了当前文件夹下面的所有的文件的时候不要忘了删除自身
d.delete();
}
}
}
3.从键盘接收两个文件夹路径,把其中一个文件夹中(包含内容)拷贝到另一个文件夹中
public class Demo3
{
public static void main(String[] args) throws IOException
{
//需求:3,从键盘接收两个文件夹路径,把其中一个文件夹中(包含内容)拷贝到另一个文件夹中
Scanner sc = new Scanner(System.in);
String dir = sc.nextLine();
String dir1 = sc.nextLine();
File d = new File(dir);//需要拷贝的目录
File d1 = new File(dir1);//拷贝到的目录
copy(d,d1);
}
private static void copy(File d, File d1) throws IOException
{
File f = new File(d1,d.getName());
if(!f.exists())
{
f.mkdirs();
}
File[] files = d.listFiles();
for(int i=0; i< files.length; i++)
{
if(files[i].isFile())
{
//使用IO进行复制
BufferedInputStream in =
new BufferedInputStream(
new FileInputStream(files[i]));
BufferedOutputStream out =
new BufferedOutputStream(
new FileOutputStream(new File(f,files[i].getName())));
int c = 0;
while((c = in.read()) != -1)
{
out.write(c);
}
in.close();
out.flush();
out.close();
}else
{
//递归创建
copy(files[i],f);
}
}
}
}
4.从键盘接收一个文件夹路径,把文件夹中的所有文件以及文件夹的名字按层级打印, 例如: