IO流

1. I/O   输入/输出

流(stream):根据方向分为:输入流和输出流。方向是基于我们的程序的,以先进先出的方式发送到信息通道中。流向我们程序的流叫做:输入流;从程序向外流的叫做:输出流。

2. 输入流是用于获取(读取write())数据的,输出流是用于向外输出(写出 write())数据的。

  InputStream:该接口定义了输入流的特征。

  OutputStream:该接口定义了输出流的特征。

  FileInputStream: 文件输入流,专门用于从文件中读取字节到程序内存中。

  FileOutputStream: 文件输出流,专门用于冲内存中写入字节到文件中。

 1 import java.io.File;
 2 import java.io.FileInputStream;
 3 import java.io.FileNotFoundException;
 4 import java.io.IOException;
 5 
 6 public class Test1 {
 7     public static void main(String[] args) {
 8         // 读取文件中的字节
 9         File file = new File("D:" + File.separator + "javatest"
10                 + File.separator + "c.txt");
11         // (1)创建管道
12         FileInputStream in = null;
13         try {
14 
15             // (2)循环读取字节
16             in = new FileInputStream(file);
17             int t;
18             StringBuilder b = new StringBuilder();
19             while ((t = in.read()) != -1) {
20                 b.append((char) t);
21             }
22             System.out.println(b.toString());
23 
24         } catch (FileNotFoundException e) {
25             e.printStackTrace();
26         } catch (IOException e) {
27             e.printStackTrace();
28         }
29 
30         // (3)关闭管道
31         try {
32             in.close();
33         } catch (IOException e) {
34             e.printStackTrace();
35         }
36     }
37 }

注意: FileInputStream 会抛出:FileNotFoundException 异常- 如果该文件不存在,或者它是一个目录,而不是一个常规文件,抑或因为其他某些原因而无法打开进行读取。

           close会抛出:IOException异常 - 如果发生 I/O 错误。

 

 一次读取多个字节:

 1 import java.io.File;
 2 import java.io.FileInputStream;
 3 import java.io.FileNotFoundException;
 4 import java.io.IOException;
 5 
 6 public class Test2 {
 7     public static void main(String[] args) throws FileNotFoundException, IOException {
 8         File file = new File("D:" + File.separator + "javatest" + File.separator + "t.txt");
 9 //        (1)创建读取通道
10         FileInputStream in = new FileInputStream(file);
11 
12         byte[] buf = new byte[5];
13 //        (2)管道中读取多个字节到缓冲区
14         int len;
15         // len = in.read();//从管道中读取超过len之后返回-1;
16         StringBuilder b = new StringBuilder();
17         while ((len = in.read(buf)) != -1) {
18             String str = new String(buf, 0, len);
19             b.append(str);
20         }
21         System.out.println(b.toString());
22 //        (3)关闭通道
23         in.close();
24     }
25 
26 }

一次写入多个字节:

 1 import java.io.File;
 2 import java.io.FileNotFoundException;
 3 import java.io.FileOutputStream;
 4 import java.io.IOException;
 5 
 6 public class Test1 {
 7     public static void main(String[] args) throws FileNotFoundException, IOException {
 8         File file = new File("D:"+File.separator+"javatest"+File.separator+"x.txt");
 9 //        (1)创建输出流管道
10         FileOutputStream out = new FileOutputStream(file);
11 //        (2)写入数据到到管道中,
12         
13         /*一次写入一个字节
14          * out.write(97);
15          * out.write(98);
16          * out.write(99);
17         */
18         
19         //一次写入多个字节
20         String str = "hello 广州";//根据不同系统,默认的编码不同,本平台是Windows简体中文,所以默认GBK编码
21         byte[] buf = str.getBytes();
22         out.write(buf);
23         
24         /*byte[] buf = str.getBytes("UTF-8");
25         out.write(buf);*/ // 自定义编码导入;
26         
27         System.out.println("写入完成");
28         
29 //        (3)关闭流
30         out.close();
31         
32     }
33 
34 }

 InputStream/OutputStream 用于字节的读写。主要用于读取二进制文件(图片、音频、视频),也可以读取文件性文件。

 

3. 转换流

InputStreamReader 继承于Reader,是字节流通向字符流的桥梁,可以把字节流按照指定编码 解码 成字符流。

OutputStreamWriter 继承于Writer,是字符流通向字节流的桥梁,可以把字符流按照指定的编码 编码 成字节流。

1)OutputStreamWriter 创建一个字符集的输出流

 1 import java.io.File;
 2 import java.io.FileNotFoundException;
 3 import java.io.FileOutputStream;
 4 import java.io.IOException;
 5 import java.io.OutputStreamWriter;
 6 
 7 public class Test2 {
 8     public static void main(String[] args) throws IOException, FileNotFoundException {
 9         //程序输出,写入文档
10         String str = "Hello广州";
11         File file = new File("D:"+File.separator+"javatest"+File.separator+"z.txt");
12         //创建管道
13         
14         FileOutputStream out = new FileOutputStream(file);
15         OutputStreamWriter writer = new OutputStreamWriter(out, "UTF-8");
16         
17         //写入管道
18         writer.write(str);
19         
20         //刷新缓冲区
21         writer.flush();
22         
23         //关闭管道
24         out.close();
25         writer.close();
26         
27         System.out.println("完成写入");
28         
29     
30     }
31 
32 }

 

