当前位置:网站首页>Learn about I / O flow and file operations
Learn about I / O flow and file operations
2022-04-23 03:23:00 【Tangable22】
Catalog
I/O Flow principle and classification of flow
I/O principle
- I/O yes Input and Output Abbreviation ,I/O Technology is a very practical technology , Used to handle data transmission ( Such as : read / Writing documents , Network communication )
- Java In the program , For data input / The output operation is based on
flow (stream)
In the way of- java.io A variety of
flow (stream)
Classes and interfaces , To get different kinds of data , And input and output data through methods .- File stream : Files are operated in the form of stream in the program
Input stream : Data from data sources ( file ) To the program ( Memory ) The path of
Output stream : Data from the program ( Memory ) To the data source ( file ) The path of
I/O Classification of flows
- It is divided into... According to the unit of operation data : Byte stream ( Binary )、 Character stream ( text file )
- It is divided into... According to the flow direction of data : Input stream 、 Output stream
- According to the role of flow, it is divided into : Node flow 、 Processing flow
Abstract base class | Byte stream | Character stream |
---|---|---|
Input stream | InputStream | Reader |
Output stream | OutputStream | Writer |
️I/O Architecture of
️ file (File)
Concept
- What is a document ?
file , It's not new to us , A file is where data is stored , such as word file 、txt Text 、excel file 、 picture 、 video … It's all documents , In the operating system, the data in the disk is managed in the unit of file . From a data storage perspective , All documents are essentially the same , They are all composed of bytes. In the final analysis, they are 0-1 Bit string .
- Folder ( Catalog )
If multiple files are not classified and put together , It is very inconvenient for users to use , therefore , A tree directory is also introduced ( It's also called a folder ) The mechanism of , You can put files in different folders , Folders can also be nested in folders , This makes it easy for users to manage and use files .
️ Common operations (File class )
- Create file object related constructors and methods
new File(String pathname);// Build a... Based on the path File object
new File(File parent,String child);// According to the parent directory file + Sub path construction
new File(String parent,String child);// According to the parent directory path + Sub path construction
createNewFile();// Create a new file
stay E Under the plate , Create the file in the above way test01.txt\test02.txt\test03.txt
import java.io.File;
import java.io.IOException;
public class FileCreate {
public static void main(String[] args) throws IOException {
// The way 1
String pathname = "e:\\test01.txt";
File file1 = new File(pathname);
file1.createNewFile();
// The way 2
File parentfile = new File("e:\\");
String child2 = "test02.txt";
File file2 = new File(parentfile, child2);
file2.createNewFile();
// The way 3
String parent = "e:\\";
String child3 = "test03.txt";
File file3 = new File(parent, child3);
file3.createNewFile();
}
}
- Get information about the file
get.getName();// Get the file name
canRead();// Is the file readable
canWrite();// Is the document writable
getAbsoultePath();// Get the absolute path of the file
getPath();// Relative paths
getParent();// Get file parent directory
lenth();// file size ( byte )
exists();// Judge whether the file exists
isFile();// Judge whether it's a file
isDirectory();// Determine whether it is a directory
import java.io.File;
public class FileInfomation {
public static void main(String[] args) {
// Create a file object
File file = new File("e:\\test01.txt");
System.out.println(" File name :" + file.getName());
System.out.println(" Is the file readable :" + file.canRead());
System.out.println(" Is the document writable :" + file.canWrite());
System.out.println(" File absolute path :" + file.getAbsolutePath());
System.out.println(" file size ( byte ):" + file.length());
System.out.println(" Does the file exist :" + file.exists());
System.out.println(" Is it a file :" + file.isFile());
System.out.println(" Is it a directory :" + file.isDirectory());
}
}
- The file is
File f1=new File("D:\\test1.txt");
File f2=new File("D:\\test2.txt");
f1==f2;// Compare the addresses of the two objects
f1.equals(f2);// Compare the path of the file corresponding to the two objects
- Directory operation and file deletion
mkdir();// Create a single level directory
mkdirs();// Create a multi-level catalog
delete();// Delete directory ( This layer directory must be empty , No content )
- Check the file directory
list();// Returns an array of strings , Name the files and directories in the directory represented by this abstract pathname .
listFiles();// Returns an array of abstract pathnames , Represents the files in the directory represented by the abstract pathname .
Case list : Traverse all files in a directory and print out
public class PrintFile {
public static void main(String[] args) {
// Create a file object
File file = new File("e:\\Test");
String[] list = file.list();// Directory under folder / An array of names corresponding to the file
for (String s : list) {
System.out.println(s);
}
File[] files = file.listFiles();
for (File f : files) {
System.out.println(f.getName() + "," + f.getAbsolutePath());
}
}
}
Traverse the directory
1. Given a file object file
2.listFiles() Get the array of all file objects under the file
3. Traverse File An array of objects , If it's a catalog , Recursively call this method to get all file objects in this directory ; If it's a file , Printout path + full name
import java.io.File;
public class PrintFile {
public static void main(String[] args) {
File file = new File("E:\\Code");
getAllFile(file);
}
public static void getAllFile(File file) {
// Get all the files in the given directory File An array of objects
File[] files = file.listFiles();
// To traverse the
if (files != null) {
for (File f : files) {
if (f.isDirectory()) {
// Determine whether it is a directory , If it is , Call recursion
getAllFile(f);
} else {
// No , Print path + file name
System.out.println(f.getAbsoluteFile() + " Under the :" + f.getName());
}
}
}
}
}
版权声明
本文为[Tangable22]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230322316599.html
边栏推荐
- Idempotency practice operation, explaining idempotency based on business
- 批量下載文件----壓縮後再下載
- 2022 团体程序设计天梯赛 模拟赛 1-8 均是素数 (20 分)
- Unity basics 2
- js 中,为一个里面带有input 的label 绑定事件后在父元素绑定单机事件,事件执行两次,求解
- 2022t elevator repair test simulation 100 questions and online simulation test
- 你真的懂hashCode和equals吗???
- Advanced sorting - fast sorting
- Using jsonserialize to realize data type conversion gracefully
- Flink customizes the application of sink side sinkfunction
猜你喜欢
When migrating tslib_ setup: No such file or directory、ts_ open: No such file or director
Peut recevoir plusieurs paramètres de type de données - paramètres variables
2022 团体程序设计天梯赛 模拟赛 L1-7 矩阵列平移 (20 分)
Data mining series (3)_ Data mining plug-in for Excel_ Estimation analysis
Visual programming - Experiment 1
[Mysql] LEFT函數 | RIGHT函數
Charles uses three ways to modify requests and responses
Configuration table and page information automatically generate curd operation page
2022t elevator repair test simulation 100 questions and online simulation test
Log4net is in Net core usage
随机推荐
JS inheritance
全新的ORM框架——BeetlSQL介绍
socket编程 send()与 recv()函数详解
2022 团体程序设计天梯赛 模拟赛 1-8 均是素数 (20 分)
Peut recevoir plusieurs paramètres de type de données - paramètres variables
QT learning summary
Comprehensive calculation of employee information
Idempotency practice operation, explaining idempotency based on business
Experiment 6 input / output stream
C abstract class
LoadRunner - performance testing tool
数据库表中不建索引,在插入数据时,通过sql语句防止重复添加(转载)
Advanced sorting - fast sorting
软件测试相关知识~
Queue storage and circular queue
L3-011 直捣黄龙 (30 分)
2022 Shandong Province safety officer C certificate work certificate question bank and online simulation examination
Visual programming -- how to customize the mouse cursor
Supersocket is Used in net5 - command
Flink real-time data warehouse project - Design and implementation of DWS layer