File文件方法使用

exists()                     //测试文件是否存在

getAbsolutePath()  //返回绝对路径名

getName()              //返回此文件名

getParent()             //返回上一级,如果没有上一级,就返回null

delete()                   //删除此对象文件

createNewFile()      //创建空文件,不创建文件夹

isDirectory()           //测试该对象表示的是否是目录

mkdir()                   //创建一个目录,它的路径由当前File对象指定

mkdirs()                 //创建包含父目录的目录


示例:创建一个新的文件

public void creat(File file){
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}

System.out.println("文件创建成功!");

示例:获取文件信息

public static void main(String[] args) {
File file = new File("
d:/11.txt");
try {
if (!file.
exists()) {
file.
createNewFile();
}else{
System.out.println("名字"+file.
getName());
System.out.println("相对路径"+file.
getPath());
System.out.println("绝对路径"+file.
getAbsolutePath());
System.out.println("字节数"+file.
length());
}
} catch (IOException e) {
e.printStackTrace();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值