day20/FileDemo.java

/*
File类常见方法:
1.创建
	boolean createNewFile()	 在指定位置创建文件,如果该文件已经存在,则不创建,返回false
								和输出流不一样,输流对象一建立创建文件,而且文件已经存在,会覆盖。

	boolean mkdir()		创建文件夹
	boolean mkdirs()	创建多级文件夹

2.删除
	boolean delete()		 删除失败返回false
	void deleteOnExit()		 在程序退出时删除指定文件 

3.判断
	boolean exists()	文件是否存在
	isFile()			要先用 exists() 方法判断文件是否存在,才能用 isFile() 方法判断是否是 文件
	isDirectory()		要先用 exists() 方法判断文件是否存在,才能用 isDirectory() 方法判断是否是 目录

	isHidden()
	isAbsolute()		是否是绝对路径

4.获取信息
	getName()
	getPath()
	getParent()

	getAbsolutePath()
	long lastModified()
	long length()


*/
import java.io.*;
class FileDemo 
{
	public static void main(String[] args) throws IOException
	{	
		//file();
		//method_1();
		//method_2();
		//method_3();
		method_4();
	}
	

	// boolean renameTo(File dest) 
	public static void method_4()
	{
		File f1 = new File("FileDemo.class");
		File f2 = new File("FileDemo_rename.class");

		boolean b = f1.renameTo(f2);//给文件重命名。可以从C盘到D盘。原理是剪切复制重命名
	}
	public static void method_3()
	{
		File f = new File("file.txt");

		System.out.println(f.getPath());
		System.out.println(f.getAbsolutePath());
		System.out.println(f.getParent());//该方法返回的是绝对路径中的父录。如果获取的是相对路径,返回null
										  //如果相对路径中有上一层目录,那么该目录就是返回结果。
	}

	public static void method_2() 
	{
		File f = new File("FileDemo.java");
		boolean b = f.canExecute();
		System.out.println(b);
		
	}

	public static void method_1()throws IOException
	{
		File f = new File("file.txt");

		//boolean b = f.createNewFile();
		//System.out.println(b);

		boolean b = f.delete();
		System.out.println(b);

		File dir = new File("aa\\bb\\cc\\cc\\dd");
		boolean b1 = dir.mkdirs();//创建多级文件夹

	}

	//创建File对象
	public static void file()
	{
		//将a.txt封装成File对象。可以将已有的和未出现的文件或者文件夹封装成对象。
		File f1 = new File("a.txt");

		File f2 = new File("c:\\abc\\b.txt");

		File f3 = new File("c:\\abc","c.txt");

		File f4 = new File("c:"+File.separator+"abc"+File.separator+"d.txt");//字段separator所属File类,是跨平台的分隔符

		
		System.out.println(f1);
		System.out.println(f2);
		System.out.println(f3);
		System.out.println(f4);
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值