码迷,mamicode.com
首页 > 其他好文 > 详细

IO流

时间:2019-05-06 23:19:53      阅读:156      评论:0      收藏:0      [点我收藏+]

标签:一个   信息   组类型   res   ali   main   这一   代码   uil   

IO

1.1 File类

File在java中表示(路径的)文件或者目录。

创建文件时会抛出检查时异常IOException。

1.1.1 File常用属性和方法

 1 package com.day506.IO;
 2 
 3 import java.io.File;
 4 import java.io.IOException;
 5 
 6 public class IO1 {
 7 public static void main(String[] args) {
 8         
 9         // 给定路径创建File对象
10         // File file = new File("D:"+File.separator+"javatest"+File.separator+"a.txt");
11         File file = new File("e:\\Test\\a.mp3");
12         // 文件的创建
13         if(!file.exists()){
14             boolean r;
15             try {
16                  r=file.createNewFile();
17                 if(r){
18                     System.out.println("文件创建成功");
19                 }
20             } catch (IOException e) {
21                 
22                 e.printStackTrace();
23             }
24         }
25         //文件的删除
26         file.delete();
27         System.out.println("删除文件成功");
28 }
29 }

1.1.2目录的创建

 

 1 package com.day506.IO;
 2 
 3 import java.io.File;
 4 
 5 
 6 
 7 public class IO1 {
 8 public static void main(String[] args) {
 9         
10         // 给定路径创建File对象
11         // File file = new File("D:"+File.separator+"javatest"+File.separator+"a.txt");
12         File file = new File("e:\\Test\\a\\b\\c");
13         // 文件的创建
14         if(!file.exists()){
15             boolean r=file.mkdirs();    
16             if (r) {
17                 System.out.println("目录创建成功");
18                 }
19             }
20         else{
21             System.out.println("目录已存在");
22            }
23     }
24 }

 

1.1.3目录的遍历

list():返回一个file表示的目录中的子目录或者文件,字符串数组类型。

 以下面代码为例:返回b目录里的子目录以及文件。

 

 1 import java.io.File;
 2 
 3 
 4 
 5 public class IO1 {
 6 public static void main(String[] args) {
 7 
 8         
 9         // 需求:遍历d:\javatest目录
10         // list()
11         //返回b目录里的子目录以及文件
12         File file =  new File("e:\\Test\\a\\b");
13         String[] list = file.list();
14         for (String str : list) {
15             System.out.print(str);
16             File f = new File(file.getPath()+"\\"+str);
17             if(f.isDirectory()) {
18                 System.out.println(" 目录");
19             }else {
20                 System.out.println(" 文件");
21             }
22         }
23         
24     }
25 }

 

listFiles():返回一个file表示的目录中的子目录或者文件File数组类型。

 

1.2 IO

 

1.2.1 

流(stream):流是一连串流动的数据(字节、字符),先进先出的方式发送的信息的通道中。

 

 技术图片

1.2.2 输入流和输出流

输入流

数据从源数据源程序的过程称为输入流可以理解为从源数据源读取数据到程序的过程

 技术图片

输出

数据从程序流到目的地的过程称为输出流。可以理解为把数据程序写入目的地的过程

 技术图片

 

数据源一般指提供数据的原始媒介,一般常见有文件、数据库、云端其他硬件等能提供数据的媒介。

1.2.3 的分类

按照流向分为输入流和输出流

按照处理单元分为字节流和字符流

按照功能分为节点流转换流

 

 技术图片

1.3 InputStream/OutputStream

InputStream 是所有字节输入流抽象父类提供了

read() 读取一个字节

read(byte[] buf) 读取一定量的字节到缓冲区数组 buf中

 

OutputStream 是所有字节输出流的抽象父类,提供了

write() 写入一个字节

write(byte[] buf) 写入一定量的字节到输出流

 

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

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

 

需求:文件读取一个字节:

 

 1 public static void main(String[] args) {
 2         
 3         // 需求:读取一个文件中的一个字节
 4         File file = new File("d:\\javatest\\a.txt");
 5         
 6         // 【1】创建管道
 7         FileInputStream in = null;
 8         
 9         try {
10             in = new FileInputStream(file);
11             
12             // 【2】从管道读取一个字节
13             /*
14             int t;
15             t = in.read();
16             t = in.read();
17             t = in.read();
18             t = in.read();
19             */
20             // System.out.println(t);
21             
22             // 循环读取一个字节
23             int t;
24             StringBuilder sb = new StringBuilder();
25             while( (t=in.read()) != -1 ) {
26                 sb.append((char)t);
27             }
28             
29             System.out.println(sb.toString());
30             
31             
32             
33         } catch (FileNotFoundException e) {
34             e.printStackTrace();
35         } catch(IOException e) {
36             e.printStackTrace();
37         }
38         
39         // 【3】关闭流管道
40         try {
41             in.close();
42         } catch (IOException e) {
43             e.printStackTrace();
44         }
45     }

 

