JAVA File类基本操作

博客主要围绕Java中的File类展开。File类在Java编程里是重要的基础内容,可用于文件和目录的操作等,在文件管理方面有重要作用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

File类

//exists()判断文件是否存在
//getName()返回文件名
//getAbsolutePath() 返回文件的绝对路径
//length()返回文件的大小
//getParentFile()找到父文件所在的位置
//mkdirs()创建目录文件
//delete()删除文件
import java.io.*;
public class T_File {

     public static void main(String[] args) throws IOException{
     //File f = new File("file");
     //File f = new File("C:"+File.separator+"h"+File.separator+"newfile.txt");
     //f.createNewFile();在C盘下一个叫h的目录文件下创建newfile.txt 
     File f1 = new File("new/new01","newfile01");
     if(f1.exists()) {
   
	   System.out.println("文件名:"+f1.getName());
	   System.out.println("文件绝对路径:"+f1.getAbsolutePath());
	   System.out.println("文件大小:"+f1.length());
	   System.out.println("文件最后修改时间:"+new Date(f1.lastModified());
	   System.out.println("文件");
	  }else {
	   f1.getParentFile().mkdirs();
	   try {
	    f1.createNewFile();
	   }catch (Exception e) {
	    e.printStackTrace();
	   }finally {
	    //f1.delete();
	   }
	   
	  }
	 }}
	 ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值