package IODemo;/*** @author Alina* @date 2021年11月15日 11:48 下午* 设计思想:设计模式,装饰模式* JAVA中有23种设计思想,全部基于面向对象* 装饰设计模式,核心思想,解决什么问题* 增强原有对象的功能**/
//第一代人类只有吃饭的功能
class Person{public void eat(){System.out.println("吃饱饭");}
}
//第二代人类有了新的吃法
class newPerson{private Person per;//提供构造函数public newPerson(Person pe) {this.per = pe;}public void neweat(){System.out.println("喝奶茶");per.eat();System.out.println("吃巧克力");}
}public class buffer {public static void main(String[] args) {Person p = new Person();newPerson n = new newPerson(p);n.neweat();}
package IODemo;import java.io.*;/*** @author Alina* @date 2021年11月16日 11:35 下午* 实现读取文本一行* 读取一行返回字符串* 文件默认读到行位返回-1**/
class myread{private Reader reader;public myread(Reader reader) {this.reader = reader;}public String readLine()throws IOException {//创建字符串缓冲区对象,保留读取到的有效字符StringBuilder builder = new StringBuilder();int len = 0 ;//利用Reader类的方法read读取文件,read 文件结尾返回-1while ((len =reader.read())!= -1){//判断读取到的是不是\rif (len == '\r'){ continue;}//判断读取到的是否是\nif(len == '\n'){return builder.toString();}//如果读取的不是\r\n 视为有效字符,存放到缓冲区中builder.append((char) len);}//判断缓冲区是否有字符,如果有返回去if(builder.length()!=0){return builder.toString();}//文件读取完毕return null;}//定义关闭资源方法public void close()throws IOException{reader.close();}
}
public class myReadLine {public static void main(String[] args)throws IOException {myread my = new myread(new FileReader("/src/IODemo/test.txt"));String line = null;while ((line = my.readLine())!=null){System.out.println(line);}// BufferedReader br = new BufferedReader(new FileReader("/src/IODemo/test.txt"));}}