第一章 字节流
1.1 字节输入流【InputStream】
java.io.InputStream 抽象类是表示字节输入流的所有类的超类,可以读取字节信息到内存中。它定义了字节输入
流的基本共性功能方法。
public void close() :关闭此输入流并释放与此流相关联的任何系统资源。
public abstract int read() : 从输入流读取数据的下一个字节。
public int read(byte[] b) : 从输入流中读取一些字节数,并将它们存储到字节数组 b中 。
close方法,当完成流的操作时,必须调用此方法,释放系统资源。
1.2 FileInputStream类
java.io.FileInputStream
类是文件输入流,从文件中读取字节。
构造方法
FileInputStream(File file)
: 通过打开与实际文件的连接来创建一个
FileInputStream
,该文件由文件系
统中的
File
对象
fifile
命名。
FileInputStream(String name)
: 通过打开与实际文件的连接来创建一个
FileInputStream
,该文件由文件
系统中的路径名
name
命名。
当你创建一个流对象时,必须传入一个文件路径。该路径下,如果没有该文件
,
会抛出
FileNotFoundException
。
构造举例,代码如下:
public static
void
main
(
String
[]
args
)
throws
IOException
{
//
使用
File
对象创建流对象
File file
=
new
File
(
"a.txt"
);
FileInputStream fis1
=
new
FileInputStream
(
file
);
//
使用文件名称创建流对象
FileInputStream fis2
=
new
FileInputStream
(
"a.txt"
);
}
读取字节数据
1.
读取字节
:
read
方法,每次可以读取一个字节的数据,提升为
int
类型,读取到文件末尾,返回
-
1
,代码使
用演示:
public static
void
main
(
String
[]
args
)
throws
IOException
{
//
使用文件名称创建流对象
FileInputStream fis
=
new
FileInputStream
(
"a.txt"
);
//
读取数据,返回一个字节
int
read
=
fis
.
read
();
System
.
out
.
println
((
char
)
read
);
read
=
fis
.
read
();
System
.
out
.
println
((
char
)
read
);
read
=
fis
.
read
();
System
.
out
.
println
((
char
)
read
);
read
=
fis
.
read
();
System
.
out
.
println
((
char
)
read
);
read
=
fis
.
read
();
System
.
out
.
println
((
char
)
read
);
//
读取到末尾
,
返回
-1
read
=
fis
.
read
();
System
.
out
.
println
(
read
);
//
关闭资源
fis
.
close
();
}
循环改进读取方式,代码使用演示:
public static
void
main
(
String
[]
args
)
throws
IOException
{
//
使用文件名称创建流对象
FileInputStream fis
=
new
FileInputStream
(
"a.txt"
);
//
定义变量,保存数据
int
b
;
//
循环读取
while
((
b
=
fis
.
read
())
!=-
1
) {
System
.
out
.
println
((
char
)
b
);
}
//
关闭资源
fis
.
close
();
}
1.
虽然读取了一个字节,但是会自动提升为
int
类型。
2. 流操作完毕后,必须释放系统资源,千万记得,调用close方法。
2.
使用字节数组读取
:
read(byte[] b)
,每次读取
b
的长度个字节到数组中,返回读取到的有效字节个数,读
取到末尾时,返回
-
1
,代码使用演示:
public static
void
main
(
String
[]
args
)
throws
IOException
{
//
使用文件名称创建流对象
.
FileInputStream fis
=
new
FileInputStream
(
"a.txt"
);
//
文件中为
abcde
//
定义变量,作为有效个数
int
len
;
//
定义字节数组,作为装字节数据的容器
byte
[]
b
=
new
byte
[
2
];
//
循环读取
while
((
len
=
fis
.
read
(
b
))
!=-
1
) {
//
每次读取后
,
把数组变成字符串打印
System
.
out
.
println
(
new
String
(
b
));
}
//
关闭资源
fis
.
close
();
}
错误数据
,是由于最后一次读取时,只读取一个字节
,数组中,上次读取的数据没有被完全替换,所以要通
过
len
,获取有效的字节,代码使用演示:
public static
void
main
(
String
[]
args
)
throws
IOException
{
//
使用文件名称创建流对象
.
FileInputStream fis
=
new
FileInputStream
(
"a.txt"
);
//
文件中为
a
//
定义变量,作为有效个数
int
len
;
//
定义字节数组,作为装字节数据的容器
byte
[]
b
=
new
byte
[
2
];
//
循环读取
while
((
len
=
fis
.
read
(
b
))
!=-
1
) {
//
每次读取后
,
把数组的有效字节部分,变成字符串打印
System
.
out
.
println
(
new
String
(
b
,
0
,
len
));
// len
每次读取的有效字节个数
}
//
关闭资源
fis
.
close
();
}
第二章 字符流
当使用字节流读取文本文件时,可能会有一个小问题。就是遇到中文字符时,可能不会显示完整的字符,那是因为
一个中文字符可能占用多个字节存储。所以
Java
提供一些字符流类,以字符为单位读写数据,专门用于处理文本文
件。
2.1
字符输入流【
Reader
】
java.io.Reader
抽象类是表示用于读取字符流的所有类的超类,可以读取字符信息到内存中。它定义了字符输入
流的基本共性功能方法。
public void close()
:关闭此流并释放与此流相关联的任何系统资源。
public int read()
: 从输入流读取一个字符。
public int read(char[] cbuf)
: 从输入流中读取一些字符,并将它们存储到字符数组
cbuf
中 。
2.2 FileReader
类
java.io.FileReader
类是读取字符文件的便利类。构造时使用系统默认的字符编码和默认字节缓冲区。
1.
字符编码:字节与字符的对应规则。
Windows
系统的中文编码默认是
GBK
编码表。
idea
中
UTF-8
2.
字节缓冲区:一个字节数组,用来临时存储字节数据。
构造方法
FileReader(File file)
: 创建一个新的
FileReader
,给定要读取的
File
对象。
FileReader(String fileName)
: 创建一个新的
FileReader
,给定要读取的文件的名称。
当你创建一个流对象时,必须传入一个文件路径。类似于
FileInputStream
。
构造举例,代码如下:
public static
void
main
(
String
[]
args
)
throws
IOException
{
//
使用
File
对象创建流对象
File file
=
new
File
(
"b.txt"
);
FileReader fr1
=
new
FileReader
(
file
);
//
使用文件名称创建流对象
FileReader fr2
=
new
FileReader
(
"a.txt"
);
}
读取字符数据
1.
读取字符
:
read
方法,每次可以读取一个字符的数据,提升为
int
类型,读取到文件末尾,返回
-
1
,循环读
取,代码使用演示:
public static
void
main
(
String
[]
args
)
throws
IOException
{
//
使用文件名称创建流对象
FileReader fr
=
new
FileReader
(
"a.txt"
);
//
定义变量,保存数据
int
b
;
//
循环读取
while
((
b
=
fr
.
read
())
!=-
1
) {
System
.
out
.
println
((
char
)
b
);
}
//
关闭资源
fr
.
close
();
}
2.
使用字符数组读取
:
read(char[] cbuf)
,每次读取
b
的长度个字符到数组中,返回读取到的有效字符个数,
读取到末尾时,返回
-
1
,代码使用演示:
public static
void
main
(
String
[]
args
)
throws
IOException
{
//
使用文件名称创建流对象
FileReader fr
=
new
FileReader
(
"a.txt"
);
//
定义变量,保存有效字符个数
int
len
;
//
定义字符数组,作为装字符数据的容器
char
[]
cbuf
=
new
char
[
2
];
//
循环读取
while
((
len
=
fr
.
read
(
cbuf
))
!=-
1
) {
System
.
out
.
println
(
new
String
(
cbuf
));
}
//
关闭资源
fr
.
close
();
}
获取有效的字符改进,代码使用演示:
//
使用文件名称创建流对象
FileReader fr
=
new
FileReader
(
"a.txt"
);
//
定义变量,保存有效字符个数
int
len
;
//
定义字符数组,作为装字符数据的容器
char
[]
cbuf
=
new
char
[
2
];
//
循环读取
while
((
len
=
fr
.
read
(
cbuf
))
!=-
1
) {
System
.
out
.
println
(
new
String
(
cbuf
,
0
,
len
));
}
//
关闭资源
fr
.
close
();
2.3
字符输出流【
Writer
】
java.io.Writer
抽象类是表示用于写出字符流的所有类的超类,将指定的字符信息写出到目的地。它定义了字节
输出流的基本共性功能方法。
public abstract void close()
:关闭此输出流并释放与此流相关联的任何系统资源。
public abstract void flush()
:刷新此输出流并强制任何缓冲的输出字符被写出。
public void write(int c)
:写出一个字符。
public void write(char[] cbuf)
:将
b.length
字符从指定的字符数组写出此输出流。
public abstract void write(char[] b, int off, int len)
:从指定的字符数组写出
len
字符,从偏移量
offff
开始输出到此输出流。
public void write(String str)
:写出一个字符串。
2.4 FileWriter
类
java.io.FileWriter
类是写出字符到文件的便利类。构造时使用系统默认的字符编码和默认字节缓冲区。
构造方法
FileWriter(File file)
: 创建一个新的
FileWriter
,给定要读取的
File
对象。
FileWriter(String fileName)
: 创建一个新的
FileWriter
,给定要读取的文件的名称。
当你创建一个流对象时,必须传入一个文件路径,类似于
FileOutputStream
。
构造举例,代码如下:
//
使用文件名称创建流对象
FileReader fr
=
new
FileReader
(
"a.txt"
);
//
定义变量,保存有效字符个数
int
len
;
//
定义字符数组,作为装字符数据的容器
char
[]
cbuf
=
new
char
[
2
];
//
循环读取
while
((
len
=
fr
.
read
(
cbuf
))
!=-
1
) {
System
.
out
.
println
(
new
String
(
cbuf
,
0
,
len
));
}
//
关闭资源
fr
.
close
();
}
2.3
字符输出流【
Writer
】
java.io.Writer
抽象类是表示用于写出字符流的所有类的超类,将指定的字符信息写出到目的地。它定义了字节
输出流的基本共性功能方法。
public abstract void close()
:关闭此输出流并释放与此流相关联的任何系统资源。
public abstract void flush()
:刷新此输出流并强制任何缓冲的输出字符被写出。
public void write(int c)
:写出一个字符。
public void write(char[] cbuf)
:将
b.length
字符从指定的字符数组写出此输出流。
public abstract void write(char[] b, int off, int len)
:从指定的字符数组写出
len
字符,从偏移量
offff
开始输出到此输出流。
public void write(String str)
:写出一个字符串。
2.4 FileWriter
类
java.io.FileWriter
类是写出字符到文件的便利类。构造时使用系统默认的字符编码和默认字节缓冲区。
构造方法
FileWriter(File file)
: 创建一个新的
FileWriter
,给定要读取的
File
对象。
FileWriter(String fileName)
: 创建一个新的
FileWriter
,给定要读取的文件的名称。
当你创建一个流对象时,必须传入一个文件路径,类似于
FileOutputStream
。
构造举例,代码如下:
public static
void
main
(
String
[]
args
)
throws
IOException
{
//
使用
File
对象创建流对象
File file
=
new
File
(
"b.txt"
);
FileWriter fw1
=
new
FileWriter
(
file
);
//
使用文件名称创建流对象
FileWriter fw2
=
new
FileWriter
(
"a.txt"
);
}
写出字符
:
write(int b)
方法,每次可以写出一个字符数据,代码使用演示:
public static
void
main
(
String
[]
args
)
throws
IOException
{
//
使用文件名称创建流对象
FileWriter fw
=
new
FileWriter
(
"a.txt"
);
//
写出数据
fw
.
write
(
97
);
//
写出第
1
个字符
fw
.
write
(
'b'
);
//
写出第
2
个字符
fw
.
write
(
'C'
);
//
写出第
3
个字符
fw
.
write
(
40000
);
//
写出第
4
个字符,中文编码表中
40000
对应一个汉字。
/*
【注意】关闭资源时
,
与
FileOutputStream
不同。
如果不关闭
,
数据只是保存到缓冲区,并未保存到文件。
*/
// fw.close();
}
关闭和刷新
因为内置缓冲区的原因,如果不关闭输出流,无法写出字符到文件中。但是关闭的流对象,是无法继续写出数据
的。如果我们既想写出数据,又想继续使用流,就需要
flush
方法了。
flush
:刷新缓冲区,流对象可以继续使用。
close
:关闭流,释放系统资源。关闭前会刷新缓冲区。
public static
void
main
(
String
[]
args
)
throws
IOException
{
//
使用文件名称创建流对象
FileWriter fw
=
new
FileWriter
(
"a.txt"
);
//
写出数据,通过
flush
fw
.
write
(
'
刷
'
);
//
写出第
1
个字符
fw
.
flush
();
fw
.
write
(
'
新
'
);
//
继续写出第
2
个字符,写出成功
fw
.
flush
();
//
写出数据,通过
close
fw
.
write
(
'
关
'
);
//
写出第
1
个字符
fw
.
close
();
fw
.
write
(
'
闭
'
);
//
继续写出第
2
个字符
,
【报错】
java.io.IOException: Stream closed
fw
.
close
();
}
写出其他数据
1.
写出字符数组
:
write(char[] cbuf)
和
write(char[] cbuf, int off, int len)
,每次可以写出字符数
组中的数据,用法类似
FileOutputStream
,代码使用演示:
public static
void
main
(
String
[]
args
)
throws
IOException
{
//
使用文件名称创建流对象
FileWriter fw
=
new
FileWriter
(
"a.txt"
);
//
字符串转换为字节数组
char
[]
chars
=
"硅谷
程序员
"
.
toCharArray
();
//
写出字符数组
fw
.
write
(
chars
);
// 硅谷
程序员
//
写出从索引
2
开始,
2
个字节。索引
2
是
'
程
'
,两个字节,也就是
'
程序
'
。
fw
.
write
(
b
,
2
,
2
);
//
程序
//
关闭资源
fos
.
close
();
}
2.
写出字符串
:
write(String str)
和
write(String str, int off, int len)
,每次可以写出字符串中的
数据,更为方便,代码使用演示:
public static
void
main
(
String
[]
args
)
throws
IOException
{
//
使用文件名称创建流对象
FileWriter fw
=
new
FileWriter
(
"fw.txt"
);
//
字符串
String
str
=
"硅谷
程序员
"
;
//
写出字符数组
fw
.
write
(
str
);
//
硅谷
程序员
//
写出从索引
2
开始,
2
个字节。索引
2
是
'
程
'
,两个字节,也就是
'
程序
'
。
fw
.
write
(
str
,
2
,
2
);
//
程序
//
关闭资源
fos
.
close
();
}
3.
续写和换行
:操作类似于
FileOutputStream
public static
void
main
(
String
[]
args
)
throws
IOException
{
//
使用文件名称创建流对象,可以续写数据
FileWriter fw
=
new
FileWriter
(
"a.txt"
,
true
);
//
写出字符串
fw
.
write
(
"硅谷
"
);
//
写出换行
fw
.
write
(
"\r\n"
);
//
写出字符串
fw
.
write
(
"
程序员
"
);
//
关闭资源
fw
.
close
();
}
字符流,只能操作文本文件,不能操作图片,视频等非文本文件。
当我们单纯读或者写文本文件时 使用字符流 其他情况使用字节流。