1、 流的继承关系,以及字节流和字符流。
2、 节点流FileOutputStream和FileInputStream和处理流BufferedInputStream和BufferedOutputStream。以及对应的FileOutputWriter,FileInputReader,BufferedInputReader,BufferedOutputWriter。
3、 转换流InputStreamReader和OutputStreamWriter
一:流的继承关系
字节流
字符流
字符流和字节流的使用范围:字节流一般用来处理图像,视频,以及PPT,Word类型的文件。字符流一般用于处理纯文本类型的文件,如TXT文件等,字节流可以用来处理纯文本文件,但是字符流不能用于处理图像视频等非文本类型的文件。
二:处理流BufferedReader,BufferedWriter,BufferedInputStream
BufferedOutputsStream,都要包上一层节点流。也就是说处理流是在节点流的基础之上进行的,带有Buffered的流又称为缓冲流,缓冲流处理文件的输入输出的速度是最快的。所以一般缓冲流的使用比较多。
下面是两个简单的文件复制的实例:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
public class MycopyTest {
public static void main(String[] args) {
File src = new File( "D:/1.jpg" );
// D:/1.jpg必须的存在不然会报错
File dest = new File( "D:/2.jpg" );
// 如果D:/2.jpg存在则覆盖,如果不存在则新建
streamCopy(src, dest);
}
private static void readCopy(File src,File dest)
{
FileReader fr= null ;
FileWriter fw= null ;
BufferedReader br= null ;
BufferedWriter bw= null ;
try {
fr= new FileReader(src);
fw= new FileWriter(dest);
br= new BufferedReader(fr);
bw= new BufferedWriter(fw);
String str;
while ((str=br.readLine())!= null )
{
bw.write(str);
bw.newLine();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
bw.close();
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private static void streamCopy(File src, File dest) {
FileInputStream fis = null ;
FileOutputStream fos = null ;
BufferedInputStream bis = null ;
BufferedOutputStream bos = null ;
try {
fis = new FileInputStream(src);
fos = new FileOutputStream(dest);
bis = new BufferedInputStream(fis);
bos = new BufferedOutputStream(fos);
int len;
byte [] b = new byte [ 1024 ];
while ((len = bis.read(b)) != - 1 ) {
bos.write(b, 0 , len);
// bos.write(b,0,len);是把读到数组的大小字节写入
// bos.write(b);最后一次如果数组未写满的话就会多读。
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
bos.close();
bis.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
|
三:转换流InputStreamReader和OutputStreamWriter
转换流的作用,文本文件在硬盘中以字节流的形式存储时,通过InputStreamReader读取后转化为字符流给程序处理,程序处理的字符流通过OutputStreamWriter转换为字节流保存。
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
public class InputStreamWriterTest {
public static void main(String[] args) {
File src = new File( "D:/Files/狗屁.txt" );
File dest = new File( "D:/Files/斯密斯.txt" );
BufferedWriter bw = null ;
BufferedReader br = null ;
FileInputStream fis = null ;
FileOutputStream fos = null ;
try {
fis = new FileInputStream(src);
fos = new FileOutputStream(dest);
InputStreamReader ir = new InputStreamReader(fis, "GBK" );
OutputStreamWriter ow = new OutputStreamWriter(fos, "GBK" );
bw = new BufferedWriter(ow);
br = new BufferedReader(ir);
String str;
while ((str = br.readLine()) != null ) {
bw.write(str);
bw.newLine();
bw.flush();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
bw.close();
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
|
相关文章
猜你喜欢
- 64M VPS建站:能否支持高流量网站运行? 2025-06-10
- 64M VPS建站:怎样选择合适的域名和SSL证书? 2025-06-10
- 64M VPS建站:怎样优化以提高网站加载速度? 2025-06-10
- 64M VPS建站:是否适合初学者操作和管理? 2025-06-10
- ASP.NET自助建站系统中的用户注册和登录功能定制方法 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-05-29 53
-
2025-05-27 70
-
2025-05-29 59
-
2025-05-25 92
-
2025-05-29 37
热门评论