网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

java开发之File类详细使用方法介绍

File类简介

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

成都创新互联是专业的分宜网站建设公司,分宜接单;提供网站设计制作、成都网站建设,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行分宜网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!

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:\\1.txt");
  System.out.println(file);
  //File(String parent,String child) 根据指定的父路径和文件路径创建File对象
  File file1 = new File("D:\\a","1.txt");
  System.out.println(file1);
  //File(File parent,String child) 根据指定的父路径对象和文件路径创建File对象
  File parent = new File("D:\\a");
  File file2 = new File(parent, "1.txt");
  System.out.println(file2);
  File file3 = new File(new File("D:\\a"),"1.txt");
  System.out.println(file3);

 }

运行结果

java开发之File类详细使用方法介绍

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\\1.txt");
 File file1 = new File("1.txt");
 boolean flag = file1.createNewFile();
 System.out.println(flag);

 File file2 = new File("b");
 boolean flag2 = file2.mkdir();
 System.out.println(flag);

 File file3 = new File("c\\d\\e");
 boolean d = file1.mkdir();
 boolean c = file1.mkdirs();
 System.out.println(d);
 System.out.println(c);
 File file4 = new File("c.txt");
 System.out.println(file4.mkdir());

 File file5 = new File("b");
 System.out.println(file2.delete());
}

运行结果

java开发之File类详细使用方法介绍

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("a.txt");
 file.createNewFile();
 boolean flag = file.exists();
 System.out.println(flag);
}
//判断当前路径是否为绝对路径
public static void method2() throws IOException{
 File file = new File("D:\\a\\1.txt");
 boolean flag = file.isAbsolute();
 System.out.println(flag);
}
//判断当前是文件夹还是文件
public static void method3() throws IOException{
 File file = new File("1.txt");
 File file1 = new File("b");
 file1.mkdir();
 boolean flag = file.isDirectory();
 boolean flag2 = file1.isFile();
 System.out.println(flag);
 System.out.println(flag2);
}
//判断当前路径是否为隐藏文件
public static void method4() throws IOException{
 File file = new File("D:\\a\\1.txt");
 System.out.println(file.isHidden());
}

运行结果

method()

java开发之File类详细使用方法介绍

method2()

java开发之File类详细使用方法介绍

method3()

java开发之File类详细使用方法介绍

method4()

java开发之File类详细使用方法介绍

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

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

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

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

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

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

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

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

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

代码实现过程:

public static void main(String[] args) throws IOException {
 // method();
 // method2();
 // method3();
 // method4();
}
public static void method(){
 File file = new File("D:\\a\\1.txt");
 File file1 = new File("1.txt");
 //以File对象返回的形式返回当前File对象所指向的绝对路径
 System.out.println(file1.getAbsoluteFile());
 //返回File对象所指向的绝对路径
 System.out.println(file1.getAbsolutePath());
}
public static void method2() throws IOException {
 File file = new File("a.txt");
 File file1 = new File("b","c.txt");
 System.out.println(file1.createNewFile());

 File parent = new File("b");
 File file2 = new File(parent,"c.txt");
 if (!parent.exists()){
  parent.mkdirs();
 }
 System.out.println(file2.createNewFile());
 System.out.println(file2.getParent());
 System.out.println(file2.getParentFile());
}
public static void method3() throws IOException{
 File file = new File("1.txt");
 File file1 = new File("D:\\a\\1.txt");
 File file2 = new File("b");

 System.out.println(file.getName());
 System.out.println(file1.getName());
 System.out.println(file2.getName());

 System.out.println(file.getPath());
 System.out.println(file1.getPath());
 System.out.println(file2.getPath());

 System.out.println(file.lastModified());
 Date date = new Date(1556085068524L);
 System.out.println(date.toLocaleString());

 System.out.println(file.length());
 System.out.println(file2.length());
}
public static void method4() throws IOException{
 File file = new File("a.txt");
 File file1 = new File("e.txt");
 System.out.println(file.renameTo(file1));
}

运行结果

method()

java开发之File类详细使用方法介绍

method2()

java开发之File类详细使用方法介绍

method3()

java开发之File类详细使用方法介绍

method4()

java开发之File类详细使用方法介绍

5、File 类的其他获取功能

String[] list(); 以字符串的形式返回当前路径下所有的文件和文件夹的名称

File[] listFile 以File对象的形式返回当前路径下的所有文件和文件夹名称

Static File[] listRoots() 获取计算机中的所有盘符

代码实现过程

public static void main(String[] args) {
 //method();
 // method2();
 //method3();
}

public static void method(){
 File file = new File("b");
 File file1 = new File("D:\\QQ\\1916247350");
 File file2 = new File("e.txt");
 String[] files = file1.list();
 for (int i=0;i

运行结果

method()

java开发之File类详细使用方法介绍

method2()

java开发之File类详细使用方法介绍

method(3)

java开发之File类详细使用方法介绍

练习

1、输出指定目录下的所有java文件

public static void main(String[] args) {
 File file = new File("D:\\IDEA\\IDEAprogram");
 method(file);
}
public static void method(File file){
//判断文件是够是目录文件
 if (file.isDirectory()){
  File[] files = file.listFiles();
  for (File file1 : files) {
  //判断是否为文件
   if (file1.isFile()){
   //判断文件是否是以.java结尾的文件
    if (file1.getName().endsWith(".java")){
     System.out.println(file1.getName());
    }
   }else if (file1.isDirectory()){ 
   //回调,遍历file1
    method(file1);
   }
  }
 }
}

运行结果

java开发之File类详细使用方法介绍

2、删除指定的目录(包括子目录)

public static void main(String[] args) {
 File file = new File("D:\\a");
 method(file);
}
//删除指定目录下的文件
public static void method(File file){
 //删除自己所有的子文件和子目录
 //获取子文件和子目录
 if (file.isDirectory()){
  File[] files = file.listFiles();
  for (File file1 : files) {
   if (file1.isFile()){
    System.out.println(file1.getName());
    //干掉它
    file1.delete();
   }else if (file1.isDirectory()){
    //继续查看
    method(file1);
   }
  }
  //干掉自己
  System.out.println(file.getName());
  file.delete();
 }
}

代码运行结果

java开发之File类详细使用方法介绍

本文介绍了java开发过程中File类的详细使用方法,更多关于File类使用方法请查看下面的相关链接


网站名称:java开发之File类详细使用方法介绍
文章分享:http://bjjierui.cn/article/pcijgo.html

其他资讯