Java流操作之?dāng)?shù)據(jù)流的示例分析-創(chuàng)新互聯(lián)

這篇文章主要介紹Java流操作之?dāng)?shù)據(jù)流的示例分析,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!

創(chuàng)新互聯(lián)公司專注骨干網(wǎng)絡(luò)服務(wù)器租用十多年,服務(wù)更有保障!服務(wù)器租用,成都服務(wù)器托管 成都服務(wù)器租用,成都服務(wù)器托管,骨干網(wǎng)絡(luò)帶寬,享受低延遲,高速訪問。靈活、實現(xiàn)低成本的共享或公網(wǎng)數(shù)據(jù)中心高速帶寬的專屬高性能服務(wù)器。

 實例1:

package dataInputStreamAndPrintStreamDemo; 
import java.io.BufferedInputStream; 
import java.io.BufferedOutputStream; 
import java.io.DataInputStream; 
import java.io.DataOutputStream; 
import java.io.IOException; 
import java.io.PrintStream; 
//示范如何自鍵盤讀入字符串,并使用DataInputStream,PrintStream類將程序執(zhí)行顯示在屏幕(標(biāo)準(zhǔn)輸出)上 
public class DataInputStreamAndPrintStreamDemo { 
 public static void main(String[] args) { 
 int count; 
 byte input[] = new byte[256]; 
 String InputString; 
 // 鍵盤讀入 
 DataInputStream stdin = new DataInputStream(System.in); 
 //提高執(zhí)行效率,幾乎所有的InputStream類都可以被BufferedStream類包覆(wrap)來提高I/O效率 
 BufferedInputStream bufin = new BufferedInputStream(stdin); 
 // 屏幕輸出 
 DataOutputStream stdout = new DataOutputStream(System.out);// 將結(jié)果輸出至屏幕 
 BufferedOutputStream bufout = new BufferedOutputStream(stdout);// 提高輸出效率 
 PrintStream p = new PrintStream(System.out);// 將結(jié)果輸出至屏幕 
 try { 
 if (bufin.markSupported()) { 
 p.println("支持串流標(biāo)記:是");// 使用PrintStream輸出 
 p.println("輸入字符串,結(jié)束請按【Enter】...\n" + "=>"); 
 //使得流在第一個位被作上標(biāo)記(mark),并且會保留256位(mark(256)) 
 bufin.mark(256); 
 //讀取字節(jié)并存放在指定的數(shù)組中 
 count = bufin.read(input); 
 p.println("讀入字符數(shù):" + count); 
 p.print("你輸入的字符串為:"); 
 // 寫入流,只是將數(shù)據(jù)寫入流中而已,并不輸出數(shù)據(jù) 
 // 所以在其后必須使用flush()函數(shù)將流中的數(shù)據(jù)強制輸出 
 bufout.write(input, 0, count); 
 bufout.flush();// 強制輸出至指定的輸出裝置 
 bufin.reset();// 將讀取位置移至標(biāo)記處,也就是流中的第一位 
 bufin.read(input, 0, count); 
 p.print("字符串的前半段:"); 
 bufout.write(input, 0, count / 2); 
 //相當(dāng)于System.out.println(); 
 bufout.write((int)('\n')); 
 bufout.flush(); 
 bufin.reset(); 
 bufin.skip(count / 2); 
 bufin.read(input, 0, count / 2); 
 p.print("字符串的后半段:"); 
 bufout.write(input, 0, count / 2); 
 bufout.flush(); 
 } else { 
 System.out.println("字符串流標(biāo)記:否"); 
 } 
 // 關(guān)閉流 
 p.close(); 
 stdin.close(); 
 bufin.close(); 
 stdout.close(); 
 bufout.close(); 
 } catch (IOException E) { 
 System.out.println("發(fā)生I/O錯誤?。。?quot;); 
 } 
 } 
} 
//其實我們對PrintStream類應(yīng)該很熟悉才對,System.out就是一個PrintStream類對象,其提供的print()和println()函數(shù) 
//幾乎可顯示所有數(shù)據(jù)類型的變量
//例程2:package iotest; 
 
import java.io.FileNotFoundException; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.PrintStream; 
import java.util.logging.Level; 
import java.util.logging.Logger; 
 
 
public class IOtest { 
 
 public static void main(String[] args) throws IOException { 
   
  byte buf[] = new byte[255]; 
  byte bufin[] = new byte[255];    //只能用byte格式將數(shù)據(jù)送入文件 
  String str = "輸入的文字:"; 
  buf = str.getBytes(); 
  try { 
   FileOutputStream fout = new FileOutputStream("test.txt"); 
   PrintStream p = new PrintStream(fout); 
   p.println("輸入的文字~~~~~~~"+'\n');  //方式一 
    fout.write(buf, 0, buf.length); //方式二 
    fout.write(buf);     //方式三 
    //fout.flush(); 
    //fout.close(); 
    System.out.println("快輸入文字:"); 
    int bytes = System.in.read(bufin, 0, 255); 
        
    //追加文本!!!!!!!!!!!!!!!! 
    //fout = new FileOutputStream("test.txt",true); 
    fout.write(bufin, 0, bytes); 
  } catch (FileNotFoundException ex) { 
   Logger.getLogger(IOtest.class.getName()).log(Level.SEVERE, null, ex); 
  } 
   
 } 
  
}

結(jié)果:

//輸入的文字~~~~~~~ 
 
 
//輸入的文字:輸入的文字:鍩庡競宸ヤ笟 fdsfdssssssssssssssssssssssssssss

以上是“Java流操作之?dāng)?shù)據(jù)流的示例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!

網(wǎng)頁名稱:Java流操作之?dāng)?shù)據(jù)流的示例分析-創(chuàng)新互聯(lián)
瀏覽地址:http://bm7419.com/article4/gioie.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站策劃、App設(shè)計、關(guān)鍵詞優(yōu)化、做網(wǎng)站網(wǎng)站導(dǎo)航、網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站制作