当前位置:网站首页>10.File/IO流-bite
10.File/IO流-bite
2022-06-22 08:21:00 【风生u】
File
文件对象
目录又称为文件夹,文件夹也是一个文件,具体的目录和文件都可以用File对象来表示 .\表示该层所在的文件夹

字节流对象和字符流对象
字节流对象:
读:InputStream(抽象类)->FileInputStream(实现子类)
写:OutputStream(抽象类)->FileOutputStream(实现子类)
FileInputStream:
read提供了三个版本的重载.
1)无参数版本:一次读一个字节.返回值是读到的这个字节:返回值是对应字节的ASCII码值,读完了返回-1
2)一个参数版本:一次读若干个字节,把读的结果放到参数中指定的数组中,返回值就是读到的字节数.
返回填充的字节数组长度,读完返回-1
3)三个参数版本:一次读若干个字节,把读的结果放到参数中指定的数组中,返回值就是读到的字节数.不是从数组的起始位置放置,而是从中间位置放置(off这个下标的位置)len表示最多能放多少个元素(字节)
// 构造方法中需要指定打开文件的路径.
// 此处的路径可以是绝对路径, 也可以是相对路径, 还可以是 File 对象
InputStream inputStream = null;
try {
// 1. 创建对象, 同时也是在打开文件.
inputStream = new FileInputStream("d:/test.txt");
// 2. 尝试一个一个字节的读, 把整个文件都读完.
while (true) {
int b = inputStream.read();
if (b == -1) {
// 读到了文件末尾
break;
}
System.out.println(b);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
// 3. 读完之后要记得关闭文件, 释放资源~
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
另一种写法:由try自动调用close
try (InputStream inputStream = new FileInputStream("d:/test.txt")) {
while (true) {
int b = inputStream.read();
if (b == -1) {
break;
}
System.out.println(b);
}
} catch (IOException e) {
e.printStackTrace();
}
一次读若干个字节放在数组中:
try (InputStream inputStream = new FileInputStream("d:/test.txt")) {
// 一次读取若干个字节.
while (true) {
byte[] buffer = new byte[1024];
int len = inputStream.read(buffer);
if (len == -1) {
// 如果返回 -1 说明读取完毕了
break;
}
for (int i = 0; i < len; i++) {
System.out.println(buffer[i]);
}
}
} catch (IOException e) {
e.printStackTrace();
}
FileOutputStream:每次写入文件是会清空原有文件内容,可以在构造方法后面再加上一个参数:true,使之不清空,接着原文件后面写入
// 使用字节流, 写文件的案例.
public class Demo9 {
public static void main(String[] args) {
try (OutputStream outputStream = new FileOutputStream("d:/test.txt")) {
// outputStream.write(97);
// outputStream.write(98);
// outputStream.write(99);
byte[] buffer = new byte[]{
97, 98, 99};
outputStream.write(buffer);
} catch (IOException e) {
e.printStackTrace();
}
}
}
字符流对象:
读:Reader->FileReader
// 按照字符来读写
public class Demo10 {
public static void main(String[] args) {
try (Reader reader = new FileReader("d:/test.txt")) {
// 按照字符来读.
while (true) {
char[] buffer = new char[1024];
int len = reader.read(buffer);
if (len == -1) {
break;
}
// for (int i = 0; i < len; i++) {
// System.out.println(buffer[i]);
// }
// 如果这里传入的 数组 是 byte 数组, 还可以手动的指定一下 utf8 字符集避免乱码.
String s = new String(buffer, 0, len);
System.out.println(s);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
写:Writer->FileWriter
// 按照字符来写
public class Demo11 {
public static void main(String[] args) {
try (Writer writer = new FileWriter("d:/test.txt")) {
writer.write("xyz");
} catch (IOException e) {
e.printStackTrace();
}
}
}
边栏推荐
- [Oracle database] mammy tutorial Day12 character function
- 第八章 Web项目测试(此章完结)
- Alibaba cloud ~ simply send SMS
- 依图在实时音视频中语音处理的挑战丨RTC Dev Meetup
- How to create an index
- 0 basic self-study STM32 (wildfire) -- what is a register?
- 解析认知理论对创客教师实训的作用
- 矩阵运算
- 年度十强!赛宁网安再次入围《中国数字安全百强报告》
- Weekly recommended short video: what is the "computing world"?
猜你喜欢

Mt4/mql4 getting started to proficient in foreign exchange EA automatic trading tutorial - special identification of the K line on the chart

One hot and embedding

Three concurrent features 1- visibility

Mt4/mql4 getting started to proficient in foreign exchange EA automatic trading tutorial - identify the emergence of the new K line

Basic concepts of homomorphic encryption

Chapter VIII web project testing (the end of this chapter)

Example of multipoint alarm clock

Any to Any 实时变声的实现与落地丨RTC Dev Meetup

Mt4/mql4 getting started to proficient in foreign exchange EA automatic trading tutorial - common functions of MQL language

Mt4/mql4 getting started to be proficient in EA tutorial lesson 6 - common functions of MQL language (VI) - common order function
随机推荐
Svn submits subfolder questions
Questions 101 to 200 of the national information security grade examination nisp level 1 question bank (1)
Thread status (timed wait, lock blocking, infinite wait (key))
Easyui数据表实现增删改
中断中为何不能使用信号量,中断上下文为何不能睡眠
QT 控件增加双击事件
How to design the dead shot, the best and eye-catching performance of the watch Vanguard
Calculation days ()
Design skills of common table structure in database design
Questions 1 to 100 of the national information security grade examination nisp level 1 question bank (1)
Spark Yarn内存资源计算分析(参考)--Executor Cores、Nums、Memory优化配置
Web Knowledge 2 (request+response)
Using KDJ metrics on MT4
Oracle database pl/sql procedure body, cursor, stored procedure
JSON使用示例
Collections以及Arrays
找出不是两个数组共有的元素
Basic concepts of homomorphic encryption
Some problems encountered in using swagger in golang
377.组合总和 Ⅳ