java第二階段2day


關於IO流的經常使用類java

           一、InputStream is = System.in;
              OutputStream os = System.out;數組

           二、InputStreamReader    --》字節流轉換爲字符流
              OutputStreamWriter  --》字符流轉換爲字節流app


           三、PrintStream ps = System.out
            ide

           四、ByteArrayOutputStream  --》 一次把字節寫完存入內存
              ByteArrayInputStream   --》一次把字節讀完
 函數

----------------------------
 //接收鍵盤輸入的字符 但不能使用Scanner   ui

public class SystemIn {this

 public static void main(String[] args) {內存

          //用InputStream 來讀取
  InputStream is = System.in;
  OutputStream os = null;
  int n = 0;
  try {
   os = new FileOutputStream("d:\\aa.txt");
   while((n = is.read()) != 't'){
    os.write(n);
    
   }
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }finally{
   try {
    is.close();
    os.close();
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  
  }
  
 }get

}
 ---------------------
//接收鍵盤輸入的字符 但不能使用Scanner
    it

public class System2 {

 public static void main(String[] args) {
  InputStream  is = System.in;
  //字節流轉換成爲字符流
  InputStreamReader ir = new InputStreamReader(is);
  //緩衝
  BufferedReader br = new BufferedReader(ir);
  
  Writer w = null;
  String str;
   try {
    w = new FileWriter("d:\\dd.txt");
    while(true){
    str = br.readLine();
    if("886".equals(str)){break;}
    w.write(str);
    w.flush();
    }
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }finally{
    try {
     br.close();
     w.close();
    } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
   }
   
  

 }

}
-----------------------------
//把某個文件的內容讀取 而後打印到控制檯 可是不能用println或print

public class PrintStream1 {

 public static void main(String[] args) {
  // TODO Auto-generated method stub

  PrintStream ps = System.out;
  //字符流轉換成爲字節流
  OutputStreamWriter ow = new OutputStreamWriter(ps);
  
  BufferedWriter bw = new BufferedWriter(ow);
  int  s = 0;
  Reader r;
  try {
   r = new FileReader("d:\\dd.txt");
   while((s = r.read())!= -1 ){
    bw.write(s);
    bw.flush();
   }
  } catch (FileNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }finally{
   try {
    bw.close();
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  }
  
 }

}
--------------------------------
ByteArrayOutputStream --> 把東西一次寫入內存


 public class ByteArrayOutputStream1 {

 public static void main(String[] args) {
  // TODO Auto-generated method stub

  InputStream is = null;
  ByteArrayOutputStream bos = null;
  OutputStream os = null;
  byte[] b = new byte[1024];
  int l = 0;
  try {
   bos = new ByteArrayOutputStream();
   is = new FileInputStream("d:\\a.txt");
   os = new FileOutputStream("d:\\dd.txt");
   while((l = is.read(b)) != -1){
    //寫入內存
    bos.write(b,0,l);
    //從內存取數據 存到數組
    byte[] byte1 = bos.toByteArray();
  
    os.write(byte1);
    //bos.close(); 關閉無效
   }
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
    
 }

}
-----------------------------------
重寫LineNumberReader 函數
1
public class RewriteLineNumberRead extends BufferedReader {
 

 public RewriteLineNumberRead(Reader r) {
  super(r);
  // TODO Auto-generated constructor stub
 }
 public RewriteLineNumberRead(Reader in, int sz) {
  super(in, sz);
  // TODO Auto-generated constructor stub
 }

 private int number = 0;  //行號
 public int getNumber(){
  return number;
 }
 @Override
 public String readLine() throws IOException {
  // TODO Auto-generated method stub
  number++;
  return super.readLine();
 }
 


}
----------
2

import java.io.*;
public class Test1 {
//調用本身重寫的LineNumberReader函數 打印出行數
 public static void main(String[] args) {
 // PrintStream ps = System.out;
  //字符流轉換成字節流
 // OutputStreamWriter osw = new OutputStreamWriter(ps);
  //緩衝
 // BufferedWriter bw = new BufferedWriter(osw);
  Reader r = null;
  RewriteLineNumberRead re = null;
  String str;
  try {
    r = new FileReader("d:\\dd.txt");
    re = new RewriteLineNumberRead(r);
   while((str = re.readLine()) != null){
    //bw.write(str);
    System.out.println(re.getNumber() + "  " + str);
   }
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }finally{
   try {
    re.close();
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  }
 }

}
------------------------------------------------
重寫Buffered 函數

1
public class RewriterBuffered extends Reader {
 public RewriterBuffered(Reader in) {
 // super(in);
  this.reader = in;
  // TODO Auto-generated constructor stub
 }


 private Reader reader;

 
 public String readLine(){
  StringBuilder sb = new StringBuilder();
  while(true){
   try {
   int ch = reader.read();
    if(ch == '\r'){
     continue;
     }else if(ch == '\n' || ch == -1){
     break;
    }
    sb.append((char)ch);
    
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  }
  return sb.toString();
 }


 @Override
 public int read(char[] cbuf, int off, int len) throws IOException {
  // TODO Auto-generated method stub
  return 0;
 }


 @Override
 public void close() throws IOException {
  // TODO Auto-generated method stub
  
 }

}
----------------
2

public class TestBuff {

 public static void main(String[] args) throws FileNotFoundException {
  Reader r = new FileReader("d:\\a.txt");
  RewriterBuffered rb = new RewriterBuffered(r);
  //rb.readLine();
  System.out.println(rb.readLine());

 }

}

相關文章
相關標籤/搜索