由于字数限制,只能分上下了
6、利用缓冲区完成文本文件拷贝功能
- package com.itheima.io;
- import java.io.BufferedReader;
- import java.io.BufferedWriter;
- import java.io.FileReader;
- import java.io.FileWriter;
- import java.io.IOException;
- public class CopyTextByBuf_2_3 {
- /**
- * 利用缓冲区复制文本文件
- */
- public static void main(String[] args) {
- BufferedReader br = null;
- BufferedWriter bw = null;
- FileReader fr = null;
- FileWriter fw = null;
- try {
- fr = new FileReader(".\\testData\\buf.txt");
- br = new BufferedReader(fr);
- fw = new FileWriter(".\\testData\\buf_copy.txt");
- bw = new BufferedWriter(fw);
- String line = "";
- while ((line = br.readLine()) != null) {
- bw.write(line);
- //readline()不读取换行符
- bw.newLine();
- //bw.flush();
- }
- } catch (IOException e) {
- throw new RuntimeException("读写失败!");
- } finally {
- try {
- if (bw != null)
- bw.close();
- if (br != null)
- br.close();
- } catch (IOException e) {
- throw new RuntimeException("关闭失败!");
- }
- }
- }
- }
复制代码
7、了解原理,编写自己的缓冲区方法
- package com.itheima.io;
- import java.io.FileReader;
- import java.io.IOException;
- /**
- * 装饰者设计模式:当想要对已有的对象进行功能增强时,可以定义 类,将已有对象传入,基于已有功能,并提供加强功能
- *
- * @author StephenHu
- *
- */
- public class MyBufferedReader_2_4 {
- public static void main(String[] args) {
- FileReader fr = null;
- MyBufferedReader mbr = null;
- try {
- fr = new FileReader(".\\testData\\buf.txt");
- mbr = new MyBufferedReader(fr);
- String line = "";
- while ((line = mbr.myReadLine()) != null) {
- System.out.println(line);
- }
- } catch (IOException e) {
- throw new RuntimeException("读写失败!");
- } finally {
- try {
- if (mbr != null)
- mbr.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- }
- class MyBufferedReader {
- /**
- * 自己写一个类似BufferedReader功能的方法
- */
- private FileReader fr;
- MyBufferedReader(FileReader fr) {
- this.fr = fr;
- }
- // 类似readline()方法
- public String myReadLine() throws IOException {
- StringBuilder sb = new StringBuilder();
- int ch = 0;
- while ((ch = fr.read()) != -1) {
- if (ch == '\r') {
- continue;
- } else if (ch == '\n') {
- return sb.toString();
- } else {
- sb.append((char) ch);
- }
- }
- if (sb.length() != 0)
- return sb.toString();
- return null;
- }
- public void close() throws IOException {
- fr.close();
- }
- }
复制代码
8、从自己编写的缓冲区方法浅谈装饰者模式,以及他和继承的区别
- package com.itheima.io;
- import java.io.FileReader;
- import java.io.IOException;
- import java.io.Reader;
- /*class MyLineNumberReader {
- private Reader r;
- private int lineNumber;
- public int getLineNumber() {
- return lineNumber;
- }
- public void setLineNumber(int lineNumber) {
- this.lineNumber = lineNumber;
- }
- public MyLineNumberReader(Reader r) {
- this.r = r;
- }
- public String myReaderLine() throws IOException {
- lineNumber++;
- StringBuilder sb = new StringBuilder();
- int ch = 0;
- while ((ch = r.read()) != -1) {
- if (ch == '\r')
- continue;
- else if (ch == '\n') {
- return sb.toString();
- } else {
- sb.append((char) ch);
- }
- }
- if (sb.length() != 0) {
- return sb.toString();
- }
- return null;
- }
- public void myClose() throws IOException {
- r.close();
- }
- }*/
- //利用继承来简化
- class MyLineNumberReader extends MyBufferedReader{
- private int lineNumber;
- public int getLineNumber() {
- return lineNumber;
- }
- public void setLineNumber(int lineNumber) {
- this.lineNumber = lineNumber;
- }
- public MyLineNumberReader(Reader r) {
- super(r);
- }
- public String myReaderLine() throws IOException {
- lineNumber++;
- return super.myReadLine();
- }
- }
- public class MyLineNumberReaderDemo_3_2 {
- public static void main(String[] args) {
- FileReader fr = null;
- MyLineNumberReader mln = null;
- try {
- fr = new FileReader(".\\testData\\buf.txt");
- mln = new MyLineNumberReader(fr);
- String line = "";
- mln.setLineNumber(100);
- while ((line = mln.myReaderLine()) != null) {
- System.out.println(mln.getLineNumber()+">>"+line);
- }
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
复制代码
9、编写自己的LineNumberReader,并用继承来简化
- package com.itheima.io;
- import java.io.FileReader;
- import java.io.IOException;
- import java.io.Reader;
- /*class MyLineNumberReader {
- private Reader r;
- private int lineNumber;
- public int getLineNumber() {
- return lineNumber;
- }
- public void setLineNumber(int lineNumber) {
- this.lineNumber = lineNumber;
- }
- public MyLineNumberReader(Reader r) {
- this.r = r;
- }
- public String myReaderLine() throws IOException {
- lineNumber++;
- StringBuilder sb = new StringBuilder();
- int ch = 0;
- while ((ch = r.read()) != -1) {
- if (ch == '\r')
- continue;
- else if (ch == '\n') {
- return sb.toString();
- } else {
- sb.append((char) ch);
- }
- }
- if (sb.length() != 0) {
- return sb.toString();
- }
- return null;
- }
- public void myClose() throws IOException {
- r.close();
- }
- }*/
- //利用继承来简化
- class MyLineNumberReader extends MyBufferedReader{
- private int lineNumber;
- public int getLineNumber() {
- return lineNumber;
- }
- public void setLineNumber(int lineNumber) {
- this.lineNumber = lineNumber;
- }
- public MyLineNumberReader(Reader r) {
- super(r);
- }
- public String myReaderLine() throws IOException {
- lineNumber++;
- return super.myReadLine();
- }
- }
- public class MyLineNumberReaderDemo_3_2 {
- public static void main(String[] args) {
- FileReader fr = null;
- MyLineNumberReader mln = null;
- try {
- fr = new FileReader(".\\testData\\buf.txt");
- mln = new MyLineNumberReader(fr);
- String line = "";
- mln.setLineNumber(100);
- while ((line = mln.myReaderLine()) != null) {
- System.out.println(mln.getLineNumber()+">>"+line);
- }
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
复制代码
|