java File类

 
File类API为我们提供了一个通用的抽象文件,用于操作计算机中的文件和目录,采用Unix的思想,目录也被看作是一种文件,所以全部用File类来处理。
 
创建一个File类的实例,其初始化值就意味着加载待处理的相应文件(或目录),比如:
 
File objDir = new File(“D:/XP/test”);
File objFile = new File(“D:/XP/test/filename.txt”);
注意:可以统一使用 / 来表示目录,Unix系统如此,Windows系统还可以使用 //
下面用这两个实例来分别创建目录和文件:
 
创建目录有两个方法:
1. objDir.mkDir(); 返回一个boolean表示创建成功与否,只能在已有目录下创建(子目录)
2. objDir.mkDirs();返回一个boolean表示创建成功与否,即使父目录不存在也能够创建。例如:当 File objDir = new File(“ hehe/dir ”); 表示在当前相对目录下再创建两级目录hehe/dir,这时使用mkDirs()方法才行,而mkDir()方法失败。注意:如果new File(“/hehe/dir”);表示从当前盘符的根目录(D:/)创建,由于这个根目录总是存在的,所以mkDir()方法也能创建成功,却往往不是我们所要的结果。
 
创建了目录就可以创建文件了,如果在不存在的目录下创建文件会抛出异常。
objFile. createNewFile ();返回一个boolean表示创建成功与否,这样就创建了一个空文件:D:/XP/test/filename.txt
下面可以查看文件的名称、大小、路径、父目录、可读可写状态、判断文件是否存在、是否是一个目录或是文件、获取文件最后修改的时间、修改文件名,删除文件或目录等。
方法名如下:
System.out.println("绝对路径:" + objFile.getAbsolutePath());
System.out.println("相对路径:" + objFile.getPath());
System.out.println("父目录:" + objFile.getParent());
System.out.println("是否可读:" + objFile.canRead());
System.out.println("是否可写:" + objFile.canWrite());
System.out.println("文件长度:" + objFile.length());
//要使用文件所在的目录来取里面的list,再通过循环来进行处理
System.out.println("所在目录文件列表:" + (int) (objDir.list().length));
System.out.println("文件名:" + objFile.getName());
System.out.println("文件存在否:" + objFile.exists());
System.out.println("文件删除成功否:" + objFile.delete());
注:要删除一个目录objDir.delete();必须要目录为空才行,如果目录不为空,可以使用objDir.list()获取当前目录下的文件列表String[],循环将其中的文件删除后继而删除目录
Java代码 复制代码  收藏代码
  1.   
  2. import java.io.*;   
  3.   
  4. public class FileOperate {   
  5.   public FileOperate() {   
  6.   }   
  7.   
  8.   /**  
  9.    * 新建目录  
  10.    * @param folderPath String 如 c:/fqf  
  11.    * @return boolean  
  12.    */  
  13.   public void newFolder(String folderPath) {   
  14.     try {   
  15.       String filePath = folderPath;   
  16.       filePath = filePath.toString();   
  17.       java.io.File myFilePath = new java.io.File(filePath);   
  18.       if (!myFilePath.exists()) {   
  19.         myFilePath.mkdir();   
  20.       }   
  21.     }   
  22.     catch (Exception e) {   
  23.       System.out.println("新建目录操作出错");   
  24.       e.printStackTrace();   
  25.     }   
  26.   }   
  27.   
  28.   /**  
  29.    * 新建文件  
  30.    * @param filePathAndName String 文件路径及名称 如c:/fqf.txt  
  31.    * @param fileContent String 文件内容  
  32.    * @return boolean  
  33.    */  
  34.   public void newFile(String filePathAndName, String fileContent) {   
  35.   
  36.     try {   
  37.       String filePath = filePathAndName;   
  38.       filePath = filePath.toString();   
  39.       File myFilePath = new File(filePath);   
  40.       if (!myFilePath.exists()) {   
  41.         myFilePath.createNewFile();   
  42.       }   
  43.       FileWriter resultFile = new FileWriter(myFilePath);   
  44.       PrintWriter myFile = new PrintWriter(resultFile);   
  45.       String strContent = fileContent;   
  46.       myFile.println(strContent);   
  47.       resultFile.close();   
  48.   
  49.     }   
  50.     catch (Exception e) {   
  51.       System.out.println("新建目录操作出错");   
  52.       e.printStackTrace();   
  53.   
  54.     }   
  55.   
  56.   }   
  57.   
  58.   /**  
  59.    * 删除文件  
  60.    * @param filePathAndName String 文件路径及名称 如c:/fqf.txt  
  61.    * @param fileContent String  
  62.    * @return boolean  
  63.    */  
  64.   public void delFile(String filePathAndName) {   
  65.     try {   
  66.       String filePath = filePathAndName;   
  67.       filePath = filePath.toString();   
  68.       java.io.File myDelFile = new java.io.File(filePath);   
  69.       myDelFile.delete();   
  70.   
  71.     }   
  72.     catch (Exception e) {   
  73.       System.out.println("删除文件操作出错");   
  74.       e.printStackTrace();   
  75.   
  76.     }   
  77.   
  78.   }   
  79.   
  80.   /**  
  81.    * 删除文件夹  
  82.    * @param filePathAndName String 文件夹路径及名称 如c:/fqf  
  83.    * @param fileContent String  
  84.    * @return boolean  
  85.    */  
  86.   public void delFolder(String folderPath) {   
  87.     try {   
  88.       delAllFile(folderPath); //删除完里面所有内容   
  89.       String filePath = folderPath;   
  90.       filePath = filePath.toString();   
  91.       java.io.File myFilePath = new java.io.File(filePath);   
  92.       myFilePath.delete(); //删除空文件夹   
  93.   
  94.     }   
  95.     catch (Exception e) {   
  96.       System.out.println("删除文件夹操作出错");   
  97.       e.printStackTrace();   
  98.   
  99.     }   
  100.   
  101.   }   
  102.   
  103.   /**  
  104.    * 删除文件夹里面的所有文件  
  105.    * @param path String 文件夹路径 如 c:/fqf  
  106.    */  
  107.   public void delAllFile(String path) {   
  108.     File file = new File(path);   
  109.     if (!file.exists()) {   
  110.       return;   
  111.     }   
  112.     if (!file.isDirectory()) {   
  113.       return;   
  114.     }   
  115.     String[] tempList = file.list();   
  116.     File temp = null;   
  117.     for (int i = 0; i < tempList.length; i++) {   
  118.       if (path.endsWith(File.separator)) {   
  119.         temp = new File(path + tempList[i]);   
  120.       }   
  121.       else {   
  122.         temp = new File(path + File.separator + tempList[i]);   
  123.       }   
  124.       if (temp.isFile()) {   
  125.         temp.delete();   
  126.       }   
  127.       if (temp.isDirectory()) {   
  128.         delAllFile(path+"/"+ tempList[i]);//先删除文件夹里面的文件   
  129.         delFolder(path+"/"+ tempList[i]);//再删除空文件夹   
  130.       }   
  131.     }   
  132.   }   
  133.   
  134.   /**  
  135.    * 复制单个文件  
  136.    * @param oldPath String 原文件路径 如:c:/fqf.txt  
  137.    * @param newPath String 复制后路径 如:f:/fqf.txt  
  138.    * @return boolean  
  139.    */  
  140.   public void copyFile(String oldPath, String newPath) {   
  141.     try {   
  142.       int bytesum = 0;   
  143.       int byteread = 0;   
  144.       File oldfile = new File(oldPath);   
  145.       if (oldfile.exists()) { //文件存在时   
  146.         InputStream inStream = new FileInputStream(oldPath); //读入原文件   
  147.         FileOutputStream fs = new FileOutputStream(newPath);   
  148.         byte[] buffer = new byte[1444];   
  149.         int length;   
  150.         while ( (byteread = inStream.read(buffer)) != -1) {   
  151.           bytesum += byteread; //字节数 文件大小   
  152.           System.out.println(bytesum);   
  153.           fs.write(buffer, 0, byteread);   
  154.         }   
  155.         inStream.close();   
  156.       }   
  157.     }   
  158.     catch (Exception e) {   
  159.       System.out.println("复制单个文件操作出错");   
  160.       e.printStackTrace();   
  161.   
  162.     }   
  163.   
  164.   }   
  165.   
  166.   /**  
  167.    * 复制整个文件夹内容  
  168.    * @param oldPath String 原文件路径 如:c:/fqf  
  169.    * @param newPath String 复制后路径 如:f:/fqf/ff  
  170.    * @return boolean  
  171.    */  
  172.   public void copyFolder(String oldPath, String newPath) {   
  173.   
  174.     try {   
  175.       (new File(newPath)).mkdirs(); //如果文件夹不存在 则建立新文件夹   
  176.       File a=new File(oldPath);   
  177.       String[] file=a.list();   
  178.       File temp=null;   
  179.       for (int i = 0; i < file.length; i++) {   
  180.         if(oldPath.endsWith(File.separator)){   
  181.           temp=new File(oldPath+file[i]);   
  182.         }   
  183.         else{   
  184.           temp=new File(oldPath+File.separator+file[i]);   
  185.         }   
  186.   
  187.         if(temp.isFile()){   
  188.           FileInputStream input = new FileInputStream(temp);   
  189.           FileOutputStream output = new FileOutputStream(newPath + "/" +   
  190.               (temp.getName()).toString());   
  191.           byte[] b = new byte[1024 * 5];   
  192.           int len;   
  193.           while ( (len = input.read(b)) != -1) {   
  194.             output.write(b, 0, len);   
  195.           }   
  196.           output.flush();   
  197.           output.close();   
  198.           input.close();   
  199.         }   
  200.         if(temp.isDirectory()){//如果是子文件夹   
  201.           copyFolder(oldPath+"/"+file[i],newPath+"/"+file[i]);   
  202.         }   
  203.       }   
  204.     }   
  205.     catch (Exception e) {   
  206.       System.out.println("复制整个文件夹内容操作出错");   
  207.       e.printStackTrace();   
  208.   
  209.     }   
  210.   
  211.   }   
  212.   
  213.   /**  
  214.    * 移动文件到指定目录  
  215.    * @param oldPath String 如:c:/fqf.txt  
  216.    * @param newPath String 如:d:/fqf.txt  
  217.    */  
  218.   public void moveFile(String oldPath, String newPath) {   
  219.     copyFile(oldPath, newPath);   
  220.     delFile(oldPath);   
  221.   
  222.   }   
  223.   
  224.   /**  
  225.    * 移动文件到指定目录  
  226.    * @param oldPath String 如:c:/fqf.txt  
  227.    * @param newPath String 如:d:/fqf.txt  
  228.    */  
  229.   public void moveFolder(String oldPath, String newPath) {   
  230.     copyFolder(oldPath, newPath);   
  231.     delFolder(oldPath);   
  232.   
  233.   }   
  234. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值