处理流
1.缓冲流(重点):BufferedReader,BufferedWriter,BufferedInputStream,BufferedOutputStream
(1).作用:提高流读取和写入的速度,提高读写速度的原因:内部提供了一个缓冲区
(2).两个实例:
缓冲字符流(文本)文件输入输出
1 /* 2 * 使用BufferedReader和BufferedWriter实现文本文件复制 3 */ 4 public void testBufferedReaderWriter() 5 { 6 //创建文件和相应的流 7 BufferedReader br = null; 8 BufferedWriter bw = null; 9 try { 10 br = new BufferedReader(new FileReader(new File("hello.txt"))); 11 bw = new BufferedWriter(new FileWriter(new File("hi.txt"))); 12 13 //读写操作 14 char[] cbuf = new char[1024]; 15 int len; 16 while ((len = br.read(cbuf)) != -1) 17 { 18 bw.write(cbuf,0,len); 19 bw.flush();//刷新情况缓冲区的内容 20 } 21 } catch (Exception e) { 22 // TODO Auto-generated catch block 23 e.printStackTrace(); 24 } finally { 25 //关闭资源 26 if (br != null) 27 try { 28 br.close(); 29 } catch (IOException e) { 30 // TODO Auto-generated catch block 31 e.printStackTrace(); 32 } 33 if (bw != null) 34 try { 35 bw.close(); 36 } catch (IOException e) { 37 // TODO Auto-generated catch block 38 e.printStackTrace(); 39 } 40 }
缓冲字节流的(非文本)文件输入输出
1 //实现非文本文件的复制 2 public static void BufferedStreamTest() 3 { 4 //2.2造缓冲流 5 BufferedInputStream bis = null; 6 BufferedOutputStream bos = null; 7 try { 8 //1.造文件 9 File srcFile = new File("1.jpg"); 10 File desFile = new File("2.jpg"); 11 12 //2.造流 13 //2.1.造节点流 14 FileInputStream fis = new FileInputStream(srcFile); 15 FileOutputStream fos = new FileOutputStream(desFile); 16 bis = new BufferedInputStream(fis); 17 bos = new BufferedOutputStream(fos); 18 19 //3.复制的细节:读取,写入 20 byte[] buffer = new byte[10]; 21 int len; 22 while ((len = bis.read(buffer)) != -1) 23 { 24 bos.write(buffer,0,len); 25 } 26 } catch (FileNotFoundException e) { 27 // TODO Auto-generated catch block 28 e.printStackTrace(); 29 } catch (IOException e) { 30 // TODO Auto-generated catch block 31 e.printStackTrace(); 32 } finally { 33 //4.资源的关闭 34 //要求:先关闭外层的流,再关闭内层的流 35 //说明:系统在我们关闭外层流后,会帮我们自动关闭内层流(所以只需关闭外层流) 36 if (bis != null) 37 { 38 try { 39 bis.close(); 40 } catch (IOException e) { 41 // TODO Auto-generated catch block 42 e.printStackTrace(); 43 } 44 } 45 if (bos != null) 46 { 47 try { 48 bos.close(); 49 } catch (IOException e) { 50 // TODO Auto-generated catch block 51 e.printStackTrace(); 52 } 53 } 54 } 55 }
2.转换流(重点):
* (1).属于字符流
* InputStreamReader:将字节输入流转为字符输入流
* OutputStreamReader:将字符输出流转为字节输出流
* 所以整个过程就是将字节流先封装成字符流进行传输,最后再将其转回字节流的过程
* (2).作用:提供字节流与字符流之间的转换(整个过程:字节流->字符流->字符流->字节流)
*
* (3).解码过程:字节数组-->字符数组 ,字符串
* 编码过程:字符数组,字符串-->字节数组
*
常见的字符集补充
* ASCII:美国标准信息交换表
* ISO8859-1:拉丁表码,欧洲码表
* GB2312:中文编码表,最多两个字节编码表示所有字符
* GBK:国际标准编码表,融合了更多中文符号,最多两字节编码
* Unicode:国际标准码,融合目前所有使用的字符,为每个字符分配唯一的一个字符码
* UTF-8:变长的编码方式,可用1-4个字节来表示1个字符
应用:
1 //综合使用InputStreamReader和OutputStreamWriter 2 public static void test2() 3 { 4 InputStreamReader isr = null; 5 OutputStreamWriter osr = null; 6 try { 7 File file1 = new File("hello.txt"); 8 File file2 = new File("hi.txt"); 9 10 FileInputStream fis = new FileInputStream(file1); 11 FileOutputStream fos = new FileOutputStream(file2); 12 13 isr = new InputStreamReader(fis); 14 osr = new OutputStreamWriter(fos); 15 16 char[] cbuf = new char[5]; 17 int len; 18 while ((len = isr.read(cbuf)) != 0) 19 { 20 osr.write(cbuf,0,len); 21 } 22 } catch (FileNotFoundException e) { 23 // TODO Auto-generated catch block 24 e.printStackTrace(); 25 } catch (IOException e) { 26 // TODO Auto-generated catch block 27 e.printStackTrace(); 28 } finally { 29 if (isr != null) 30 try { 31 isr.close(); 32 } catch (IOException e) { 33 // TODO Auto-generated catch block 34 e.printStackTrace(); 35 } 36 if (osr != null) 37 try { 38 osr.close(); 39 } catch (IOException e) { 40 // TODO Auto-generated catch block 41 e.printStackTrace(); 42 } 43 } 44 }
3.标准输入输出流:(1).System.in:默认从键盘中读入 System.out:默认从显示屏上读出
(2).System类的setIn(InputStream is) / setOut(PrintStream ps)方式,即将原本再键盘和显示屏输入输出的转移到is 和 ps流中进行输入和输出
(3).System.In其具体实现是通过转换流InputStreamReader为中间方式,在连接到缓冲读入字符流(BufferReader),调用readLine()函数实现
实例:
1 //从键盘输入字符串,将输入的字符串转为大写,如果输入的字符串为e,exit时,退出程序 2 //法一:Scanner实现,调用next()返回字符串 3 //法二:System.in实现,System.in-->转换流-->BufferReader的ReadLine() 4 public static void test1() 5 { 6 BufferedReader br = null; 7 try { 8 //System.in是字节流,通过转换流将其转换为字符缓冲流BufferedReader 9 InputStreamReader isr = new InputStreamReader(System.in); 10 br = new BufferedReader(isr); 11 12 while (true) 13 { 14 //调用readLine()函数读取一行字符串 15 String data = br.readLine(); 16 if (data.equalsIgnoreCase("e") || data.equalsIgnoreCase("exit")) 17 { 18 System.out.println("程序结束"); 19 break; 20 } 21 String upperCase = data.toUpperCase(); 22 System.out.println(upperCase); 23 } 24 } catch (Exception e) { 25 // TODO Auto-generated catch block 26 e.printStackTrace(); 27 } finally { 28 if (br != null) 29 try { 30 br.close(); 31 } catch (IOException e) { 32 // TODO Auto-generated catch block 33 e.printStackTrace(); 34 } 35 } 36 }
4.数据流:(1).DataInputStream和DataOutputStream
(2).作用:用于读取或写出基本数据类型或变量
1 //DataOutputStream(将文本类型和字符串输入到文件中) 2 public void test3() 3 { 4 DataOutputStream dos = null; 5 try { 6 dos = new DataOutputStream(new FileOutputStream("hello.txt")); 7 dos.writeUTF("张三"); 8 dos.flush(); 9 dos.writeInt(20); 10 dos.flush(); 11 dos.writeBoolean(true); 12 } catch (IOException e) { 13 // TODO Auto-generated catch block 14 e.printStackTrace(); 15 } finally { 16 if (dos != null) 17 try { 18 dos.close(); 19 } catch (IOException e) { 20 // TODO Auto-generated catch block 21 e.printStackTrace(); 22 } 23 } 24 }
5.打印流:(1).PrintStream和PrintWriter (2).用于将本来打印在控制台上的内容打印到对应的打印流中的文件去
(3).关键句:System.setOut(ps);将本应在控制台输出的内容转移到ps打印输出流中的文件去
1 public static void test2() 2 { 3 PrintStream ps = null; 4 try { 5 FileOutputStream fos = new FileOutputStream(new File("hello.txt")); 6 ps = new PrintStream(fos); 7 //即将输出的内容从控制台转移到了ps对应的文件中 8 if (ps != null) { 9 System.setOut(ps); 10 } 11 12 for (int i = 0; i < 255; i++) 13 { 14 System.out.print((char)i + " "); 15 if (i % 50 == 0) 16 { 17 System.out.println(); 18 } 19 } 20 } catch (Exception e) { 21 // TODO Auto-generated catch block 22 e.printStackTrace(); 23 } finally { 24 if (ps != null) 25 try { 26 ps.close(); 27 } catch (Exception e) { 28 // TODO Auto-generated catch block 29 e.printStackTrace(); 30 } 31 } 32 }
总结:处理流的总的构建思路:
(1).先建立一个文件:File file = new File("文件路径");
(2).建立一个文件流:FileInputStream fis = new FileInputStream(file);
(3).建立一个处理流:BufferedInputStream bis = new BufferedInputStream(bis);