一次读取多个字节:

 

 1 ublic static void main(String[] args) {
 2         
 3         // 需求:一次读取多个字节
 4         File file = new File("d:\\javatest\\a.txt");
 5         
 6         // 【1】创建管道
 7         FileInputStream in = null;
 8         
 9         try {
10             in = new FileInputStream(file);
11             
12             // 【2】从管道读取多个字节到缓冲区
13             /*
14             byte[] buf = new byte[5];
15             int len;
16             len = in.read(buf);
17             len = in.read(buf);
18             len = in.read(buf);
19             len = in.read(buf);
20             
21             for(byte b:buf) {
22                 System.out.print((char)b+"\t");
23             }
24             System.out.println(len);
25             */
26             
27             // 通过循环读取文件
28             byte[] buf = new byte[5];
29             int len;
30             StringBuilder sb = new StringBuilder();
31             while( (len=in.read(buf)) != -1 ) {
32                 // 读取的内容是原始二进制流,需要根据编码的字符集解码成对于字符
33                 String str = new String(buf,0,len);
34                 sb.append(str);
35             }
36             System.out.println(sb.toString());
37             
38             
39             
40             
41         } catch (FileNotFoundException e) {
42             e.printStackTrace();
43         } catch(IOException e) {
44             e.printStackTrace();
45         }
46         
47         // 【3】关闭流管道
48         try {
49             in.close();
50         } catch (IOException e) {
51             e.printStackTrace();
52         }
53     }

 

需求:按照指定编码写入文件:

 

 1 public static void main(String[] args) {
 2         
 3         
 4         File file = new File("d:\\javatest\\c.txt");
 5         
 6         FileOutputStream out = null;
 7         
 8         try {
 9             // 【1】创建输出流管道
10             out = new FileOutputStream(file);
11             
12             // 【2】写入数据到管道中
13             // 一次写入一个字节
14             /*
15             out.write(97);
16             out.write(98);
17             out.write(99);
18             */
19             
20             // 一次写入多个字节
21             String str = "hello world";
22             // gbk
23             /*
24             byte[] buf = str.getBytes();
25             out.write(buf);
26             */
27             
28             byte[] buf = str.getBytes("UTF-8");
29             out.write(buf);
30             
31             System.out.println("写入完成!");
32             
33         } catch (FileNotFoundException e) {
34             e.printStackTrace();
35         } catch (IOException e) {
36             e.printStackTrace();
37         }
38         
39         // 【3】关闭流
40         try {
41             out.close();
42         } catch (IOException e) {
43             e.printStackTrace();
44         }
45     }

 

注意:

[1]字符串写入文件时一定会存在编码问题

[2]使用utf8编码写入文件时,如果不含中文时,win系统会对文件的编码造成误判。

[3] 通过字节流写入文件时,管道写入一个字节,该字节立即写入文件中。

 

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

 

需求:d:\\javatest\\logo.png 复制到工程目录中并显示复制进度:

 

 1 public static void main(String[] args) throws FileNotFoundException,IOException {
 2         
 3         
 4         File oriFile = new File("d:\\javatest\\logo.jpg");
 5         File toFile = new File("logo.jpg");
 6         
 7         long totalLen = oriFile.length();    // 文件大小
 8         long cpyedLen = 0;    // 已复制完成的大小
 9         float progress = 0.0f;
10         
11         FileInputStream in = new FileInputStream(oriFile);
12         FileOutputStream out = new FileOutputStream(toFile);
13         
14         // 一次读取1kb
15         byte[] buf = new byte[512];
16         int len;
17         while( (len=in.read(buf)) != -1) {
18             out.write(buf, 0, len);
19             cpyedLen += len;
20             progress = cpyedLen*1.0f/totalLen;
21             System.out.println(progress);
22             
23             
24         }
25         
26         in.close();
27         out.close();
28         
29         System.out.println("复制完成!");
30         
31     }

 

思考:用字节流来读取文本性文件时,按字节读容易造成乱码。此时,能按字符来读取。

 

1.4 Reader/Writer

Reader 是字符输入流的抽象父类,提供了

read 一次读取一个字符

read(char[] cbuf) 一次读取多个字符到字符缓冲区cbuf返回长度表示读取的字符个数

 

