File(文件)

File对象表示一个路径,可以是文件的路径,也可以是文件夹的路径。

这个路径可以存在,也允许不存在。

创建File对象的方法

public class test {
    public static void main(String [] args) {
    	//根据字符串创建文件
    	String str="C:\\Users\\PC\\Desktop\\a.txt";
    	File f=new File(str);
    	System.out.println(f);
    	//根据父路径和子路径创建文件
    	//父路径:C:\\Users\\PC\\Desktop
    	//子路径:a.txt
    	String parent="C:\\Users\\PC\\Desktop";
    	String child="a.txt";
    	File f1=new File(parent,child);
    	System.out.println(f1);
    	//把一个File表示的路径和String表示的路径进行拼接
    	File parent2=new File("C:\\Users\\PC\\Desktop");
    	String child2="a.txt";
    	File f2=new File(parent2,child2);
    	System.out.println(f2);
    }
}

File常见的成员方法

在电脑D盘中先创建了一个文件夹Date再在Date中创建了几个文件夹和文件

判断方法:

public class test {
    public static void main(String [] args) {
    	//对一个文件进行判断
    	File f1=new File("D:\\Date\\c.txt");
    	System.out.println(f1.isDirectory());//判断是否为文件夹
    	System.out.println(f1.isFile());//判断是否为文件
    	System.out.println(f1.exists());//判断是否存在
    	System.out.println("-------------");
    	//对一个文件夹进行判断
    	File f2=new File("D:\\Date\\aaa");
    	System.out.println(f2.isDirectory());
    	System.out.println(f2.isFile());
    	System.out.println(f2.exists());
    	System.out.println("-------------");
    	//对一个不存在路径进行判断
    	File f3=new File("D:\\Date\\abc");
    	System.out.println(f3.isDirectory());
    	System.out.println(f3.isFile());
    	System.out.println(f3.exists());
    	
    }
}

获取方法:

public class test {
    public static void main(String [] args) {
    	
    	File f1=new File("D:\\Date\\c.txt");
        long len=f1.length();//获取文件长度(字节数)
        System.out.println(len);
        System.out.println("---------");
        File f2=new File("\\c.txt");
        String s1 =f1.getAbsolutePath();
        String s2 =f2.getAbsolutePath();//获取文件绝对路径
        System.out.println(s1);
        System.out.println(s2);
        System.out.println("---------");
        String t1= f1.getPath();//返回文件使用时的路径,就是定义file的路径
        String t2= f2.getPath();
        System.out.println(t1);
        System.out.println(t2);
        System.out.println("---------");
        String n1= f1.getName();
        System.out.println(n1);
        File f3=new File("D:\\Date\\aaa");
        String n2=f3.getName();
        System.out.println(n2);
        System.out.println("---------");
        System.out.println(f1.lastModified());//最后修改时间(毫秒值)
    }
}

createNewFile()——创建一个新的空的文件

注:当前路径的表示的文件是不存在的,则创建成功,方法返回true,若文件已存在,则创建失败,方法返回false。

若父级路径是不存在的,那么方法会有异常。

createNewFile方法创建的一定是文件,若路径中不包含后缀名,则创建一个没有后缀名的文件。

public class test {
    public static void main(String [] args) throws IOException {
    	
    	File f1=new File("D:\\Date\\g.txt");
        boolean file=f1.createNewFile();
        System.out.println(file);
    }
}

mkdir()——创建单级文件夹

public class test {
    public static void main(String [] args) throws IOException {
    	
    	File f1=new File("D:\\Date\\ggg");
        boolean file=f1.mkdir();
        System.out.println(file);
    }
}

mkdirs()——创建多级文件夹

public class test {
    public static void main(String [] args) throws IOException {
    	
    	File f1=new File("D:\\Date\\ggg\\bbb\\sss");
        boolean file=f1.mkdirs();
        System.out.println(file);
    }
}

delete()——删除文件、文件夹

若删除的是文件,则直接删除不走回收站。

若删除的是空文件夹,则直接删除不走回收站。

若删除的是有内容的文件夹,则删除失败。

public class test {
    public static void main(String [] args) throws IOException {
    	
    	File f1=new File("D:\\Date\\g.txt");
        boolean b=f1.delete();
        System.out.println(b);
    }
}

public class test {
    public static void main(String [] args) throws IOException {
    	
    	File f1=new File("D:\\Date");
        File[] file= f1.listFiles();
        for (File file2 : file) {//使用增强for进行遍历
			System.out.println(file2);
		}
      
    }
}

 

综合练习

1.在当前模块下的aaa文件夹创建一个a.txt文件

public class test {
    public static void main(String [] args) throws IOException {
    	//先创建父级路径文件夹aaa
    	File f=new File("aaa");
    	f.mkdirs();//创建文件夹
    	String t="a.txt";
        File file=new File(f,t);//将父级路径和子级路径拼接
        boolean b=file.createNewFile();
        if(b) {
        	System.out.println("创建成功");
        }else {
        	System.out.println("创建失败");
        }
    }

	
}

2.定义一个方法找某个一个文件夹中,是否有def结尾的文件。

public class test {
    public static void main(String [] args) throws IOException {
    	File f1=new File("D:\\Date\\eee");
    	boolean b=find(f1);
    	System.out.println(b);
    }

