当前位置:网站首页>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
边栏推荐
- 可以接收多種數據類型參數——可變參數
- JS recursive tree structure calculates the number of leaf nodes of each node and outputs it
- Docker拉取mysql并连接
- Super easy to use asynchronous export function of Excel
- Unity basics 2
- MySQL索引详解【B+Tree索引、哈希索引、全文索引、覆盖索引】
- Flink实时数仓项目—DWS层设计与实现
- C-11 problem I: find balloon
- EasyUI's combobox implements three-level query
- IOTOS物联中台对接海康安防平台(iSecure Center)门禁系统
猜你喜欢

Chapter 7 of C language programming (fifth edition of Tan Haoqiang) analysis and answer of modular programming exercises with functions

一文了解全面静态代码分析

《C语言程序设计》(谭浩强第五版) 第8章 善于利用指针 习题解析与答案
![[Mysql] LEFT函数 | RIGHT函数](/img/26/82e0f2280de011636c26931a74e749.png)
[Mysql] LEFT函数 | RIGHT函数

移植tslib时ts_setup: No such file or directory、ts_open: No such file or director

MySQL query specifies that a row is sorted to the first row

Super easy to use asynchronous export function of Excel

《C语言程序设计》(谭浩强第五版) 第9章 用户自己建立数据类型 习题解析与答案

2022 P cylinder filling training test questions and simulation test

After the mobile phone is connected to the computer, how can QT's QDIR read the mobile phone file path
随机推荐
《C语言程序设计》(谭浩强第五版) 第8章 善于利用指针 习题解析与答案
L3-011 直捣黄龙 (30 分)
It can receive multiple data type parameters - variable parameters
QT dynamic translation of Chinese and English languages
IOTOS物联中台对接海康安防平台(iSecure Center)门禁系统
移植tslib时ts_setup: No such file or directory、ts_open: No such file or director
MySQL索引详解【B+Tree索引、哈希索引、全文索引、覆盖索引】
为什么BI对企业这么重要?
JS inheritance
Flink customizes the application of sink side sinkfunction
Flink real-time data warehouse project - Design and implementation of DWS layer
研讨会回放视频:如何提升Jenkins能力,使其成为真正的DevOps平台
Problem a: face recognition
JS recursive tree structure calculates the number of leaf nodes of each node and outputs it
Huawei mobile ADB devices connection device is empty
Eight elder brothers chronicle [4]
Chapter 8 of C language programming (fifth edition of Tan Haoqiang) is good at using pointer exercises to analyze and answer
12. < tag linked list and common test site synthesis > - lt.234 palindrome linked list
2022 团体程序设计天梯赛 模拟赛 L2-4 哲哲打游戏 (25 分)
可以接收多种数据类型参数——可变参数