Writer 是字符输出流的抽象父类,提供了

write一次写入一个字符

write(char[] cbuf)一次写入多个字符到字符缓冲区cbuf,返回长度表示读取的字符个数。

write(string)

 

FileReader 文件字符输入流,专门用于读取默认字符编码文本性文件。

FileWriter 文件字符输出流,专门用于写入默认字符编码的文本性文件。为了提高效率,FileWriter内部存在一个字节缓冲区,用于对待写入的字符进行统一编码字节缓冲区,一定要在关闭流之前,调用flush方法刷新缓冲区。

需求:一次读取一个字符/多个字符到cbuf:

 1 public static void main(String[] args) throws IOException {
 2         
 3         File file = new File("d:\\javatest\\d.txt");
 4         
 5         FileReader reader = new FileReader(file);
 6         
 7         // 【1】一次读取一个字符
 8         /*
 9         int c;
10         c = reader.read();
11         c = reader.read();
12         c = reader.read();
13         c = reader.read();
14         c = reader.read();
15         System.out.println((char)c);
16         */
17         
18         // 【2】一次读取多个字符到cbuf中
19         /*
20         char[] cbuf = new char[2];
21         int len;
22         len = reader.read(cbuf);
23         len = reader.read(cbuf);
24         len = reader.read(cbuf);
25         len = reader.read(cbuf);
26         System.out.println(Arrays.toString(cbuf));
27         System.out.println(len);
28         */
29         
30         char[] cbuf = new char[2];
31         int len;
32         StringBuilder sb = new StringBuilder();
33         while( (len=reader.read(cbuf)) != -1 ) {
34             sb.append(cbuf,0,len);
35         }
36         
37         System.out.println(sb);
38     }

需求:写入字符到文件中:

 

 1 public static void main(String[] args) throws IOException {
 2         
 3         
 4         File file = new File("d:\\javatest\\f.txt");
 5         
 6         FileWriter writer = new FileWriter(file);
 7         
 8         // 【1】一次写入一个字符
 9         /*writer.write(‘中‘);
10         writer.write(‘国‘);*/
11         
12         // 【2】一次写入多个字符
13         /*char[] cbuf = {‘h‘,‘e‘,‘l‘,‘l‘,‘o‘,‘中‘,‘国‘};
14         writer.write(cbuf);*/
15         
16         // 【3】一次写入一个字符串
17         String str = "hello你好";
18         writer.write(str);
19         
20         
21         // 刷新字节缓冲区
22         writer.flush();
23         
24         // 关闭流通道
25         writer.close();
26         
27         System.out.println("写入完成");
28     }

 

 

1.5 转换