2)InputStreamReader 创建一个字符集的输入流

 1 import java.io.File;
 2 import java.io.FileInputStream;
 3 import java.io.FileNotFoundException;
 4 import java.io.IOException;
 5 import java.io.InputStreamReader;
 6 import java.io.UnsupportedEncodingException;
 7 
 8 public class Test3 {
 9     public static void main(String[] args) throws FileNotFoundException, UnsupportedEncodingException, IOException  {
10         File file = new File("D:" + File.separator + "javatest"
11                 + File.separator + "d.txt");
12 
13         // 建立管道
14         FileInputStream in = new FileInputStream(file);
15         InputStreamReader reader = new InputStreamReader(in, "UTF-8");
16 
17         // 读写文件
18         char[] cbuf = new char[2];
19         int len;
20 
21         StringBuilder sb = new StringBuilder();
22         while ((len = reader.read(cbuf)) != -1) {
23             sb.append(cbuf, 0, len);
24         }
25         System.out.println(sb.toString());
26     }
27 
28 }

 读取一个默认GBK编码的文件

 1 import java.io.File;
 2 import java.io.FileReader;
 3 import java.io.IOException;
 4 
 5 public class Test4 {
 6     public static void main(String[] args) throws IOException {
 7         // 读取一个本地的GBK编码文件 (本平台是Windows简体中文)
 8 
 9         File file = new File("D:" + File.separator + "javatest"
10                 + File.separator + "a.rar");
11         // 1创建管道
12         /*
13          * 可以不写这种方式 FileInputStream in = new FileInputStream(file);
14          * InputStreamReader reader = new InputStreamReader(in, "GBK");
15          */
16 
17         FileReader reader = new FileReader(file);
18         char[] cbuf = new char[2];
19         int len;
20         // 2读取
21         StringBuilder sb = new StringBuilder();
22         while ((len = reader.read(cbuf)) != -1) {
23             sb.append(cbuf, 0, len);
24 
25         }
26 
27         // 3关闭管道
28         reader.close();
29         cbuf.clone();
30 
31         System.out.println(sb.toString());
32 
33     }
34 
35 }

 4.缓冲字符高级流 BufferedWriter 和 BufferedReader

 1)BufferedWriter是缓冲字符输入流,以 作为单位写入字符

 2)BufferedReader是缓冲字符输出流,以 作为单位读取字符

5月初的广州不出太阳不升温,反而一连下了一个多星期的雨,返春寒了。

来个需求利用BufferedWriter写个一首诗到文本里 如:

惊闻昨夜大雨,
今晨河流湍急。
偶遇柱上蜗牛,
一步一步寻梦。
你我怎可知?

 1 import java.io.BufferedWriter;
 2 import java.io.File;
 3 import java.io.FileWriter;
 4 import java.io.IOException;
 5 
 6 public class Test1 {
 7     public static void main(String[] args) throws IOException {
 8         File file = new File("D:" + File.separator + "javatest"
 9                 + File.separator + "c.txt");
10         // 创建通道,以默认编码写入诗
11         FileWriter writer = new FileWriter(file);
12         BufferedWriter bWriter = new BufferedWriter(writer);
13 
14         // 写入一行
15         bWriter.write("惊闻昨夜大雨,");
16         bWriter.newLine();// 回车换行
17         /*
18          * bWriter.writer("\r\n"); 不建议这样写(因为windows平台可以这样写,如果是Linux或Mac则是"\n")
19          */
20         bWriter.write("今晨河流湍急。");
21         bWriter.newLine();
22         bWriter.write("偶遇柱上蜗牛,");
23         bWriter.newLine();
24         bWriter.write("一步一步寻梦。");
25         bWriter.newLine();
26         bWriter.write("你我怎么可知?");
27         bWriter.newLine();
28         
29         bWriter.flush();//刷新缓冲区
30         
31         //关闭管道
32         bWriter.close();
33         writer.close();
34         
35         System.out.println("写入成功");
36     }
37 }

 利用BufferedReader输出以上文件里的诗。

 1 import java.io.BufferedReader;
 2 import java.io.File;
 3 import java.io.FileNotFoundException;
 4 import java.io.FileReader;
 5 import java.io.IOException;
 6 
 7 public class Test3 {
 8     public static void main(String[] args) throws IOException, FileNotFoundException{
 9         File file = new File("D:"+File.separator+"javatest"+File.separator+"c.txt");
10         //1创建通道
11         FileReader reader = new FileReader(file);
12         BufferedReader bReader = new BufferedReader(reader);
13         
14         //读取行
15         String line;
16         while ((line=bReader.readLine()) != null) {
17             System.out.println(line);
18             
19         }
20         
21         bReader.close();
22     }
23 
24 }

 

posted @ 2019-05-06 21:38  abcdjava  阅读(222)  评论(0编辑  收藏  举报