Java流操作之?dāng)?shù)據(jù)流實(shí)例代碼
實(shí)例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;
//示范如何自鍵盤(pán)讀入字符串,并使用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;
// 鍵盤(pán)讀入
DataInputStream stdin = new DataInputStream(System.in);
//提高執(zhí)行效率,幾乎所有的InputStream類都可以被BufferedStream類包覆(wrap)來(lái)提高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é)束請(qǐng)按【Enter】...\n" + "=>");
//使得流在第一個(gè)位被作上標(biāo)記(mark),并且會(huì)保留256位(mark(256))
bufin.mark(256);
//讀取字節(jié)并存放在指定的數(shù)組中
count = bufin.read(input);
p.println("讀入字符數(shù):" + count);
p.print("你輸入的字符串為:");
// 寫(xiě)入流,只是將數(shù)據(jù)寫(xiě)入流中而已,并不輸出數(shù)據(jù)
// 所以在其后必須使用flush()函數(shù)將流中的數(shù)據(jù)強(qiáng)制輸出
bufout.write(input, 0, count);
bufout.flush();// 強(qiáng)制輸出至指定的輸出裝置
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錯(cuò)誤?。。?);
}
}
}
//其實(shí)我們對(duì)PrintStream類應(yīng)該很熟悉才對(duì),System.out就是一個(gè)PrintStream類對(duì)象,其提供的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é)果:
//輸入的文字~~~~~~~ //輸入的文字:輸入的文字:鍩庡競(jìng)宸ヤ笟 fdsfdssssssssssssssssssssssssssss
總結(jié)
以上就是本文關(guān)于Java流操作之?dāng)?shù)據(jù)流實(shí)例代碼的全部?jī)?nèi)容,希望對(duì)大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站其他相關(guān)專題,如有不足之處,歡迎留言指出。感謝朋友們對(duì)本站的支持!
相關(guān)文章
SpringBoot實(shí)現(xiàn)數(shù)據(jù)加密脫敏的示例代碼
這篇文章主要為大家學(xué)習(xí)介紹了SpringBoot如何利用注解+反射+AOP實(shí)現(xiàn)數(shù)據(jù)加密脫敏的功能,文中的示例代碼講解詳細(xì),需要的可以參考一下2023-08-08
SpringBoot調(diào)用service層的三種方法
在Spring?Boot中,我們可以通過(guò)注入Service層對(duì)象來(lái)調(diào)用Service層的方法,Service層是業(yè)務(wù)邏輯的處理層,它通常包含了對(duì)數(shù)據(jù)的增刪改查操作,本文給大家介紹了SpringBoot調(diào)用service層的三種方法,需要的朋友可以參考下2024-05-05
springBoot定時(shí)任務(wù)處理類的實(shí)現(xiàn)代碼
這篇文章主要介紹了springBoot定時(shí)任務(wù)處理類,需要的朋友可以參考下2018-06-06
java中用數(shù)組實(shí)現(xiàn)環(huán)形隊(duì)列的示例代碼
這篇文章主要介紹了java中用數(shù)組實(shí)現(xiàn)環(huán)形隊(duì)列的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04
mybatis spring配置SqlSessionTemplate的使用方式
這篇文章主要介紹了mybatis spring配置SqlSessionTemplate的使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08
Java設(shè)計(jì)模式以虹貓藍(lán)兔的故事講解橋接模式
橋接是用于把抽象化與實(shí)現(xiàn)化解耦,使二者可以獨(dú)立變化。這種類型的設(shè)計(jì)模式屬于結(jié)構(gòu)型模式,它通過(guò)提供抽象化和實(shí)現(xiàn)化之間的橋接結(jié)構(gòu),來(lái)實(shí)現(xiàn)二者的解耦。這種模式涉及到一個(gè)作為橋接的接口,使得實(shí)體類的功能獨(dú)立于接口實(shí)現(xiàn)類。這兩種類型的類可被結(jié)構(gòu)化改變而互不影響2022-04-04
mybatis in foreach 雙層嵌套問(wèn)題
這篇文章主要介紹了mybatis in foreach 雙層嵌套問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03

