file类的常用方法


<strong><span style="font-family:KaiTi_GB2312;font-size:18px;">package com.hfxt;

import java.io.File;
import java.io.IOException;

//执行 创建文件  查询文件相关信息 删除文件
public class FileDemo {
	
	//创建文件
	public void create(File file) {
		//当文件不存在时,创建文件
		if (!file.exists()) {
			try {
				file.createNewFile();
				System.out.println("文件已创建!");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	//查看文件
	public void showInfo(File file){
		if(file.exists()){
			//文件存在,则查看其信息
			if(file.isFile()){
				System.out.println("该文件名:"+file.getName());
				System.out.println("相对路径:"+file.getPath());
				System.out.println("绝对路径:"+file.getAbsolutePath());
				System.out.println("文件大小:"+file.length()+"字节");
			}
			if(file.isDirectory()){
				System.out.println("此文件是目录");
			}
		}else{
			System.out.println("文件不存在!");
		}
	}
	
	//删除文件
	public void dele(File file){
		if(file.exists()){
			//如果文件存在
			file.delete();
			System.out.println("文件删除成功!");
		}else{
			System.out.println("文件删除失败!");
		}
	}
	public static void main(String[] args) {
		FileDemo fd = new FileDemo();
		File file = new File("F:/fangchen/test.txt");	 //新建文件储存路劲
		//创建文件
		fd.create(file);
		
		//查看文件
		//fd.showInfo(file);
		
		//删除文件
		//fd.dele(file);
	}
}
</span></strong>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值