黑马程序员:File对象的基本功能

本文详细介绍了Java中File类的功能及使用方法,包括创建、删除文件/目录,判断文件属性,获取文件信息等实用技巧。

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

------- android培训java培训、期待与您交流! --------

File类:用来将文件或者文件夹封装成对象。
方便对文件与文件夹的属性信息进行操作。
File对象可以作为参数传递给流的构造函数


File类常见方法:
1.创建。

boolean mkdirs():创建多级目录
boolean mkdir():创建一级目录
boolean createNewFile():在指定位置创建文件,如果有同名文件存在,则不创建,返回false,否则常见文件并返回true。


2.删除。
boolean delete():删除失败返回false
void deleteOnExit(): 在程序退出后删除指定文件


3.判断。
boolean exists():文件是否存在
boolean canExecute()); //是否可执行
boolean canRead());//是否可读
boolean canWrite()); //是否可写
boolean exists());  //是否存在
boolean isDirectory());//是否是目录,在执行该判断时必须先判断是否存在
boolean isFile());//是否是文件
boolean isHidden());//是否为隐藏的
boolean isAbsolute());//是否是绝对路径,文件不存在也能判断。


4.获取信息。
String getName():
String getPath();
String getParent();
String getAbsolutePath();
File   getAbsoluteFile(); 返回绝对路径并封装成对象
long   lastModified()
long   length()

[java]  view plain copy
  1. import java.io.*;  
  2. class  FileDemo  
  3. {  
  4.     public static void main(String[] args) throws IOException  
  5.     {  
  6.           
  7.         listFiles();  
  8.     }  
  9.   
  10.   
  11.     //创建File对象  
  12.     public static void file()throws IOException  
  13.     {  
  14.         //可以将已有和未出现的文件或文件夹封装成对象  
  15.         File f = new File("a.txt");  
  16.         //将目录和文件名分开传递  
  17.         File f2 = new File("c:\\abc","b.txt");  
  18.   
  19.   
  20.         File d = new File("c:\\abc");  
  21.         File f3 = new File(d,"c.txt");  
  22.         //File.separetor 是可以跨平台的分隔符。  
  23.         File f4 = new File("c:"+File.separator+"abc"+File.separator+"d.txt");  
  24.     }  
  25.   
  26.   
  27.     public static void method_1()throws IOException  
  28.     {  
  29.         File f = new File("file.txt");  
  30.         sop(f.createNewFile());  //创建文件file.txt并返回true  
  31.         sop(f.createNewFile()); //返回false  
  32.         sop(f.delete());//删除file.txt文件,并返回true  
  33.         sop(f.delete());//返回false  
  34.         f.deleteOnExit();//在程序退出时删除指定文件。  
  35.         File dir = new File("abc\\kkk");  
  36.         sop(dir.mkdir());  
  37.     }  
  38.   
  39.   
  40.     public static void method_2()throws IOException  
  41.     {  
  42.         File f = new File("file.txt");  
  43.         sop(f.canExecute()); //是否可执行  
  44.         sop(f.canRead());   //是否可读  
  45.         sop(f.canWrite()); //是否可写  
  46.         sop(f.exists());  //是否存在  
  47.         sop(f.isDirectory());//是否是目录,在执行该判断时必须先判断是否存在  
  48.         sop(f.isFile());//是否是文件  
  49.         sop(f.isHidden());//是否为隐藏的  
  50.         sop(f.isAbsolute());//是否是绝对路径,文件不存在也能判断。  
  51.     }  
  52.   
  53.   
  54.     public static void method_3()throws IOException  
  55.     {  
  56.         File f = new File("file.txt");  
  57.         sop(f.getPath());//对象定义的是什么就返回什么  
  58.         sop(f.getAbsolutePath());  
  59.         sop(f.getParent());//该方法返回的是对象定义的绝对路径中的父目录。如果获取的是相对路径返回null,如该方法中就返回null  
  60.     }  
  61.   
  62.   
  63.     public static void method_4()  
  64.     {  
  65.         File f1 = new File("demo.txt");  
  66.         File f2 = new File("test.txt");  
  67.         f1.renameTo(f2);//将demo.txt改名成test.txt。加上路径可以产生剪切的效果。  
  68.     }  
  69.       
  70.     public static void listFiles()  
  71.     {  
  72.         File dir = new File("d:\\JAVA\\lianxi");  
  73.         File[] files = dir.listFiles();  
  74.         for(File f : files)  
  75.         {  
  76.             sop(f.getName());  
  77.         }  
  78.     }  
  79.   
  80.   
  81.     public static void list()  
  82.     {  
  83.         //列出了c盘下的所有文件  
  84.         File f = new File("d:\\JAVA\\lianxi");  
  85.         String[] names = f.list(new FilenameFilter()  
  86.         {  
  87.             public boolean accept(File f,String name)  
  88.             {  
  89.                 return name.endsWith(".java");//返回后缀是java的文件  
  90.             }  
  91.         });//调用list方法的file对象必须是一个目录,并且必须存在。  
  92.         for(String name : names)  
  93.         {  
  94.             sop(name);  
  95.         }  
  96.     }  
  97.   
  98.   
  99.     public static void listRoots()  
  100.     {  
  101.         File[] files = File.listRoots();  
  102.         //列出系统的所有盘符。  
  103.         for(File f : files)  
  104.         {  
  105.             sop(f);  
  106.         }  
  107.     }  
  108.   
  109.   
  110.     public static void sop(Object obj)  
  111.     {  
  112.         System.out.println(obj);  
  113.     }  
  114. }  

--------- android培训java培训、期待与您交流! ----------

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值