zoukankan      html  css  js  c++  java
  • File 类

    File类在java中表示(带路径的)文件或者目录。

    File 常用属性和方法

     1 public static void main(String[] args) {
     2         
     3         // 给定路径创建File对象
     4         // File file = new File("D:"+File.separator+"javatest"+File.separator+"a.txt");
     5         File file = new File("d:\javatest\b.mp3");
     6         System.out.println(file);
     7         
     8         // 文件基本属性
     9         System.out.println(file.canExecute());
    10         System.out.println(file.canRead());
    11         System.out.println(file.canWrite());
    12         
    13         // 文件的创建、删除
    14         if(!file.exists()) {
    15             
    16             boolean r;
    17             try {
    18                 r = file.createNewFile();
    19                 if(r) {
    20                     System.out.println("文件创建成功");
    21                 }
    22             } catch (IOException e) {
    23                 e.printStackTrace();
    24             }
    25         }
    26         
    27         // 删除文件
    28         file.delete();
    29 }
    创建文件时会抛出检查时异常IOException
     1 //路径相关
     2 public static void main(String[] args) {
     3         
     4          File file = new File("d:\javatest\a.txt");
     5 //        File file = new File("a.txt");
     6         
     7         // 获取file的绝对路径
     8         System.out.println(file.getAbsolutePath());
     9         // 获取file的创建时的路径字符串
    10         System.out.println(file.getPath());
    11         // 获取文件或者目录的名字
    12         System.out.println(file.getName());
    13         // 获取文件或者目录的父目录
    14         System.out.println(file.getParent());
    15         
    16 }

     

     1 //目录相关
     2 public static void main(String[] args) {
     3         
     4          File file = new File("d:\javatest\c\d\e");
     5          
     6          if(!file.exists()) {
     7              boolean r;
     8              
     9             try {
    10                 // 一次只能创建一个目录
    11                 // r = file.mkdir();
    12                 r = file.mkdirs();
    13                 if(r) {
    14                     System.out.println("目录创建成功");
    15                 }
    16             } catch (Exception e) {
    17                 e.printStackTrace();
    18             }
    19             
    20          }
    21 }

     

     1 //目录的遍历
     2 /*
     3 list():返回一个file表示的目录中的子目录或者文件,字符串数组类型
     4 listFiles():返回一个file表示的目录中的子目录或者文件,File数组类型
     5 */
     6 public static void main(String[] args) {
     7         
     8         // 需求:遍历d:javatest目录
     9         // list()
    10         File file =  new File("d:\javatest");
    11         
    12         
    13         /*
    14         String[] list = file.list();
    15         
    16         for (String str : list) {
    17             System.out.print(str);
    18             File f = new File(file.getPath()+"\"+str);
    19             if(f.isDirectory()) {
    20                 System.out.println(" 目录");
    21             }else {
    22                 System.out.println(" 文件");
    23             }
    24         }*/
    25         
    26         // listFiles();
    27         File[] listFiles = file.listFiles();
    28         for (File f : listFiles) {
    29             System.out.print(f.getName());
    30             if(f.isDirectory()) {
    31                 System.out.println(" 目录");
    32             }else {
    33                 System.out.println(" 文件");
    34             }
    35         }
    36 }

    自己写了一个浏览文件内容的小程序,实现效果如下:

    主要时采用了递归的方式,遍历过程中如果发现是文件夹,则调用自己。其实最难的地方是这个输出的形式的实现,源码如下:

     1 import java.io.File;
     2 import java.util.ArrayList;
     3 import java.util.List;
     4 
     5 public class Test {
     6     public static void main(String[] args) {
     7         File file = new File("C:\Test");
     8         fileInfo(file);
     9         
    10     }
    11     
    12     private static void fileInfo(File file) {
    13         System.out.println(file.getName());
    14         List<Integer> cursor = new ArrayList<Integer>();
    15         fileInfo(file, cursor);
    16     }
    17     
    18     private static void fileInfo(File file, List<Integer> cursor) {
    19         File[] files = file.listFiles();
    20         List<Integer> tmp = new ArrayList<Integer>(cursor);
    21         for (int i = 0; i < files.length; i++) {
    22             for(int num : cursor) {
    23                 if(num == 0)
    24                     System.out.print("  ");
    25                 else {
    26                     System.out.print("┃ ");
    27                 }
    28             }
    29             if(files[i].isDirectory()) {
    30                 if(i == files.length - 1) {
    31                     System.out.println("┖ " + files[i].getName());
    32                     tmp.add(0);
    33                 }
    34                 else {
    35                     System.out.println("┠ "+files[i].getName());
    36                     tmp.add(1);
    37                 }
    38                 fileInfo(files[i], tmp);
    39                 tmp.remove(tmp.size() - 1);
    40             }else {
    41                 if(i == files.length - 1)
    42                     System.out.println("┖ " + files[i].getName());
    43                 else
    44                     System.out.println("┠ "+files[i].getName());
    45             }
    46         }
    47         
    48     }
    49 }
  • 相关阅读:
    "LC.exe" exited with code -1 错误
    GridControl的用法(1)
    oracle建库及plsql建表空间的用法
    sql server还原数据库文件(.bak)常见问题解决办法笔记
    ubuntu下安装deb包
    ubuntu下安装五笔输入法
    ubuntu下安装codeTyphon
    centos7下源码安装lazarus
    后台对Json数据加密、解密
    Http请求纯后台简单实现分页并返回JSON格式
  • 原文地址:https://www.cnblogs.com/carlosouyang/p/10821807.html
Copyright © 2011-2022 走看看