IO流技术
IO流的基本概念
·流的原理
- 在java程序中,对于数据的输入\输出操作以“流”(stream)方式进行;
- J2SDK提供了各种各样的“流”类,用以获取不同种类的数据;程序中通过标准的方法输入或输出数据;
- java的流类型一般位于java.io包中
·流的概念
数据源:1.data source. 提供原始数据的原始媒介。常见的:数据库、文件、其他程序、内存、网络连接、IO设备。
2.数据源就像水箱,流就像水管中流着的水流,程序就是我们最终的用户。流是一个抽象、动态的概念,是一连串连续动态的数据集合。
流的细分和体系_四大抽象类
·流的分类
流的方向:1.输入流:数据源到程序(InputStream、Reader读进来)
2.输出流:程序到目的地(OutPutStream、Writer写出去)
处理数据单元:1.字节流:按照字节读取数据(InputStream、OutputStream)
2.字符流:按照字符读取数据(Reader、Writer)
注意:输入输出是相对于程序而言,而不是相对于源和目标而言。
功能不同:1.节点流:可以直接从数据源或目的地读写数据。
2.处理流(包装流):不直接连接到数据源或目的地,是其他流进行封装。目的主要是简化操作和提高性能。
节点流和数据流的处理关系:1.节点流处于io操作的第一线,所有操作必须通过他们进行;
2.处理流可以对其他流进行处理(提高效率或操作灵活性)
·IO流的体系
InputStream和OutputStream:java语言中最基本的两个字节输入输出类。
其他所有字节输入输出流类都继承自这两个基类。
这两个类都是抽象类,不能创建它们的实例,只能使用它们的子类。
Reader和Writer:java语言中最基本的两个字符输入输出类。
文件字节流
·FileInputStream/FileOutputStream
使用FileInputStream读取文件内容
1.abstract int read();
1 package com.zqf.ioproject;
2
3 import java.io.File;
4 import java.io.FileInputStream;
5 import java.io.FileNotFoundException;
6 import java.io.IOException;
7
8 public class TestInputStream {
9 public static void main(String[] args) throws IOException {
10 //(1)数据源与应用程序之间搭建管理(通过创建对象)
11 FileInputStream fis = new FileInputStream(new File("C:\\test.txt"));
12 //(2)从数据源开始向程序中读数据
13 int count = fis.available(); //统计文件中字符的个数
14 System.out.println(count);
15 //中转站比较小,一次读一个字节
16 //System.out.println(fis.read( )); //读取一个字节
17 //System.out.println(fis.available());
18 int buf = 0; //存储读到的字节
19 while((buf=fis.read())!=-1){
20 System.out.println((char)buf);
21 }
22 //(3)关闭
23 fis.close();
24 }
25 }
2.int read(byte b[]);
1 package com.zqf.ioproject;
2
3 import java.io.FileInputStream;
4 import java.io.FileNotFoundException;
5 import java.io.IOException;
6
7 public class TestInputStream2 {
8 public static void main(String[] args) throws IOException {
9 //(1)搭桥
10 FileInputStream fis = new FileInputStream("C:\\test.txt");
11 //(2)创建大一些的中转站
12 byte [] buf = new byte[1024];
13 int len = 0; //用于存储每次读到的实际字符
14 while((len=fis.read(buf))!=-1){
15 //借助String类构造方法
16 System.out.println(new String(buf,0,len));
17 }
18 //(3)关闭
19 fis.close();
20 }
21 }
3.int read(byte b[],int off,int len);
4.int available();
5.close();
使用FileOutputStream写内容到文件
1.abstract void write(int b);
2.void write(byte b[]);
3.void write(byte b[],int off,int len);
4.void flush();
5.void close()
1 package com.zqf.ioproject;
2
3 import java.io.FileNotFoundException;
4 import java.io.FileOutputStream;
5 import java.io.IOException;
6
7 public class TestOutputStream {
8 public static void main(String[] args) {
9 //(1)搭桥
10 FileOutputStream fos=null;
11 try {
12 fos = new FileOutputStream("C:\\test.txt");
13 //(2)写数据,一次写一个字节
14 //fos.write(97);
15 //(3)一次写多个字节
16 byte[] buf ="helloworld".getBytes();
17 fos.write(buf);
18 } catch (FileNotFoundException e) {
19 // TODO Auto-generated catch block
20 e.printStackTrace();
21 } catch (IOException e) {
22 // TODO Auto-generated catch block
23 e.printStackTrace();
24 }finally{
25 //(3)关闭
26 try {
27 if(fos!=null){
28 fos.close();
29 }
30 } catch (IOException e) {
31 // TODO Auto-generated catch block
32 e.printStackTrace();
33 }
34 }
35 }
36 }
使用字节流实现文件复制
·文件复制的原理
·文件复制的代码实现
1 package com.zqf.ioproject;
2
3 import java.io.FileInputStream;
4 import java.io.FileNotFoundException;
5 import java.io.FileOutputStream;
6 import java.io.IOException;
7
8 public class TestFileCopy {
9 public static void main(String[] args) {
10 //数据源是文件
11 FileInputStream fis=null;
12 //目的地
13 FileOutputStream fos;
14 try {
15 fis = new FileInputStream("D:\\test.txt");
16 fos = new FileOutputStream("C:\\target.txt");
17
18 byte [] buf=new byte[1024]; //中转站
19 int len=0; //用于存储每次读到的字节个数
20 while((len=fis.read(buf))!=-1){
21 fos.write( buf,0,len);
22 }
23
24 /*int b=0;//用于存储读到的字节,(中转站)
25 while ((b=fis.read())!=-1){
26 //写入文件
27 fos.write(b);
28 }*/
29 } catch (FileNotFoundException e) {
30 // TODO Auto-generated catch block
31 e.printStackTrace();
32 } catch (IOException e) {
33 // TODO Auto-generated catch block
34 e.printStackTrace();
35 }finally{
36 //关闭
37 try {
38 if(fis!=null){
39 fis.close();
40 }
41 } catch (IOException e) {
42 // TODO Auto-generated catch block
43 e.printStackTrace();
44 }
45 }
46 }
47 }
文件字符流
·Reader/Writer
使用Reader读取文件内容
- int read();
- int read(char [] cbuf);
- int read (char [] cbuf,int off,int len);
- int available();
- close();
1 package com.zqf.reader; 2 3 import java.io.FileNotFoundException; 4 import java.io.FileReader; 5 import java.io.IOException; 6 7 public class TestFileReader { 8 public static void main(String[] args) throws IOException { 9 //(1)搭桥 10 FileReader reader = new FileReader("C:\\test.txt"); 11 //(2)读取 12 //int b = reader.read(); //读到的字符的int类型数据 13 //System.out.println((char)b); 14 /*int b=0; //用于存储每次读到的字符数据的整数值 15 while ((b=reader.read())!=-1){ 16 System.out.println((char)b); 17 }*/ 18 19 char [] cbuf = new char[1024]; 20 int len = 0; //用于存储读到的字符的个数 21 while((len=reader.read(cbuf))!=-1){ 22 System.out.println(new String (cbuf,0,len)); 23 } 24 //(3)关闭 25 reader.close(); 26 } 27 }
·使用Writer写内容到文件
- void write(int c);
- void write(char[] cbuf);
- abstract void write(char [] cbuf,int off,int len);
- void write(String str);
- abstract void flush()
1 package com.zqf.writer;
2
3 import java.io.FileWriter;
4 import java.io.IOException;
5
6 public class TestWriter {
7 public static void main(String[] args) {
8 //创建对象
9 FileWriter writer=null;
10 try {
11 writer = new FileWriter("C:\\a.txt");
12 //写数据
13 writer.write("你好吗?"); //写到缓冲区中
14 } catch (IOException e) {
15 // TODO Auto-generated catch block
16 e.printStackTrace();
17 }finally{
18 //关闭
19 if(writer!=null){
20 try {
21 writer.close();
22 } catch (IOException e) {
23 // TODO Auto-generated catch block
24 e.printStackTrace();
25 }
26 }
27 }
28 }
29 }