当前位置:网站首页>淺學一下I/O流和File類文件操作
淺學一下I/O流和File類文件操作
2022-04-23 03:25:00 【Tangable22】
I/O流原理及流的分類
I/O原理
- I/O是Input和Output的縮寫,I/O技術是非常實用的技術,用於處理數據傳輸(如:讀/寫文件,網絡通信)
- Java程序中,對於數據的輸入/輸出操作是以
流(stream)的方式進行的- java.io包下提供了各種
流(stream)類和接口,用以獲取不同種類的數據,並通過方法輸入和輸出數據。- 文件流:文件在程序中是以流的形式操作的
輸入流:數據從數據源(文件)到程序(內存)的路徑
輸出流:數據從程序(內存)到數據源(文件)的路徑
I/O流的分類
- 按操作數據單比特分為:字節流(二進制文件)、字符流(文本文件)
- 按數據的流向分為:輸入流、輸出流
- 按流的角色分為:節點流、處理流
| 抽象基類 | 字節流 | 字符流 |
|---|---|---|
| 輸入流 | InputStream | Reader |
| 輸出流 | OutputStream | Writer |
️I/O的體系結構

️文件(File)
概念
- 什麼是文件?
文件,對於我們並陌生,文件就是保存數據的地方,比如word文檔、txt文本、excel文件、圖片、視頻…等都是文件,操作系統中以文件為單比特管理磁盤中的數據。從數據存儲角度來說,所有文件本質上都是一樣的,都是由一個個字節組成的歸根到底都是0-1比特串。
- 文件夾(目錄)
多個文件如果不分類放在一起,用戶使用起來就非常不方便,因此,又引入了樹形目錄(也叫文件夾)的機制,可以把文件放在不同的文件夾中,文件夾中還可以嵌套文件夾,這就便於用戶對文件進行管理和使用。
️常用操作(File類)
- 創建文件對象相關構造器和方法
new File(String pathname);//根據路徑構建一個File對象
new File(File parent,String child);//根據父目錄文件+子路徑構建
new File(String parent,String child);//根據父目錄路徑+子路徑構建
createNewFile();//創建新文件
在E盤下,用以上方式創建文件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 {
//方式1
String pathname = "e:\\test01.txt";
File file1 = new File(pathname);
file1.createNewFile();
//方式2
File parentfile = new File("e:\\");
String child2 = "test02.txt";
File file2 = new File(parentfile, child2);
file2.createNewFile();
//方式3
String parent = "e:\\";
String child3 = "test03.txt";
File file3 = new File(parent, child3);
file3.createNewFile();
}
}

- 獲取文件的相關信息
get.getName();//獲取文件名字
canRead();//文件是否可讀
canWrite();//文件是否可寫
getAbsoultePath();//獲取文件的絕對路徑
getPath();//相對路徑
getParent();//獲取文件父級目錄
lenth();//文件大小(字節)
exists();//判斷文件是否存在
isFile();//判斷是不是一個文件
isDirectory();//判斷是不是一個目錄
import java.io.File;
public class FileInfomation {
public static void main(String[] args) {
//創建文件對象
File file = new File("e:\\test01.txt");
System.out.println("文件名字:" + file.getName());
System.out.println("文件是否可讀:" + file.canRead());
System.out.println("文件是否可寫:" + file.canWrite());
System.out.println("文件絕對路徑:" + file.getAbsolutePath());
System.out.println("文件大小(字節):" + file.length());
System.out.println("文件是否存在:" + file.exists());
System.out.println("是不是一個文件:" + file.isFile());
System.out.println("是不是一個目錄:" + file.isDirectory());
}
}

- 文件比較
File f1=new File("D:\\test1.txt");
File f2=new File("D:\\test2.txt");
f1==f2;//比較的是兩個對象的地址
f1.equals(f2);//比較兩個對象對應的文件的路徑
- 目錄操作和文件删除
mkdir();//創建單層目錄
mkdirs();//創建多層目錄
delete();//删除目錄(這層目錄必須為空,沒有內容)
- 查看文件目錄
list();//返回一個字符串數組,命名由此抽象路徑名錶示的目錄中的文件和目錄。
listFiles();//返回一個抽象路徑名數組,錶示由該抽象路徑名錶示的目錄中的文件。
案列:遍曆一個目錄下的所有文件打印輸出
public class PrintFile {
public static void main(String[] args) {
//創建文件對象
File file = new File("e:\\Test");
String[] list = file.list();//文件夾下目錄/文件對應的名字的數組
for (String s : list) {
System.out.println(s);
}
File[] files = file.listFiles();
for (File f : files) {
System.out.println(f.getName() + "," + f.getAbsolutePath());
}
}
}

遍曆目錄
1.給定一個文件對象file
2.listFiles()獲取該文件下的所有文件對象數組
3.遍曆File對象數組,如果是目錄,遞歸調用該方法獲取該目錄下的所有文件對象;如果是文件,打印輸出路徑+姓名
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) {
//獲取給定目錄下的所有File對象數組
File[] files = file.listFiles();
//開始遍曆
if (files != null) {
for (File f : files) {
if (f.isDirectory()) {
//判斷是否為目錄,如果是,調用遞歸
getAllFile(f);
} else {
//不是,就打印路徑+文件名
System.out.println(f.getAbsoluteFile() + "下的:" + f.getName());
}
}
}
}
}
版权声明
本文为[Tangable22]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230322316599.html
边栏推荐
- 2022 团体程序设计天梯赛 模拟赛 L1-7 矩阵列平移 (20 分)
- It can receive multiple data type parameters - variable parameters
- 研讨会回放视频:如何提升Jenkins能力,使其成为真正的DevOps平台
- MySQL之explain关键字详解
- Supersocket is Use in net5 - startup
- Unity knowledge points (common core classes)
- IDEA查看历史记录【文件历史和项目历史】
- New ORM framework -- Introduction to beetlsql
- A comprehensive understanding of static code analysis
- Problem C: realize Joseph Ring with linked list
猜你喜欢
![Eight elder brothers chronicle [4]](/img/87/f695d0275f8a66b9def48a75668d15.png)
Eight elder brothers chronicle [4]

Code forces round # 784 (DIV. 4) solution (First AK CF (XD)

Test questions and some space wars

Top ten project management software similar to JIRA

"Visual programming" test paper

Unity knowledge points (ugui)

When migrating tslib_ setup: No such file or directory、ts_ open: No such file or director

2022 group programming ladder simulation match 1-8 are prime numbers (20 points)

MySQL之explain关键字详解

Supersocket is Used in net5 - command
随机推荐
2022 团体程序设计天梯赛 模拟赛 L2-4 哲哲打游戏 (25 分)
Query stored procedures in PostgreSQL
socket编程 send()与 recv()函数详解
Idea view history [file history and project history]
Experiment 5 components and event handling
浅学一下I/O流和File类文件操作
Top 9 task management system in 2022
Web Course Design - his system
Detailed description of MySQL index [B + tree index, hash index, full-text index, overlay index]
Problem a: face recognition
Queue storage and circular queue
Translation of l1-7 matrix columns in 2022 group programming ladder Simulation Competition (20 points)
Is it difficult to choose binary version control tools? After reading this article, you will find the answer
Supersocket is Use in net5 - concept
Explication détaillée des fonctions send () et recv () du programme Socket
Configure automatic implementation of curd projects
Cefsharp stores cookies and reads cookies
Experiment 6 input / output stream
Utgard connection opcserver reported an error caused by: org jinterop. dcom. common. JIRuntimeException: Access is denied. [0x800
Charles uses three ways to modify requests and responses
