Java的DataInputStream和DataOutputStream怎么使用_第1頁
Java的DataInputStream和DataOutputStream怎么使用_第2頁
Java的DataInputStream和DataOutputStream怎么使用_第3頁
Java的DataInputStream和DataOutputStream怎么使用_第4頁
Java的DataInputStream和DataOutputStream怎么使用_第5頁
已閱讀5頁,還剩4頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)

文檔簡介

第Java的DataInputStream和DataOutputStream怎么使用在io包中,提供了兩個與平臺無關(guān)的數(shù)據(jù)操作流:數(shù)據(jù)輸出流(DataOutputStream)、數(shù)據(jù)輸入流(DatAInputStream)。

通常數(shù)據(jù)輸出流會按照一定的格式將數(shù)據(jù)輸出,再通過數(shù)據(jù)輸入流按照一定的格式將數(shù)據(jù)讀入。DataOutputStream和DataOutputStream用來讀寫固定字節(jié)格式數(shù)據(jù)。

DataOutputStream

創(chuàng)建對象

DataOutputStreamout=newDataOutputStream(相接的流)

方法將一個int類型的數(shù)據(jù)寫到數(shù)據(jù)輸出流中,底層將4個字節(jié)寫到基礎(chǔ)輸出流中

writeInt(inti)

將一個double類型的數(shù)據(jù)寫到數(shù)據(jù)輸出流中,底層會將double轉(zhuǎn)換成long類型,寫到基礎(chǔ)輸出流中,輸出8個字節(jié)

writeDouble(doubled)

以機(jī)器無關(guān)的方式使用utf-8編碼方式將字符串寫到基礎(chǔ)輸出流中。先輸出2個字節(jié)表示字符串的字節(jié)長度,再輸出這些字節(jié)值

writeUTF()

DataInputStream

創(chuàng)建對象

DataInputStreamdis=newDataInputStream(InputStreamin);

方法從數(shù)據(jù)輸入流中讀取一個int類型數(shù)據(jù),讀取4個字節(jié)

readInt()

讀取8個字節(jié)

readDouble()

先讀取2個字節(jié)來確定字符串的字節(jié)長度,再讀取這些字節(jié)值

readUTF()

Tips:讀取結(jié)束,再讀取會出現(xiàn)EOFException

栗子1:寫入數(shù)據(jù)

publicclassMain{

publicstaticvoidmain(String[]args)throwsException{

DataOutputStreamout=newDataOutputStream(newFileOutputStream(d:/abc/f5));

out.writeInt(20251011);

out.writeUTF(晴,18度

out.writeInt(20251012);

out.writeUTF(晴,19度

out.writeInt(20251013);

out.writeUTF(多云,17度

out.close();

}

運行結(jié)果:

栗子2:讀取

publicclassMain{

publicstaticvoidmain(String[]args)throwsException{

DataInputStreamin=newDataInputStream(newFileInputStream(d:/abc/f5));

try{

while(true){

intdate=in.readInt();

Strings=in.readUTF();

System.out.println(date);

System.out.println(s);

}catch(EOFExceptione){

//正確讀取結(jié)束,不需要處理

in.close();

}

運行結(jié)果:

栗子3:保存學(xué)生信息

要求用如下格式保存學(xué)生信息

學(xué)號00000001

姓名0003616263

性別0061

年齡00000016

xml

LinearLayoutxmlns:Android=/apk/res/android

android:layout_width=match_parent

android:layout_height=match_parent

android:orientation=vertical

android:padding=20dp

EditText

android:id=@+id/et1

android:layout_width=match_parent

android:layout_height=wrap_content

android:hint=學(xué)號/

EditText

android:id=@+id/et2

android:layout_width=match_parent

android:layout_height=wrap_content

android:hint=姓名/

EditText

android:id=@+id/et3

android:layout_width=match_parent

android:layout_height=wrap_content

android:hint=性別/

EditText

android:id=@+id/et4

android:layout_width=match_parent

android:layout_height=wrap_content

android:hint=年齡/

Button

android:id=@+id/btn1

android:layout_width=wrap_content

android:layout_height=wrap_content

android:text=保存/

Button

android:id=@+id/btn2

android:layout_width=wrap_content

android:layout_height=wrap_content

android:text=讀取/

TextView

android:id=@+id/tv

android:layout_width=match_parent

android:layout_height=wrap_content

android:layout_marginTop=10dp/

/LinearLayout

java

publicclassIoActivityextendsAppCompatActivity{

privateEditTextet1;

privateEditTextet2;

privateEditTextet3;

privateEditTextet4;

privateButtonbtn1;

privateButtonbtn2;

privateTextViewtv;

@Override

protectedvoidonCreate(BundlesavedInstanceState){

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_io);

setViews();

setListeners();

privatevoidsetViews(){

et1=findViewById(R.id.et1);

et2=findViewById(R.id.et2);

et3=findViewById(R.id.et3);

et4=findViewById(R.id.et4);

btn1=findViewById(R.id.btn1);

btn2=findViewById(R.id.btn2);

tv=findViewById(R.id.tv);

privatevoidsetListeners(){

btn1.setOnClickListener(view-baocun());

btn2.setOnClickListener(view-duqu());

privatevoidbaocun(){

//IO操作有IO異常,所以進(jìn)行try...catch...

*┌DataOutputStream

*┌FileOutputStream

*sdcard

try{

intid=Integer.parseInt(et1.getText().toString());

Stringname=et2.getText().toString();

Stringgender=et3.getText().toString();

intage=Integer.parseInt(et4.getText().toString());

DataOutputStreamout=newDataOutputStream(

newFileOutputStream(getExternalFilesDir(null)+/stu.txt,true)

out.writeInt(id);

out.writeUTF(name);

out.writeChar(gender.charAt(0));

out.writeInt(age);

out.close();

Toast.makeText(this,保存成功,Toast.LENGTH_SHORT).show();

}catch(Exceptione){

Toast.makeText(this,保存失敗,Toast.LENGTH_SHORT).show();

e.printStackTrace();

privatevoidduqu(){

//IO操作有IO異常,所以進(jìn)行try...catch...

try{

DataInputStreamin=newDataInputStream(

newFileInputStream(getExternalFilesDir(null)+/stu.txt)

try{

tv.setText(

while(true){

intid=in.readInt();

Stringname=in.readUTF();

chargender=in.readChar();

intage=in.readInt();

tv.append(id:+id+\n+name:+name+\n+gender:+gender+\n+age:+age+\n

}catch(EOFExceptione){

in.close();

Toast.makeText(this,讀取成功,Toast.LENGTH_SHORT).show();

}catch(Exceptione){

Toast.makeText(this,讀取失敗,Toast

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論