	public static boolean find(File file) {
	
		File[] files=file.listFiles();//先把文件放入一个数组中,再进行遍历
		for(File f:files) {
			if(f.isFile()&&f.getName().endsWith(".def")) {
				return true;
			}
		}
		return false;
	}
}

3.找到电脑中D盘所有以def结尾的文件(考虑子文件夹)

分析:需要先遍历D盘,得到D盘的文件或文件夹,然后再依次遍历其中的文件或文件夹。此时需要递归的方法。

public class test {
    public static void main(String [] args)  {
    	File f=new File("D:\\");
    	find(f);
	}
    //查找方法
    public static void find(File file) {
    File[] files=file.listFiles();
    if(files!=null) {
    for(File f : files) {//遍历
	if(f.isFile()) {//f为文件
		if(f.getName().endsWith("def")) {
			System.out.println(f);
		}
	}else {//若f为文件夹,需再进行遍历
		find(f);
	}
     }
    }
    }
}

4.删除多级文件夹

分析:先删除文件夹中的内容,再删除自己。因为有多个文件夹,所以也使用递归方法。

public class test {
    public static void main(String [] args)  {
    	File f=new File("D:\\Date\\ggg");
    	delet(f);
	}
    //删除有内容的文件夹
    //先进入文件夹,再删除文件,
    public static void delet(File file) {
       File[] files=file.listFiles();
       for(File f:files) {//遍历文件夹
    	   if(f.isFile()) {
    		   f.delete();
    	   }else {
    		   delet(f);
    	   }
       }
       file.delete();//文件夹中无内容后将自己删掉
    }
}

5.统计文件夹的大小(其实就是文件夹中各个文件的大小)

分析:也用到了递归思想。

public class test {
    public static void main(String [] args)  {
    	File src=new File("D:\\Date\\aaa");
    	long len=getLen(src);
    	System.out.println(len);
	}
    //统计文件夹长度
    public static long getLen(File file) {
    	long len=0;
    	File[] files=file.listFiles();
    	for(File f:files) {
    		if(f.isFile()) {
    			len+=f.length();
    		}else {
    			len+=getLen(f);
    		}
    	}
    	return len;
    }
  
}

6.统计一个文件夹中的每种文件的个数并打印(考虑子文件夹),打印格式例如:txt:3个、doc:5个等等格式。

分析:利用File、递归、Map集合(键值对,键:文件名,值:个数)

1)先创建Map集合用来存放遍历文件的文件名和个数

2)if:先进入文件,遍历文件,判断是否为文件还是文件夹,利用getName()获取文件名字name,利用split()分割文件,成为数组。(例如:a.txt (a为arrr[0],txt为arr[1]))

 3)判断数组长度,长度小于2为没有后缀,不进入存储到集合中。

4)获取文件后缀名(txt/doc等等),判断后缀名是否存在于map集合中,若存在,获取此后缀名的值进行加加,然后存放到集合中,若不存在,则直接存放到集合中。

5)else:若进入的是文件夹,利用递归,创建一个新的sonmap集合,此集合已经遍历了文件夹中的文件,需要将此文件和个数加入到map中。

6)利用entryset遍历,获取键和值,将键与map集合比较,若存在,则获取map集合中的值,将map的值和sonmap的值相加,再存放到map中,若不存在,则直接存放到map中。

public class test {
    public static void main(String [] args)  {
    	File src=new File("D:\\Date\\aaa");
    	HashMap<String, Integer> hm=getcount(src);
    	System.out.println(hm);
	}
    //统计文件的个数
    //因为要输出(文件名=个数)所以用到了map集合的键值对
    public static HashMap<String, Integer> getcount(File file) {
    	HashMap<String, Integer> hm=new HashMap<>();
    	//进入文件夹
    	File[] files=file.listFiles();
    	for(File f:files) {
    		if(f.isFile()) {//文件
    			String name=f.getName();//先获取文件名字
    	        String[]str=name.split("\\.");//例如:t.txt,获取这个,才能获取键值对
    	        if(str.length>=2) {//防止没有后缀的文件,不加入集合中
    	        	//判断集合中是否存在这个键,存在:值加加;不存在:加入集合
    	        	String endname=str[str.length-1];//防止a.a.txt出现
    	        	if(hm.containsKey(endname)) {
    	        		//存在
    	        		int count=hm.get(endname);
    	        		count++;
    	        		hm.put(endname, count);
    	        	}else {
    	        		//不存在
    	        		hm.put(endname, 1);
    	        	}
    	        }
    		}else {//文件夹,递归
    			//若文件夹递归进去获得文件,但文件个数并未累加到map中
    			//需要在建立一个sonhm表示hm的子map
    			HashMap<String, Integer>sonhm=getcount(f);
    			//现在的sonhm中有遍历到的(文件名和个数)
    			//需要遍历sonhm
    		 Set<Entry<String, Integer>>entries=sonhm.entrySet();
    		 for(Entry<String, Integer>set:entries) {
    			String key=set.getKey();
    		    Integer value=set.getValue();
    		    //判断key在hm中是否存在
    		    if(hm.containsKey(key)){
    		    	//存在
    		    	int count=hm.get(key);
    		    	count=count+value;//将hm中的值和sonhm中的值相加
    		    	hm.put(key, count);
    		    }else {
    		    	//不存在
    		    	hm.put(key, 1);
    		    }
    		 }
    			
    		}
    	}
    	return hm;
    }
  
}

  • 8
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值