对Java中的File类的一些理解

java.Io.File类


java.Io.File类代表系统中的文件(目录和文件)。

以下通过一些代码来介绍File类中的一些常用方法。

1.java.Io.File.createNewFile()创建一个此路径的文件,创建成功返回True。若此文件已经存在返回false

 1 package com.sjj.io;
 2 
 3 import java.io.File;
 4 import java.io.IOException;
 5 
 6 public class FileIo {
 7     
 8     public static void main(String[] args)  {
 9         File file = new File("d:"+File.separator+"fileIo2.txt");
10         boolean bool;
11         try {
12             //此处会发生IO异常
13             bool = file.createNewFile();
14             System.out.println("createNewFile? "+bool);
15         } catch (IOException e) {
16             e.printStackTrace();
17         }
18     }
19 }
View Code

2.java.Io.File.exists()用于判断file路径是否存在。存在着返回True

 1 package com.sjj.io;
 2 
 3 import java.io.File;
 4 
 5 public class FileIo {
 6     
 7     public static void main(String[] args)  {
 8         File file = new File("d:"+File.separator+"fileIo.txt");
 9         boolean bool = file.exists();
10         if (bool) {
11             System.out.println("文件存在");
12         }else {
13             System.out.println("文件不存在");
14         }
15     }
16 }
View Code

3.java.Io.File.canRead()用于判断文件是否可读,可读返回True

 1 package com.sjj.io;
 2 
 3 import java.io.File;
 4 
 5 public class FileIo {
 6     
 7     public static void main(String[] args)  {
 8         File file = new File("d:"+File.separator+"fileIo.txt");
 9         boolean bool = file.canRead();
10         if (bool) {
11             System.out.println("文件可读");
12         }else {
13             System.out.println("文件不可读");
14         }
15     }
16 }
View Code

4.java.Io.File.canWrite()用于判断文件是否可写,可写返回True

 1 package com.sjj.io;
 2 
 3 import java.io.File;
 4 
 5 public class FileIo {
 6     
 7     public static void main(String[] args)  {
 8         File file = new File("d:"+File.separator+"fileIo.txt");
 9         boolean bool = file.canWrite();
10         if (bool) {
11             System.out.println("文件可写");
12         }else {
13             System.out.println("文件不可写");
14         }
15     }
16 }
View Code

 5.java.Io.File.delete()用于删除此路径定义的文件,返回True删除成功

 1 package com.sjj.io;
 2 
 3 import java.io.File;
 4 
 5 public class FileIo {
 6     
 7     public static void main(String[] args)  {
 8         File f = new File("d:"+File.separator+"fileIo.txt");
 9         boolean bool = f.delete();
10         if (bool) {
11             System.out.println("删除成功");
12         }else {
13             System.out.println("删除失败");
14         }
15     }
16 }
View Code

6.java.Io.File.isFile()用于判断此路径定义的文件是否为一个正常的文件,是则返回Ture

 1 package com.sjj.io;
 2 
 3 import java.io.File;
 4 
 5 public class FileIo {
 6     
 7     public static void main(String[] args)  {
 8         File file = new File("d:"+File.separator);
 9         boolean b = file.isFile();
10         if (b) {
11             System.out.println(file.getName()+"这是一个正常的文件");
12         }else {
13             file = new File("d:"+File.separator+"fileIo.txt");
14             b = file.isAbsolute();
15             if (b) {
16                 System.out.println(file.getName()+"这是一个正常的文件");
17             }
18         }
19     }
20 }
View Code

7.java.Io.File.isDirectory()用于判断此路径定义的文件是否为一个目录,是则返回True

 1 package com.sjj.io;
 2 
 3 import java.io.File;
 4 
 5 public class FileIo {
 6     
 7     public static void main(String[] args)  {
 8         File file = new File("d:"+File.separator);
 9         boolean b = file.isDirectory();
10         if (b) {
11             System.out.println(file.getPath()+"这是一个目录");
12         }
13     }
14 }
View Code

8.java.Io.File.list();此方法的返回值是一个String[]用于存储此目录下的所有文件与路径的文件名,若路径不是一个目录则返回null

 1 package com.qianfeng.io;
 2 
 3 import java.io.File;
 4 
 5 public class FileIo {
 6     
 7     public static void main(String[] args)  {
 8         File file = new File("d:"+File.separator);
 9         String[] fileName = file.list();
10         for (int i = 0; i < fileName.length; i++) {
11             System.out.println(fileName[i]);
12         }
13     }
14 }
View Code

9.java.Io.File.listFiles();此方法的返回值是一个File[],用于存储此目录下的所有文件与目录的路径

 1 package com.sjj.io;
 2 
 3 import java.io.File;
 4 
 5 public class FileIo {
 6     
 7     public static void main(String[] args)  {
 8         File file = new File("d:"+File.separator);
 9         File[] fileName = file.listFiles();
10         for (int i = 0; i < fileName.length; i++) {
11             System.out.println(fileName[i]);
12         }
13     }
14 }
View Code

10.java.Io.File.list(FilenameFilter filter);此方法通过文件过滤器来获取需要的文件名

 1 package com.sjj.io;
 2 
 3 import java.io.File;
 4 import java.io.FilenameFilter;
 5 
 6 public class FileIo {
 7     
 8     public static void main(String[] args)  {
 9         File file = new File("d:"+File.separator);
10         MyFile myFile = new MyFile(".txt");//返回后缀为txt的文件名
11         String[] fileStrings = file.list(myFile);
12         for (String string : fileStrings) {
13             System.out.println(string);
14         }
15     }
16     static class MyFile implements FilenameFilter{
17         private String type;
18         public MyFile(String type) {
19             this.type = type;
20         }
21         public boolean accept(File file, String name) {
22             
23             return name.endsWith(type);//返回True则文件合格。
24         }
25         
26     }
27 }
View Code

11.java.Io.File.listFiles(FilenameFilter filter);此方法通过文件过滤器来获取需要的路径

 1 package com.qianfeng.io;
 2 
 3 import java.io.File;
 4 import java.io.FilenameFilter;
 5 
 6 public class FileIo {
 7     
 8     public static void main(String[] args)  {
 9         File file = new File("d:"+File.separator);
10         MyFile myFile = new MyFile(".txt");//返回后缀为txt的路径
11         File[] fileStrings = file.listFiles(myFile);
12         for (File string : fileStrings) {
13             System.out.println(string);
14         }
15     }
16     static class MyFile implements FilenameFilter{
17         private String type;
18         public MyFile(String type) {
19             this.type = type;
20         }
21         public boolean accept(File file, String name) {
22             
23             return name.endsWith(type);//返回True则文件合格。
24         }
25         
26     }
27 }
View Code

转载于:https://www.cnblogs.com/Jhope/p/5193759.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值