(为了方便自己理解记忆:人用笔将脑袋里的东西通过笔写在纸上这一过程,人脑即程序(字符),笔为转换流,纸为目标文件,文件里储存的是字节。这个过程即是将字符流转换为字节流。也就是OutputStreamWriter。反过来就是InputStreamReader

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

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

 

1.5.1 转换流工作原理

 

 技术图片

需求:写入utf8文件:

 1 /**
 2  * 把一个字符串以utf8编码写入文件
 3  */
 4 public class Test01 {
 5     public static void main(String[] args) throws IOException {
 6         
 7         
 8         String str = "hello中国";
 9         File file = new File("d:\\javatest\\g.txt");
10         
11         // 【1】创建管道
12         FileOutputStream out = new FileOutputStream(file);
13         OutputStreamWriter writer = new OutputStreamWriter(out, "UTF-8");
14         
15         // 【2】写入管道
16         writer.write(str);
17         
18         // 【3】刷新缓冲区
19         writer.flush();
20         
21         // 【4】关闭管道
22         out.close();
23         writer.close();
24         
25         System.out.println("写入完成");
26     }
27 }

需求:读取utf8文件:

 1 /**
 2  * 读取utf8编码的文本文件
 3  */
 4 public class Test01 {
 5     public static void main(String[] args) throws IOException {
 6         
 7         File file = new File("d:\\javatest\\g.txt");
 8         
 9         // 【1】建立管道
10         FileInputStream in = new FileInputStream(file);
11         InputStreamReader reader = new InputStreamReader(in, "UTF-8");
12         
13         char[] cbuf = new char[2];
14         int len;
15         
16         StringBuilder sb = new StringBuilder();
17         while( (len=reader.read(cbuf))!=-1 ) {
18             sb.append(cbuf, 0, len);
19         }
20         System.out.println(sb.toString());
21         
22     }
23 }

注意:

[1]win平台默认的utf8编码的文本性文件带有BOM,java转换流写入的utf8文件不带BOM。所以java读取手动创建的utf8文件会出现一点乱码(?hello中国,?bom导致的)

[2] 一句话:用字符集编码,一定用字符集解码!

 

思考:

FileReader = InputStreamReader + GBK:

 1 /**
 2  * 读取一个gbk编码的文本性文件
 3  */
 4 public class Test02 {
 5     public static void main(String[] args) throws IOException {
 6         
 7         
 8         File file = new File("d:\\javatest\\f.txt");
 9         
10         // 【1】建立管道
11         /*
12          * FileInputStream in = new FileInputStream(file); 
13          * InputStreamReader reader =  new InputStreamReader(in, "GBK");
14          */
15 
16         FileReader reader = new FileReader(file);
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         
26         reader.close();
27         
28         System.out.println(sb.toString());
29     }
30 }

1.6 BufferedReader/BufferedWriter

BufferedReader 继承Reader提供

read  一次读取一个字符

read(char[] cbuf)  一次读取多个字符到字符缓冲区cbuf返回长度表示读取的字符个数

readLine() 用于读取一行文本,实现对文本的高效读取。

BufferedReader 初始化时需要一个reader本质BufferedReaderreader的基础上增加readLine()的功能。

 

BufferedWriter继承Writer提供

write 一次写入一个字符

write(char[] cbuf)一次写入多个字符到字符缓冲区cbuf,返回长度表示读取的字符个数。

write(string)用于写入一行文本,实现对文本的高效写入。

newline() 写入一个行分隔符。

 

需求:读取一首诗:

 

 1 public static void main(String[] args) throws IOException {
 2                 
 3         // 按行读取gbk文本性文件
 4         
 5         File file = new File("d:\\javatest\\i.txt");
 6         
 7         // 【1】创建管道
 8         FileReader reader = new FileReader(file);
 9         BufferedReader br = new BufferedReader(reader);
10         
11         // 【2】读取一行
12         /*
13         String line =  br.readLine();
14         line =  br.readLine();
15         line =  br.readLine();
16         line =  br.readLine();
17         */
18         
19         String line;
20         while( (line=br.readLine()) != null) {
21             System.out.println(line);
22         }
23     }

 

需求:gbk编码写入一首诗到文件:

 

 1 public static void main(String[] args) throws IOException {
 2         
 3         File file = new File("d:\\javatest\\j.txt");
 4         
 5         // 【1】创建gbk管道
 6         FileWriter writer = new FileWriter(file);
 7         BufferedWriter bw = new BufferedWriter(writer);
 8         
 9         // 【2】写入一行
10         bw.write("雨打梨花深闭门,忘了青春,误了青春。");
11         bw.newLine();
12         
13         bw.write("赏心乐事共谁论?花下销魂,月下销魂。");
14         
15         // for win
16         // bw.write("\r\n");
17         
18         // for unix/linux/mac
19         // bw.write("\n");
20         
21         bw.write("愁聚眉峰尽日颦,千点啼痕,万点啼痕。");
22         bw.newLine();
23         
24         // 【3】flush
25         bw.flush();
26         
27         // 【4】关闭管道
28         bw.close();
29         writer.close();
30     }

 

需求:utf8编码高效写入文件:

 

 1 /**
 2  * 以utf8写入一首诗
 3  * @author Administrator
 4  *
 5  */
 6 public class Test02 {
 7     public static void main(String[] args) throws IOException {
 8         
 9         File file = new File("d:\\javatest\\j-utf8.txt");
10         
11         // 【1】创建utf8管道
12         FileOutputStream out = new FileOutputStream(file);
13         OutputStreamWriter writer = new OutputStreamWriter(out, "UTF-8");
14         BufferedWriter bw = new BufferedWriter(writer);
15         
16         // 【2】写入一行
17         bw.write("雨打梨花深闭门,忘了青春,误了青春。");
18         bw.newLine();
19         
20         bw.write("赏心乐事共谁论?花下销魂,月下销魂。");
21         
22         // for win
23         bw.write("\r\n");
24         
25         // for unix/linux/mac
26         // bw.write("\n");
27         
28         bw.write("愁聚眉峰尽日颦,千点啼痕,万点啼痕。");
29         bw.newLine();
30         
31         // 【3】flush
32         bw.flush();
33         
34         // 【4】关闭管道
35         bw.close();
36         writer.close();
37     }
38 }

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

IO流

标签:一个   信息   组类型   res   ali   main   这一   代码   uil   

原文地址:https://www.cnblogs.com/LSZJZ/p/10822756.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!