当前位置:网站首页>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);}}}
边栏推荐
猜你喜欢
随机推荐
运行flutter项目时遇到的问题修改flutter为国内镜像
使用Protege4和CO-ODE工具构建OWL本体的实用指南-1.3版本(7.4 Annotation Properties-注释属性)
[Environmental Construction] tensorrt
HD Satellite Map Browser
Django实现对数据库数据增删改查(二)
【面试体系知识点总结】---JVM
Ontology Development Diary 03-Understanding Code
4.字符流
Another implementation of lateral view explode
常用命令之思科常用基础配置
TestNG使用教程详解
Anti App so层对抗分析
自动化测试简历编写应该注意哪方面?有哪些技巧?
Read file by byte and character_load configuration file
These 12 GIS software are better than the other
使用Protege4和CO-ODE工具构建OWL本体的实用指南-1.3版本(4.Building An OWL Ontology)
第三方免费开放API 获取用户IP 并查询其地理位置
RPC服务远程漏洞
接口测试主要测试哪方面?需要哪些技能?要怎么学习?
软件测试外包公司怎么样?有什么好处和坏处?为什么没人去?