当前位置:网站首页>6. The File types

6. The File types

2022-08-09 09:32:00 come here my bear

File class

  • Concept: Represents a file or folder in a physical drive letter
  • Use of the File class
    • Delimiter
    • File Operations
    • Folder operations
  • Method:
    • createNewFile() creates a new file
    • mkdir() creates a new directory
    • delete() deletes a file or empty directory
    • exists() determines whether the object represented by the File object exists
    • getAbsolutePath() gets the absolute path of the file
    • getName() get the name
    • getParent() Get the directory where the file/directory is located
    • isDirectory() is a directory
    • isFile() is a file
    • length() Get the length of the file
    • listFiles() lists all contents in a directory
    • renameTo() renames the file

Use of the File class

package com.io.file;import java.io.File;import java.io.IOException;import java.util.Date;/*** Use of File class* (1) Separator* (2) File operation* (3) Folder operation*/public class Demo1 {public static void main(String[] args) throws IOException, InterruptedException {// separator();// fileOpe();directoryOpe();}// delimiterpublic static void separator(){System.out.println("Path separator" + File.pathSeparator);System.out.println("Name separator" + File.separator);}// file operationspublic static void fileOpe() throws IOException, InterruptedException {// Create a fileFile file = new File("E:\\Desktop\\bbb.txt");if (!file.exists()){boolean b = file.createNewFile();System.out.println("Create result" + b);}// System.out.println(file.toString());// Delete Files// delete directly// System.out.println("Delete result" + file.delete());// delete on exit using jvm// file.deleteOnExit();// Thread.sleep(3000); // Sleep time 1000 = 1s// get file information// Get the absolute path of the fileSystem.out.println("Get the absolute path of the file: " + file.getAbsolutePath());System.out.println("Get path: " + file.getPath());System.out.println("Get name: " + file.getName());System.out.println("Get parent directory: " + file.getParent());System.out.println("Get file length: " + file.length());System.out.println("Get creation time: " + new Date(file.lastModified()).toLocaleString());// judgeSystem.out.println("Writable: " + file.canWrite());System.out.println("Is it a file: " + file.isFile());System.out.println("Is it a hidden file: " + file.isHidden());}// folder operationpublic static void directoryOpe() throws InterruptedException {// create folderFile dir = new File("E:\\desktop\\aa\\bb");System.out.println(dir.toString());if (!dir.exists()){// dir.mkdir(); // can only create single-level directoriesSystem.out.println("Result of creating multi-level directory: " + dir.mkdirs()); // Can create multi-level directory}// delete the folder// Directly delete (only the innermost empty directory can be deleted)// System.out.println("Delete result is: " + dir.delete());// delete using jvmdir.deleteOnExit();Thread.sleep(3000); // sleep for 3s// get folder informationSystem.out.println("Get absolute path: " + dir.getAbsolutePath());System.out.println("Get path: " + dir.getPath());System.out.println("Get folder name: " + dir.getName());System.out.println("Get parent directory: " + dir.getParent());System.out.println("Get creation time: " + new Date(dir.lastModified()).toLocaleString());// judgeSystem.out.println("Is it a folder: " + dir.isDirectory());System.out.println("Is it a hidden folder: " + dir.isHidden());// Traverse folders list() listFiles()File dir1 = new File("E:\\desktop\\img");// dir1.listFiles();for (String s : dir1.list()) {System.out.println(s);}}}
原网站

版权声明
本文为[come here my bear]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/221/202208090923102336.html