天天看點

黑馬程式員——黑馬學習日志之十四 IO流(三)

------- android教育訓練、java教育訓練、期待與您交流! ----------

黑馬學習日志之十四 IO流(三)

1 讀取鍵盤錄入

System.out:對應的是标準的輸出裝置,控制台

System.in:對應的是标準的輸入裝置,鍵盤

例子:

import java.io.*;

class ReadIn

{

 public static void main(String [] args)throws IOException{

   InputStream in = System.in;

   int by = in.read();

   int by1 = in.read();

   int by2 = in.read();

   System.out.println(by);

   System.out.println(by1);

   System.out.println(by2);//輸入a回車符 列印結果 97 13 10 

   //因為\r是13 \n是10

  InputStream in2=System.in;

     int by11=in2.read();

      int by12 =in2.read();

      int by13 =in2.read();

      System.out.println(by11);//輸入abc 列印結果97 隻有一個

      System.out.println(by12);

      System.out.println(by13);//輸入abc 列印結果 97 98 99  

 }

}

例子:

通過鍵盤錄入資料,當錄入一行資料後,就将該行資料進行列印,如果錄入的資料是over,那麼停止錄入。

import  java.io.*;

class ReadIn

{

  public static void main(String[]args) throws IOException{

   InputStream in=System.in;

   StringBuilder sb =new StringBuilder();

   while(true)

   {

    int ch =in.read();

    if(ch=='\r')

      continue;

    if(ch=='\n'){

       Strings=sb.toString();

     if("over".equals(s))

       break;

     System.out.println(s.toUpperCase());

         sb.delete(0,sb.length());

    }

    else{

     sb.append((char)ch);

    }

   }

   in.close();

 }

}

2 轉換流

InputStreamReader 位元組流通向字元流 是Reader的子類

例子:

import java.io.*;

class TransStreamDemo1

{

  public static void main(String [] args)throws IOException{

   //擷取鍵盤錄入對象

   InputStream in = System.in;

   //将位元組流對象轉成字元流對象,使用轉換流。InputStreamReader

   InputStreamReader isr =  new InputStreamReader(in);

   //為了提高效率,将字元串進行緩沖區技術高效操作。使用BufferedReader

   BufferedReader bufr = new BufferedReader(isr);

   String line = null;

   while ((line=bufr.readLine())!=null)

   {

    if("over".equals(line))

      break;

    System.out.println(line.toUpperCase());

   }

   bufr.close();

  }

}

OutputStreamWriter 字元流通向位元組流Writer的子類

例子:

import java.io.*;

class TransStreamDemo

{

 public static void main(String [] args)throws IOException{

  //擷取鍵盤錄入對象

  InputStream in = System.in;

  //将位元組流對象轉成字元流對象,使用轉換流。InputStreamReader

  InputStreamReader isr =  new InputStreamReader(in);

  //為了提高效率,将字元串進行緩沖區技術高效操作。使用BufferedReader

  BufferedReader bufr = new BufferedReader(isr);

  OutputStream out =  System.out;

  OutputStreamWriter osw = new OutputStreamWriter(out);

  BufferedWriter bufw = new BufferedWriter(osw);

  String line = null;

  while ((line=bufr.readLine())!=null)

  {

   if("over".equals(line))

    break;

   //System.out.println(line.toUpperCase());

   bufw.write(line.toUpperCase());

   bufw.newLine();

   bufw.flush();

  }

  bufr.close();//這裡不關閉也是可以的 最終是系統關閉in

 }

}

//三句話變一句 來簡化 鍵盤錄入最常見寫法

BufferedReader bufr = 

 new BufferedReader(new InputStreamReader(System.in));

BufferedWriter bufw = 

 new BufferedWriter(new OutputStreamWriter(System.out));

3 流操作規律

(1) 源 鍵盤錄入

   目的:控制台

(2) 需求:想把鍵盤錄入的資料存儲到一個檔案中

    源  鍵盤錄入

    目的  檔案

(3) 需求:想要将一個檔案的資料列印在控制台上

    源:檔案

    目的: 控制台

流操作的基本規律:

流對象有很多,通過三個明确來完成

(1).明确源和目的

 源:輸入流 InputStream Reader

 目的:輸出流 OutputStream  Writer

(2)操作的資料是否是純文字

 是:字元流

 不是:位元組流

(3)當體系明确後,在明确要使用哪個具體的對象

 通過裝置來進行區分

 源裝置:記憶體,硬碟,鍵盤

 目的裝置:記憶體,硬碟,控制台

4 改變标準輸入輸出裝置

System類中可以改變輸入輸出裝置 

static void setIn(InputStream in) 重新配置設定“标準”輸入流。 

static void setOut(PrintStream out) 重新配置設定“标準”輸出流。 setOut對象是PrintStream

例如

System.setIn(new FileInputStream("CopyText.java"));

System.setOut(new PrintStream("zz.txt"));

例子:

import java.io.*;

class TransStreamDemo

{

 public static void main(String [] args)throws IOException{

  System.setIn(new FileInputStream("CopyText.java"));

  System.setOut(new PrintStream("zz.txt"));

  //擷取鍵盤錄入對象

  InputStream in = System.in;

  //将位元組流對象轉成字元流對象,使用轉換流。InputStreamReader

  InputStreamReader isr =  new InputStreamReader(in);

  //為了提高效率,将字元串進行緩沖區技術高效操作。使用BufferedReader

  BufferedReader bufr = new BufferedReader(isr);

  OutputStream out = System.out;

  OutputStreamWriter osw = new OutputStreamWriter(out);

  BufferedWriter bufw = new BufferedWriter(osw);

  String line = null;

  while ((line=bufr.readLine())!=null)

  {

   if("over".equals(line))

    break;

   //System.out.println(line.toUpperCase());

   bufw.write(line.toUpperCase());

  bufw.newLine();

   bufw.flush();

  }

  bufr.close();

 }

}

5 IO異常資訊

這裡的異常日志資訊是和IO流有關聯的

Throwable中的printStackTrace有重載方法 printStackTrace(PrintStream s) 

例子:

import java.io.*;

import java.util.*;

import java.text.*;

class ExceptionInfo

{

 public static void main(String [] args)throws IOException{

  try

  {

   int [] arr=new int[2];

   System.out.println(arr[3]);

  }

  catch (Exception e)

  {

   try

   {

    Date d = new Date();

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");//将hh改為HH     String s= sdf.format(d);

    PrintStream ps = new PrintStream("exception.log");

    ps.println(s);

    System.setOut(ps);

   }

   catch (IOException ex)

   {

    throw new RuntimeException("日志檔案建立失敗");

   }

   e.printStackTrace(System.out);

  }

 }

}

6 系統資訊

結合流列印

import java.util.*;

import java.io.*;

class SystemInfo

{

 public static void main(String [] args)throws IOException{

  Properties prop = System.getPropreties();

  //System.out.println(prop);//當時用的是周遊集合的方式 是個map集合 map集合在記憶體中

  prop.list(new PrintStream("sysinfo.txt"));

 }

}

------- android教育訓練、java教育訓練、期待與您交流! ----------