File的使用

1、声明类型:
有3种生成类型(使用示例如下)

package File1;

import java.io.File;

public class File11 {
	public static void main(String[] args) {
		String pathname="C:\\example.txt";
		//new File(String pathname)生成方式
		File file=new File(pathname);
		System.out.println(file.exists());
		//new File(String path,String filename)   path:文件的路径,不包括文件名       filename: 文件名
		File file1=new File("C:\\","example.txt");
		System.out.println(file1.exists());
		//new File(File file,String pathname)     file:父类文件的路径                      pathname:   子类文件的路径
		//用file类来生成pathname路径下的文件对象,相当于生成多一份文件
		File file2=new File(file,"C:example2");
		System.out.println(file2.exists());
	}

}

File 的使用方法:
创建方法:
boolean createNewFile() //创建一个新文件
boolean mkdir() //创建文件夹
boolean mkdirs() //创建文件的所有父文件跟子文件夹

删除方法:
boolean delete() //删除文件或者文件夹
boolean deleteOnExit() //文件使用完成后删除

判断方法:
boolean canExcute() //判断文件是否可以执行
boolean canRead() //判断文件是否可读
boolean canWrite() //判断文件是否可写
boolean exits() //判断文件是否存在
boolean idDirector() //判断文件是不是文件夹类型
boolean isFile() //判断该文件是否是一个正常的文件
boolean isAbsolute() //判断是不是绝对路径
boolean isHidden() //判断文件是否隐藏

获取方法:
String getName() //获取文件名
String getParent() //获取文件的父路径字符串
String getPath() //获取文件的相对路径
String getAbsolutePath() //获取文件的绝对路径
int length() //获取文件的长度
Date lastModified() //获取文件的最后修改日期

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值