1.内容:
2.裝飾設計模式,PersonDemo
package java_Studing_day45_IO流5_裝飾設計模式;
public class persondemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
Person p=new Person();
//p.eat();
NewPerson np=new NewPerson(p);
np.eat();
np.runcar();
System.out.println("**********************************************");
NewPerson2 np2=new NewPerson2();
np2.eat();
}
}
class Person{
public void eat(){
System.out.println("吃飯");
}
}
//這個類的出現是為了增強Person這個類而出現的 裝飾設計模式
class NewPerson{
private Person p;
NewPerson(Person p){ //構造函數傳入Person對象進行增強
this.p=p;
}
public void eat(){
System.out.println("開胃酒");
p.eat();
System.out.println("甜點");
}
public void runcar(){
System.out.println("開跑車");
}
}
//繼承設計模式
class NewPerson2 extends Person{
public void eat(){
System.out.println("開胃酒");
super.eat();
System.out.println("甜點");
}
}
3.lineNemberReader的使用
package java_Studing_day45_IO流5_裝飾設計模式;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.LineNumberReader;
public class lineNumberReader {
private static final String Line_Menber = System.getProperty("line.separator");//擷取系統換行符
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
FileWriter fw=new FileWriter("lineNumberReader.txt");
fw.write("sdfsadfeaLine_Men\nbersdgfsdLine_Men\nberrersdfgesdLine_M\nenbernbergrsdfLinen_Menbe\nrerdgsrgLinenMenbe\nrnbersrgsdgLine_Men\nberenbergsewgsdfgs");
fw.flush();
FileReader fr=new FileReader("lineNumberReader.txt");
LineNumberReader lnr=new LineNumberReader(fr);//擷取行号的緩沖
String line=null;
lnr.setLineNumber(100);//設定行号開始編号
while((line=lnr.readLine())!=null){
System.out.println(lnr.getLineNumber()+":"+line);//列印行号和緩存内容
}
lnr.close();
}
}
4.位元組流Demo和常用的緩存方式選擇
package java_Studing_day45_IO流5_位元組流;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class ByteStreamDemo {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
demo_write();
read();
}
//讀
public static void read() throws IOException{
//建立一個讀取流對象,和指定檔案關聯
FileInputStream fis=new FileInputStream("Bytedemo1.txt");
//第4種,擷取剛好大小的數組做容器
System.out.println(fis.available());
byte[] buf=new byte[fis.available()];
fis.read(buf);
System.out.println(new String(buf));
//第3種方式,自定義Byte數組容器當做緩存容器讀取
// byte[] buf=new byte[1024];
// int len=0;
// while((len=fis.read(buf))!=-1){
// System.out.println(new String(buf,0,len));
// }
//第2種方式,系統緩存讀
// int ch=0;
// while((ch=fis.read())!=-1){
// System.out.println((char)ch);
// }
//第1種方式,一次讀取一個位元組
//int ch=fis.read();
//System.out.println(ch);
fis.close();
}
//寫
public static void demo_write() throws IOException {
//1.建立位元組輸出流對象,用于操作檔案
FileOutputStream fos=new FileOutputStream("Bytedemo1.txt");
//2.寫資料,直接寫入到了目的地中
fos.write("abcdefg".getBytes());
//fos.flush();//現在沒什麼用,隻有在緩沖區有用
fos.close();//關閉資源的動作還是要用的
}
}