当前位置:网站首页>浅学一下I/O流和File类文件操作
浅学一下I/O流和File类文件操作
2022-04-23 03:22: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://blog.csdn.net/weixin_47383392/article/details/124332905
边栏推荐
- 场景题:A系统如何使用B系统的页面
- QT uses drag and drop picture to control and mouse to move picture
- ThreadLocal test multithreaded variable instance
- 2022t elevator repair test simulation 100 questions and online simulation test
- 为什么BI对企业这么重要?
- . net 5 Web custom middleware implementation returns the default picture
- JS recursive tree structure calculates the number of leaf nodes of each node and outputs it
- Explication détaillée des fonctions send () et recv () du programme Socket
- 超好用的【通用Excel导入功能】
- Do you really understand hashcode and equals???
猜你喜欢

一套组合拳,打造一款 IDEA 护眼方案

2022g2 boiler stoker examination question bank and online simulation examination

Utgard connection opcserver reported an error caused by: org jinterop. dcom. common. JIRuntimeException: Access is denied. [0x800

为什么BI对企业这么重要?

How to achieve centralized management, flexible and efficient CI / CD online seminar highlights sharing

xutils3修改了我提报的一个bug,开心

Super easy to use asynchronous export function of Excel

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

Knowledge of software testing~
![General test technology [II] test method](/img/b7/f661f446616ad6bfbbf48eb03ea82d.png)
General test technology [II] test method
随机推荐
Advanced sorting - fast sorting
After the mobile phone is connected to the computer, how can QT's QDIR read the mobile phone file path
POI create and export Excel based on data
EasyUI's combobox implements three-level query
Chapter 8 of C language programming (fifth edition of Tan Haoqiang) is good at using pointer exercises to analyze and answer
Supersocket is Used in net5 - command
This new feature of C 11, I would like to call it the strongest!
Mysql database design specification
《C语言程序设计》(谭浩强第五版) 第8章 善于利用指针 习题解析与答案
JS implementation of new
Web Course Design - his system
通过 zxing 生成二维码
Batch download of files ---- compressed and then downloaded
Generate QR code through zxing
Talent Plan 学习营初体验:交流+坚持 开源协作课程学习的不二路径
There is no index in the database table. When inserting data, SQL statements are used to prevent repeated addition (Reprint)
Can you answer the questions that cannot be answered with a monthly salary of 10k-20k?
IDEA查看历史记录【文件历史和项目历史】
QT learning summary
Experiment 6 input / output stream
