Java基礎複習:IO流(續)

來源:互聯網
上載者:User

記憶體流:

    記憶體輸入資料流:記憶體--->程式   ByteArrayInputputStream、CharArrayReader

    記憶體輸出資料流:程式--->記憶體   ByteArrayOutputStream、CharArrayWriter

    記憶體流可以不用關閉,其讀取操作同前面的檔案流

 

列印流:

    PrintStream   PrintWriter

import java.io.FileNotFoundException;import java.io.PrintStream;public class PrintDemo {public static void main(String[] args) throws FileNotFoundException {PrintStream ps = new PrintStream("d:/Test/well.txt");ps.write('a');ps.print(true);ps.print("hello,world");}}

利用print() 可以輸出任何類型的資料到檔案中。

 

格式化輸出:printf   ,同C語言

public class PrintfDemo {public static void main(String[] args) {String name = "well";int age = 21;char score = 'A';System.out.printf("%s,%d,%c",name,age,score);//well,21,A}}

 

管道流:

又稱線程通訊流,主要用於兩個線程間的通訊。

   PipedOutputStream

   PipedInputStream

 

標準輸入輸出:

     System.in和System.out,預設情況下分別表示鍵盤和螢幕

     可以對其進行重新導向,分別利用setIn 和 setOut

package io;import java.io.FileInputStream;import java.io.IOException;import java.io.PrintStream;public class SystemDemo {public static void main(String[] args) throws IOException {/** * 標準流: * 標準輸入資料流:  System.in   預設表示的是鍵盤錄入 * 標準輸出資料流:  System.out  預設表示的是螢幕輸出 *  *  * 若現在我輸出不想列印在螢幕上,怎麼辦? * 若現在我不想通過鍵盤來錄入資料,有怎麼辦? *  *  * 流的重新導向: * static void setOut(PrintStream out)     重新分配“標準”輸出資料流。  * static void setIn(InputStream in)  重新分配“標準”輸入資料流。  */System.out.println("12123");System.out.println("----end-----");System.err.println("12123");//把螢幕輸出重新導向到setOut.txt,以後的列印就不會列印在螢幕上了,而是列印在setOut.txt檔案裡面System.setOut(new PrintStream("setOut.txt"));System.out.println("AAAA");System.out.println("BBBB");//===================================int  i = System.in.read();//接受從鍵盤輸入的資料System.out.println("鍵盤錄入資料是=  "+(char)i);System.out.println("---------------------------");/** * 重新分配 輸入的源,原本是鍵盤,現在改成 day21複習.txt檔案 */System.setIn(new FileInputStream("day21複習.txt"));byte[] buff = new byte[1024];int len = 0;while((len = System.in.read(buff)) != -1){System.out.println(new String(buff,0,len));}}}

掃描流:

import java.io.File;import java.io.IOException;import java.io.PrintStream;import java.util.Scanner;public class ScannerDemo {public static void main(String[] args) throws IOException {/** * 文本掃描器 */Scanner sc = new Scanner(System.in);//掃描鍵盤錄入的資料/** * Scanner useDelimiter(String pattern)    將此掃描器的分隔模式設定為從指定 String 構造的模式。  *///sc.useDelimiter("\\d");/** * Scanner(File source)  構造一個新的 Scanner,它產生的值是從指定檔案掃描的。 */System.setOut(new PrintStream("line.txt"));sc = new Scanner(new File("day21複習.txt"));int line = 1;while(sc.hasNextLine()){//是否有下一行System.out.println(line +" " + sc.nextLine());//讀取下一行line++;}}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.