The use of IO stream-File (3)

The use of IO stream-File (3)

  1. The role of the File class: The File class is a wrapper class provided by java for the file or directory conversion objects in the disk. A File object can represent a file or a directory. The File object can achieve functions such as obtaining asking prices and directory attributes, and can achieve functions such as creation and deletion of file directories.

  2. Common methods for operating directories and files in the File class

    • File method for file operations

      createNewFile()//Create a new file

      delete()//Delete directly from the disk

      exists()//Query whether the file in the disk exists

      getAbsolutePath()//Get the absolute path

      getName()//Get the file name, which is equivalent to calling a toString method

      isFile()//Check if it is a file

      isHidden()//Test whether the file is a hidden file by this abstract path name

    • Methods for directory operations

      exists()//Query whether the directory exists

      isDirectory()//Determine whether the current path is a directory

      mkdir()//Create directory

      getParentFile()//Get the parent directory of the current directory

      list()//returns an array of strings containing the path names of files and directories in the directory

      listFiles//returns a File array, representing the files in the directory represented by this abstract path name

  3. The concrete realization of the operation file

    import java.io.File;
    
    public class FileDemo {
          
          
        public static void main(String[] args)throws Exception {
          
          
            //创建File对象
            File file=new File("d:/aa.txt");
            System.out.println(file.createNewFile());//创建新文件
            //System.out.println(file.delete());//直接从磁盘上删除
            System.out.println(file.exists());//查询磁盘中的文件是否存在
            System.out.println(file.getName());//获取文件名,相当于调用一个toString方法
            System.out.println(file.isFile());//查看文件中是否存在
            System.out.println(file.isHidden());//测试文件是否被这个抽象路径名是一个隐藏文件
        }
    }
    
  4. The concrete realization of the operation catalog

    import java.io.File;
    
    public class DirectoryDemo {
          
          
        public static void main(String[] args) {
          
          
            //创建File对象
            File file=new File("d:/b/c");
           // System.out.println(file.mkdir());//创建单个目录
           // System.out.println(file.mkdirs());//创建多节目录
           // System.out.println(file.exists());//查询目录是否存在
            //System.out.println(file.getParent());)//获取当前目录的父级目录
           // System.out.println(file.getParentFile().getName());//获取当前目录的父级目录对象,再获取名字
            File file2=new File("d:/");
            String[] arr=file2.list();//list()返回的字符串数组,只是获取所有的文件和目录名
            for (String temp:arr){
          
          
                System.out.println(temp);
            }
            System.out.println("---------------------------------");
            File[] arr2=file2.listFiles();//listFiles()返回的File数组,返回的是所有的文件、目录名和绝对路径
            for (File temp:arr2){
          
          
                System.out.println(temp);
            }
    
    
        }
    }
    
    

Guess you like

Origin blog.csdn.net/Xun_independent/article/details/114851057