当前位置:网站首页>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);}}}边栏推荐
- 【Harmony OS】【ArkUI】ets开发 简易视频播放器
- 2048小游戏成品源码
- AES/ECB/PKCS5Padding encryption and decryption
- MySQL Leak Check (4) Stored Procedures and Cursors
- 本体开发日记02-sparql简单查询
- Consolidation of Questionnaire Questions and Answers
- 2.字节流
- Ontology Development Diary 01-Jena Configuration Environment Variables
- 进入大厂的面试经验(P7)
- 自动化测试简历编写应该注意哪方面?有哪些技巧?
猜你喜欢
随机推荐
unittest测试框架原理及测试流程解析,看完绝对有提升
本体开发日记05-努力理解SWRL(Built-Ins)
本体开发日记05-努力理解SWRL(下)
类 对象 属性 方法 类的成员
JS-常用方法整理
Ontology Development Diary 03-Understanding Code
Django实现对数据库数据增删改查(二)
"The camera can't be used" + win8.1 + DELL + external camera + USB drive-free solution
Understanding of PID control motor output as motor PWM duty cycle input
Ovie map computer terminal and mobile terminal can not be used, is there any alternative map tool
3.List接口与实现类
软件测试分析流程及输出项包括哪些内容?
7.Collections工具类
学习栈的心得和总结(数组实现)
The div simulates the textarea text box, the height of the input text is adaptive, and the word count and limit are implemented
自动化测试框架有哪几种?搭建的思路是什么?一篇文章让你彻底了解自动化
第三方免费开放API 获取用户IP 并查询其地理位置
Sweet alert
条件和递归
static_assert报错为什么?






