当前位置:网站首页>Basic operation of file
Basic operation of file
2022-07-24 13:13:00 【No fish in Haikou】
java.io.File
- File Each instance of is used to represent a file or directory on the hard disk . Use File Sure :
- 1: Access the attribute information of the file directory ( name , size , Change time, etc )
- 2: Create or delete file directories
- 3: Access all children in a directory
File When creating, you need to specify the path information of the file or directory it represents , There are two ways to write a path
Absolute path :
for example :D:/jas2009/JSD2009/demo.txt
Relative paths :
for example :./demo.txt
The advantage of absolute path is clarity , But the position is fixed , Cross platform disadvantage .
The advantage of relative path is good adaptability , Facilitate cross platform . But the disadvantage is not clear , need It depends on the running environment of the current program .
stay idea(eclipse Also the same ) The location of the current directory in is the directory where the current program is located , This is the equivalent of JSD2009_SE This directory
File file = new File("./demo.txt");
String name = file.getName(); // Get the name of the file
System.out.println(name);
long length = file.length(); // file size
System.out.println(length+" byte ");
boolean cr = file.canRead(); // Is the file readable
boolean cw = file.canWrite(); // Is the document writable
System.out.println(" Can be read :"+cr);
System.out.println(" Can write :"+cw);
boolean ih = file.isHidden(); // Whether the file can be hidden
System.out.println(" Can I hide :"+ih);
createNewFile(): create a file
// Create a new file in the current directory :test.txt
File file = new File("./test.txt");
//boolean exists() The method is to judge File Indicates whether the file or directory already exists
if(file.exists()){
System.out.println(" The file already exists ");
}else{
file.createNewFile();
System.out.println(" File created ");
}
delete(): Delete file
File file = new File("test.txt");
if(file.exists()){
file.delete();
System.out.println(" The file has been deleted ");
}else {
System.out.println(" The file does not exist ");
}
To create a directory, you usually use mkdirs
File dir = new File("a/b/c/d/e/f");
if(dir.exists()){
System.out.println(" The directory already exists !");
}else{
// dir.mkdir(); // The directory where the created directory is located must exist , Otherwise, the creation will not succeed
dir.mkdirs(); // Will create the nonexistent negative directory together
System.out.println(" The directory has been created !");
}
delete(): Delete directory :
File file = new File("./demo");
if(file.exists()){
file.delete(); // When deleting a directory, it is required that the directory must be empty
System.out.println(" The directory has been deleted ");
}else {
System.out.println(" The directory does not exist ");
}
// Get all the children of the current directory
File dir = new File(".");
/* boolean isFile() Judge the present File Indicates whether it is a file boolean isDirectory() Determine whether it is a directory */
if(dir.isDirectory()){
/* Files listFile() Get current File Object represents all children in a directory , Each subitem is represented by a File Object representation is finally returned as an array */
File[] subs = dir.listFiles();
System.out.println(" Sub item total :"+subs.length+" individual ");
for (int i=0;i<subs.length;i++){
System.out.println(subs[i].getName());
}
}else {
System.out.println(" Not a directory ");
}
Get the current directory containing a Children of
File dir = new File(".");
if(dir.isDirectory()){
File[] subs = dir.listFiles(new FileFilter() {
public boolean accept(File file) {
String name = file.getName();
int index = name.indexOf("a");
return index!=-1;
}
});
System.out.println(" altogether :"+subs.length+" individual ");
}
边栏推荐
- 2022.07.21
- About packaging objects
- Knowledge sharing | sharing some methods to improve the level of enterprise document management
- SSM hospital inpatient management system
- 3. Realize snake and basic game interface
- LeadTools 22 kit LeadTools super set
- Search engine based on boost library
- The core capability of accelerating enterprise data application innovation flexibility
- Symbol
- Teach you how to use power Bi to realize four kinds of visual charts
猜你喜欢

34. Add two numbers
![[paper reading] temporary binding for semi-superior learning](/img/59/846d868cccad238267bd984f085218.png)
[paper reading] temporary binding for semi-superior learning

20201127 use markdown to draw UML diagrams, graphviz installation experience hematemesis finishing

About the concept of thread (1)

30. Rearrange the linked list

English grammar_ Indefinite pronouns - Overview

28. Rainwater connection

Knowledge sharing | sharing some methods to improve the level of enterprise document management

基于matlab的语音处理

Redis(13)----浅谈Redis的主从复制
随机推荐
Go deadlock problem
Nacos deployment
基于matlab的语音处理
Knowledge sharing | sharing some methods to improve the level of enterprise document management
The price of domestic flagship mobile phones is nearly 6000, but they can't even beat iphone12. It's clear who users choose
Introduction of embedded network interface scheme and summary of driver debugging methods
Proxy
29. Right view of binary tree
2022.07.15 暑假集训 个人排位赛(十)
Use of PageHelper
Learn the calculation method of quantile value in n minutes
登临科技联合创始人王平:创新+自研“双核”驱动,GPU+赋能AI落地生根|量子位·视点分享回顾...
Modern data architecture selection: Data fabric, data mesh
Copy of array and array address value
Custom scroll bar
An example of how to save various data types by using esp32 EEPROM library functions under Arduino framework
setAttribute、getAttribute、removeAttribute
Vscode configuration user code snippet (including deletion method)
Getting started with SQL join use examples to learn left connection, inner connection and self connection
Nearly 65billion pieces of personal information were illegally handled in seven years, and the investigation of didi network security review case was announced