自定义bufferReader

来源:互联网 发布:net use找不到网络路径 编辑:程序博客网 时间:2024/04/20 07:48

public class MyBufferReader {
private Reader reader;
public MyBufferReader(Reader reader) {
this.reader = reader;
}
public Reader getReader() {
return reader;
}
public void setReader(Reader reader) {
this.reader = reader;
}
// 自定义readLine方法
public String myReadLine() throws IOException {
StringBuffer sb = new StringBuffer();
int len = reader.read();
//System.out.println(len);
while ((len=reader.read())!=-1) {
System.out.println(len);
if (len == 13) {
//13代表’\r表示回车’
continue;
}
if (len ==10) {
//10代表’\n’表示换行
return sb.toString();
} else {
sb.append((char) len);
}
}
if (sb.length() > 0) {
return sb.toString();
}
return null;
}
public void close() throws IOException{
reader.close();
}
public static void main(String[] args) {
MyBufferReader mr = null;
try {
Reader r = new FileReader(“d:\word.txt”);
mr = new MyBufferReader(r);
String str = mr.myReadLine();
while(str!=null){
System.out.println(str);
str = mr.myReadLine();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
mr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

package com.phone.week4.day4;

public class Person {

public void xiangQin(){    System.out.println("相亲");}public static void main(String[] args) {    SuperPerson sp = new SuperPerson(new Person());    sp.superXiangQin();}

}

class SuperPerson{
private Person person;

public SuperPerson(Person person){    this.person = person;}public void superXiangQin(){    System.out.println("给媒婆说相亲对象的要求,身高180,体重180,五官清秀,有钱,有房,有车");    System.out.println("过了几天,媒婆给你找到了合适的对象");    person.xiangQin();    System.out.println("比较满意");    System.out.println("定婚");    System.out.println("结婚");}

}

package com.phone.week4.day4;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.io.PrintWriter;

public class Test {

public static void main(String[] args) throws IOException {    /*InputStream is = System.in;    InputStreamReader isr = new InputStreamReader(is,"gb2312");    BufferedReader br = new BufferedReader(isr);    System.out.println("请输入字符");    String len =br.readLine();    System.out.println(len);*/    PrintStream pw = System.out;//new PrintStream("d:\\test.txt");    System.setOut(pw);    pw.print(97);    pw.write(97);}

}

0 0