Java Day 21
复制文本文件
思路:
1、需要读取源
2、将读到的源数据写入目的地
3、使用字符流
方式二
字符流 - 缓冲区
BufferedWriter
newLine();
BufferedReader
readLine();
readLine原理
1 package com.company.Day021; 2 3 import java.io.*; 4 5 /** 6 * Created by junius on 2016/10/18. 7 */ 8 public class CopyOfFile { 9 10 public static final int BUFFER_SIZE = 4096; 11 public static final int BUFF_SIZE = 1024; 12 13 public static void main(String[] args) throws IOException { 14 //demo_1(); 15 //demo_2(); 16 // demo_3(); 17 // demo_4(); 18 // demo_5(); 19 demo_6(); 20 } 21 22 private static void demo_6() throws IOException{ 23 FileReader fr = new FileReader("buf.txt"); 24 MyBufferedReader bufr = new MyBufferedReader(fr); 25 26 FileWriter fw = new FileWriter("buf_copy_4.txt"); 27 BufferedWriter bufw = new BufferedWriter(fw); 28 29 String line =null; 30 while((line=bufr.myReadLine())!=null){ 31 bufw.write(line); 32 bufw.newLine(); 33 bufw.flush(); 34 } 35 bufw.close(); 36 bufr.myClose(); 37 } 38 39 40 private static void demo_5() throws IOException{ 41 FileReader fr = new FileReader("buf.txt"); 42 BufferedReader bufr = new BufferedReader(fr); 43 44 FileWriter fw = new FileWriter("buf_copy_2.txt"); 45 BufferedWriter bufw = new BufferedWriter(fw); 46 47 String line =null; 48 while((line=bufr.readLine())!=null){ 49 bufw.write(line); 50 bufw.newLine(); 51 bufw.flush(); 52 } 53 //方法一: 54 /*int ch =0; 55 while((ch=bufr.read())!=-1){ 56 bufw.write(ch); 57 }*/ 58 bufw.close(); 59 bufr.close(); 60 } 61 62 private static void demo_4() throws IOException { 63 FileReader fr = new FileReader("buf.txt"); 64 BufferedReader bufr = new BufferedReader(fr); 65 String line1=null; 66 while((line1=bufr.readLine())!=null){ 67 System.out.println(line1); 68 } 69 70 bufr.close(); 71 72 //FileReaderDemo(); 73 } 74 75 private static void FileReaderDemo() throws IOException { 76 FileReader fr = new FileReader("buf.txt"); 77 char[] buf = new char[BUFF_SIZE]; 78 79 int len =0; 80 while((len=fr.read(buf))!=-1){ 81 System.out.println(new String(buf,0,len)); 82 } 83 fr.close(); 84 } 85 86 private static void demo_3() throws IOException { 87 FileWriter fw = new FileWriter("buf.txt"); 88 //创建一个字符写入流的缓冲区对象,并和指定要缓冲的流对象相关联 89 BufferedWriter bufw = new BufferedWriter(fw); 90 91 bufw.write("abc"); 92 bufw.newLine(); 93 bufw.write("eeee"); 94 bufw.flush(); 95 //关闭缓存区,就是关闭流 96 bufw.close(); 97 } 98 99 private static void demo_2() { 100 FileReader fr = null; 101 FileWriter fw = null; 102 try { 103 fr = new FileReader("IO_2.txt"); 104 fw = new FileWriter("copytext_2.txt"); 105 //创建临时容器,用于缓存读取到的字符 106 char[] buf = new char[BUFFER_SIZE]; 107 //定义一个变量记录读取到的字符数(其实就是往数组里装的字符个数) 108 int len = 0; 109 while((len=fr.read(buf))!=-1){ 110 fw.write(buf,0,len); 111 } 112 113 }catch (Exception e){ 114 throw new RuntimeException("读写失败"); 115 }finally { 116 if(fw!=null) 117 try { 118 fw.close(); 119 } catch (IOException e) { 120 e.printStackTrace(); 121 } 122 if(fr!=null) 123 try { 124 fr.close(); 125 } catch (IOException e) { 126 e.printStackTrace(); 127 } 128 } 129 } 130 131 private static void demo_1() throws IOException{ 132 //1、使用字符读取流和文件相关联 133 FileReader fr = new FileReader("IO_2.txt"); 134 //2、创建一个目的文本,用于存储读到的数据 135 FileWriter fw = new FileWriter("copytext_1.txt"); 136 //3、频繁操作读写操作 137 int ch = 0; 138 while((ch=fr.read())!=-1){ 139 fw.write(ch); 140 } 141 //4、关闭流资源 142 fw.close(); 143 fr.close(); 144 } 145 }
1 package com.company.Day021; 2 3 import java.io.File; 4 import java.io.FileReader; 5 import java.io.IOException; 6 7 /** 8 * Created by junius on 2016/10/21. 9 * 自定义读取缓冲区 10 * 缓冲区封装一个数组 11 * 12 */ 13 public class MyBufferedReader { 14 public static final int BUFF_SIZE = 1024; 15 private FileReader r; 16 private char[] buf = new char[BUFF_SIZE]; 17 //操作指针,最后一个元素,指针为0 18 private int pos = 0; 19 //计数器,数据减为0,继续读取源 20 private int count = 0; 21 22 MyBufferedReader(FileReader r){ 23 this.r = r; 24 } 25 26 public int myRead()throws IOException{ 27 if(count==0){ 28 count = r.read(buf); 29 pos = 0; 30 } 31 if(count<0) 32 return -1; 33 char ch = buf[pos++]; 34 count--; 35 return ch; 36 } 37 38 public String myReadLine()throws IOException{ 39 StringBuilder sb = new StringBuilder(); 40 int ch = 0; 41 while((ch=myRead())!=-1){ 42 if(ch=='\r') 43 continue; 44 if(ch=='\n') 45 return sb.toString(); 46 sb.append((char)ch); 47 } 48 if(sb.length()!=0) 49 return sb.toString(); 50 return null; 51 } 52 53 public void myClose() throws IOException{ 54 r.close(); 55 } 56 }
装饰设计模式
将对象的功能进行增强
装饰和继承的区别
装饰类和被装饰类都必须所属同一个接口或父类
LineNumberReader
1 package com.company.Day021; 2 3 import java.io.FileReader; 4 import java.io.IOException; 5 import java.io.LineNumberReader; 6 7 /** 8 * Created by junius on 2016/10/23. 9 */ 10 public class Demo { 11 public static void main(String[] args) throws IOException { 12 FileReader fr = new FileReader("buf_copy_4.txt"); 13 LineNumberReader lnr = new LineNumberReader(fr); 14 String line = null; 15 while((line=lnr.readLine())!=null){ 16 System.out.println(lnr.getLineNumber()+":"+line); 17 } 18 lnr.close(); 19 fr.close(); 20 } 21 }
字节流
InputStream
OutputStream
不需要临时存储,直接源码写入目的地中