c++歌手类代码_java开发之File类详细使用方法介绍

File类简介

在 Java 中,File 类是 包中唯一代表磁盘文件本身的对象。File 类定义了一些与平台无关的方法来操作文件,File类主要用来获取或处理与磁盘文件相关的信息,像文件名、 文件路径、访问权限和修改日期等,还可以浏览子目录层次结构。

File 类表示处理文件和文件系统的相关信息。也就是说,File 类不具有从文件读取信息和向文件写入信息的功能,它仅描述文件本身的属性。

File类的相关方法

1、构造方法

File(String pathname) 通过将给定路径名字符串转换为抽象路径名来创建一个新 File 实例。

File(String parent,String child) 根据指定的父路径和文件路径创建一个新File对象实例

File(File parent,String child) 根据指定的父路径对象和文件路径创建一个新的File对象实例

代码实现过程


/*
 - File的构造函数
 - */
 public static void main(String[] args) {
  //File(String pathname) 将指定路径名转换成一个File对象
  File file = new File("D:\\");
  (file);
  //File(String parent,String child) 根据指定的父路径和文件路径创建File对象
  File file1 = new File("D:\\a","");
  (file1);
  //File(File parent,String child) 根据指定的父路径对象和文件路径创建File对象
  File parent = new File("D:\\a");
  File file2 = new File(parent, "");
  (file2);
  File file3 = new File(new File("D:\\a"),"");
  (file3);

 }

运行结果

c9bc6ed9b8a65ac84e47d4b69c954555.png

2、File类创建和删除功能

boolean createNewFile();指定路径不存在该文件时创建文件,返回true 否则false

boolean mkdir() 当指定的单击文件夹不存在时创建文件夹并返回true 否则false

boolean mkdirs() 但指定的多级文件夹在某一级文件夹不存在时,创建多级文件夹并返回true 否则false

boolean delete() 删除文件或者删除单级文件夹

删除文件夹,这个文件夹下面不能有其他的文件和文件夹

代码实现过程


public static void main(String[] args) throws IOException {
 File file = new File("D:\\a\\");
 File file1 = new File("");
 boolean flag = ();
 (flag);

 File file2 = new File("b");
 boolean flag2 = ();
 (flag);

 File file3 = new File("c\\d\\e");
 boolean d = ();
 boolean c = s();
 (d);
 (c);
 File file4 = new File("");
 (());

 File file5 = new File("b");
 (());
}

运行结果

3659841e01b3c6a2d5402621ac0c03ea.png

3、File类的判断功能

boolean exists() 判断指定路径的文件或文件夹是否为空

boolean isAbsolute() 判断当前路径是否是绝对路径

boolean isDirectory() 判断当前的目录是否存在

boolean isFile() 判断当前的目录是否是一个文件

boolean isHidden() 判断当前路径是否是一隐藏文件

代码实现过程


public static void main(String[] args) throws IOException {
 // method();
 // method2();
 // method3();
 // method4();
}
//判断文件是否存在
public static void method() throws IOException {
 File file = new File("");
 ();
 boolean flag = ();
 (flag);
}
//判断当前路径是否为绝对路径
public static void method2() throws IOException{
 File file = new File("D:\\a\\");
 boolean flag = ();
 (flag);
}
//判断当前是文件夹还是文件
public static void method3() throws IOException{
 File file = new File("");
 File file1 = new File("b");
 ();
 boolean flag = ();
 boolean flag2 = ();
 (flag);
 (flag2);
}
//判断当前路径是否为隐藏文件
public static void method4() throws IOException{
 File file = new File("D:\\a\\");
 (());
}

运行结果

method()

method2()

method3()

cf07e82faad2e43c4b80d1161dd128fb.png

method4()

ef197fc64366d46a2fa71c9232781344.png

4、File类的获取功能和修改名字功能

File getAbsoluteFile() 获取文件的绝对路径,返回File对象

String getAbsolutePath() 获取文件的绝对路径,返回路径的字符串

String getParent() 获取当前路径的父级路径,以字符串形式返回该父级路径

String getName() 获取文件或文件夹的名称

String getPath() 获取File对象中封装的路径

long lastModified() 以毫秒值返回最后修改时间

long length() 返回文件的字节数

boolean renameTo(File dest) 将当前File对象所指向的路径修改为指定File所指向的路径

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值