当前位置:网站首页>淺學一下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
边栏推荐
- 【VS Code】解决jupyter文件在vs code中显示异常的问题
- Test questions and some space wars
- 通过 zxing 生成二维码
- General testing technology [1] classification of testing
- 2022 团体程序设计天梯赛 模拟赛 L1-7 矩阵列平移 (20 分)
- Generate QR code through zxing
- Codeforces Round #784 (Div. 4)题解 (第一次AK cf (XD
- C-11 problem h: treasure chest 2
- Visual programming -- how to customize the mouse cursor
- 打卡:4.23 C语言篇 -(1)初识C语言 - (12)结构体
猜你喜欢

研讨会回放视频:如何提升Jenkins能力,使其成为真正的DevOps平台

MySQL keyword group_ Concat, combined connection query

2022g2 boiler stoker examination question bank and online simulation examination

全新的ORM框架——BeetlSQL介绍

File upload vulnerability summary and upload labs shooting range documentary

Problem a: face recognition

Unity games and related interview questions

Top ten project management software similar to JIRA

Translation of l1-7 matrix columns in 2022 group programming ladder Simulation Competition (20 points)

MySQL之explain关键字详解
随机推荐
Test questions and some space wars
QT uses drag and drop picture to control and mouse to move picture
为什么BI对企业这么重要?
JS, bind the event for a label with input, and then bind the stand-alone event in the parent element. The event is executed twice and solved
第四次作业
There is no index in the database table. When inserting data, SQL statements are used to prevent repeated addition (Reprint)
MySql分组查询规则
Query stored procedures in PostgreSQL
js 中,为一个里面带有input 的label 绑定事件后在父元素绑定单机事件,事件执行两次,求解
Peut recevoir plusieurs paramètres de type de données - paramètres variables
MySQL query specifies that a row is sorted to the first row
poi根据数据创建导出excel
. net webapi access authorization mechanism and process design (header token + redis)
. net 5 Web custom middleware implementation returns the default picture
2022 团体程序设计天梯赛 模拟赛 L2-4 哲哲打游戏 (25 分)
Build websocket server in. Net5 webapi
JS recursive tree structure calculates the number of leaf nodes of each node and outputs it
C interface
2022 group programming ladder simulation match 1-8 are prime numbers (20 points)
一文了解全面静态代码分析
