码迷,mamicode.com
首页 > 编程语言 > 详细

Java基础学习 —— io

时间:2017-09-05 16:59:16      阅读:176      评论:0      收藏:0      [点我收藏+]

标签:system.in   amr   传输   except   pen   tput   字符   ati   view   

 1 /**
 2 解决数据与数据之间的传输问题。
 3 字节流:
 4 输入字节流:
 5 -------| InputStream 所有输入字节流的基类。抽象类。
 6 -----------| FileInputStream 读取文件的输入字节流。
 7 -----------| BufferedInputStream 缓冲输入字符的字节流 该类内部 维护了一个8kb的字节数组。该类出现的目的 是为了提高读取文件数据的效率。
 8 
 9 输出字节流:
10 -------| OutputStream 所有输出字节流的基类。抽象类。
11 -----------| FileOutputStream 向文件输出数据 的输出字节流。
12 -----------| BufferedOutputStream 缓冲输出字节流,为了提高写入文件的效率。类内部也是维护了一个8kb的字节数组。
13 
14 什么情况使用字节流?
15 字节流都是为了读取到2进制的数据。
16 读取到的数据 不需要经过编码 解码的情况下, 可以使用字节流 such as: 图片数据 图片数据 不需要变成我们可以读懂的字符流。不需要进过解码
17 如果读到的数据 需要转换成 我们读的懂的字 那就必须要转换成字符流
18 字符流=字节流+编码(解码)
19 
20 
21 字符流:
22 输入字符流:
23 -------| Reader 所有输入字符流的基类。抽象类。不可以直接使用
24 -----------| FileReader 读取文件字符的输入字符流
25 -----------| BufferedReader 缓冲输入字符流 该类出现的目的 是为了提高读取文件字符的效率。内部是维护了一个8192个长度字符数组。除了提高效率,并且还拓展了功能, 多了个readline()的功能
26 
27 输出字符流:
28 -------| Writer 所有输出字符流的基类。抽象类
29 -----------| FileWriter 向文件输出字符数据的输出字符流。
30 -----------|BufferedWriter 缓冲输出字符流。为了提高写入文件的效率和拓展了功能newline()
31 
32 什么时候用到字符流呢?
33 读取到的字节数据需要被转换成字符数据,这时候我们就使用字符流,如果是读写的都是字符数据这时候我们需要用字符流。
34 
35 
36 转换流:
37 -------|InputStreamReader 输入字节流的转换流。可以吧一个输入字节流 转换成输入字符流,(前面字节,后面字符)
38 -------|OutputStreamReader 输出字节流的转换流
39 
40 转换流的作用:
41   1.可以吧对应的字节流转换成字符流使用。
42   2.
43 
44  */
45 public class demo1 {
46     public static void main(String[] args) throws Exception {
47         testInput();
48     }
49     public static void testInput() throws Exception{
50         
51         //标准输入流,默认读取控制台。用in来读控制台的内容
52         InputStream in = System.in;
53         int read = in.read();
54         System.out.println(read);
55     }
56 }

 该demo输出的是字节流

技术分享
abc
97
View Code

如果想要将输入内容完整的输出到控制台 那么我们就需要将字节流转换为字符流。

 1 public class demo1 {
 2     public static void main(String[] args) throws Exception {
 3         testInput();
 4     }
 5     public static void testInput() throws Exception{
 6         
 7         //标准输入流,默认读取控制台。用in来读控制台的内容
 8         System.out.println("输入:");
 9         InputStream in = System.in;
10         //int read = in.read();
11 
12         //将in转换为字符流
13         InputStreamReader iReader = new InputStreamReader(in);
14         
15         BufferedReader bReader = new BufferedReader(iReader);
16         System.out.println("输出:"+bReader.readLine());
17     }
18 }public class demo1 {
19     public static void main(String[] args) throws Exception {
20         testInput();
21     }
22     public static void testInput() throws Exception{
23         
24         //标准输入流,默认读取控制台。用in来读控制台的内容
25         System.out.println("输入:");
26         InputStream in = System.in;
27         //int read = in.read();
28 
29         //将in转换为字符流
30         InputStreamReader iReader = new InputStreamReader(in);
31         
32         BufferedReader bReader = new BufferedReader(iReader);
33         System.out.println("输出:"+bReader.readLine());
34     }
35 }
技术分享
输入:
abc
输出:abc
View Code

 转换流将输出字节流 转换为输出字符流。

1     public static void testOutput() throws Exception, IOException {
2         Socket socket = new Socket(InetAddress.getLocalHost(), 9090);
3         //获取到字节流
4         OutputStream outputStream = socket.getOutputStream();
5         //将输出字节流转化为输出字符流。
6         OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream);
7         outputStreamWriter.write("ok");
8     }

转换流的第二个作用

2.读写文件的时候FileReader,FileWriter默认使用的是GBK编码表,不能由自己来指定码表来读写数据。而转换流可以指定码表的读写转换数据。

 1     public static void writeFile() throws IOException{
 2         FileOutputStream fileOutputStream2 = new FileOutputStream("D:\\a.txt");
 3         OutputStreamWriter outputStreamWriter2 = new OutputStreamWriter(fileOutputStream2,"utf-8");
 4         outputStreamWriter2.write("sony是猪");
 5         outputStreamWriter2.close();
 6     }
 7     public static void ReadFile() throws IOException{
 8         FileInputStream fileInputStream = new FileInputStream("D:\\a.txt");
 9         //创建转换流并且创建码表
10         InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "utf-8");
11         int content = 0;
12         while((content = inputStreamReader.read())!=-1){
13             System.out.print((char)content);
14         }
15         fileInputStream.close();
16     }
技术分享
sony是猪
View Code

 

Java基础学习 —— io

标签:system.in   amr   传输   except   pen   tput   字符   ati   view   

原文地址:http://www.cnblogs.com/wy0904/p/7472634.html

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