当前位置:网站首页>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);}}}
边栏推荐
- 软件测试面试常见问题及答案(发散思维、接口、性能、概念、)
- Do you know the basic process and use case design method of interface testing?
- 6.File类
- Understanding of PID control motor output as motor PWM duty cycle input
- 【机器学习】网络爬虫实战详解
- 进入大厂的面试经验(P7)
- 有返回值的函数
- 条件和递归
- 初窥门径代码起手,Go lang1.18入门精炼教程,由白丁入鸿儒,首次运行golang程序EP01
- China to create a domestic "Google Earth" clarity scary
猜你喜欢
随机推荐
TypeScript Brief (1)
恶意软件查杀工具分享
Ovie map computer terminal and mobile terminal can not be used, is there any alternative map tool
常用命令之思科常用基础配置
4.字符流
Ontology development diary 04 - to try to understand some aspects of protege
lateral view explode的另一种实现方式
本体开发日记05-努力理解SWRL(上)
Django实现对数据库数据增删改查(一)
web测试之功能测试常用的方法有哪几种?有什么要点要注意?
约瑟夫问题的学习心得
div模拟textarea文本框,输入文字高度自适应,且实现字数统计和限制
单元测试是什么?怎么写?主要测试什么?
2.字节流
3.List接口与实现类
Consolidation of Questionnaire Questions and Answers
Max Flow P
手机APP测试流程规范和方法你知道多少?
常用功能测试的检查点与用例设计思路
Environment build onnxruntime 】