Java中DataInputStream和DataOutputStream的使用方法
簡(jiǎn)介
在 io 包中,提供了兩個(gè)與平臺(tái)無(wú)關(guān)的數(shù)據(jù)操作流:數(shù)據(jù)輸出流(DataOutputStream)、數(shù)據(jù)輸入流 (DataInputStream)。
通常數(shù)據(jù)輸出流會(huì)按照一定的格式將數(shù)據(jù)輸出,再通過數(shù)據(jù)輸入流按照一定的格式將數(shù)據(jù)讀入。DataOutputStream 和 DataOutputStream 用來(lái)讀寫固定字節(jié)格式數(shù)據(jù)。
DataOutputStream
創(chuàng)建對(duì)象
DataOutputStream out = new DataOutputStream(相接的流)
方法將一個(gè) int 類型的數(shù)據(jù)寫到數(shù)據(jù)輸出流中,底層將 4 個(gè)字節(jié)寫到基礎(chǔ)輸出流中
writeInt(int i)
將一個(gè) double 類型的數(shù)據(jù)寫到數(shù)據(jù)輸出流中,底層會(huì)將 double 轉(zhuǎn)換成 long 類型,寫到基礎(chǔ)輸出流中,輸出8個(gè)字節(jié)
writeDouble(double d)
以機(jī)器無(wú)關(guān)的方式使用 utf-8 編碼方式將字符串寫到基礎(chǔ)輸出流中。先輸出 2 個(gè)字節(jié)表示字符串的字節(jié)長(zhǎng)度,再輸出這些字節(jié)值
writeUTF()
DataInputStream
創(chuàng)建對(duì)象
DataInputStream dis = new DataInputStream(InputStream in);
方法從數(shù)據(jù)輸入流中讀取一個(gè) int 類型數(shù)據(jù),讀取 4 個(gè)字節(jié)
readInt()
讀取8個(gè)字節(jié)
readDouble()
先讀取 2 個(gè)字節(jié)來(lái)確定字符串的字節(jié)長(zhǎng)度,再讀取這些字節(jié)值
readUTF()
Tips:讀取結(jié)束,再讀取會(huì)出現(xiàn)EOFException
栗子1:寫入數(shù)據(jù)
public class Main {
public static void main(String[] args) throws Exception {
DataOutputStream out = new DataOutputStream(new FileOutputStream("d:/abc/f5"));
out.writeInt(20211011);
out.writeUTF("晴,18度");
out.writeInt(20211012);
out.writeUTF("晴,19度");
out.writeInt(20211013);
out.writeUTF("多云,17度");
out.close();
}
}運(yùn)行結(jié)果:

栗子2:讀取
public class Main {
public static void main(String[] args) throws Exception {
DataInputStream in = new DataInputStream(new FileInputStream("d:/abc/f5"));
try {
while (true) {
int date = in.readInt();
String s = in.readUTF();
System.out.println(date);
System.out.println(s);
}
} catch (EOFException e) {
//正確讀取結(jié)束,不需要處理
}
in.close();
}
}運(yùn)行結(jié)果:

栗子3:保存學(xué)生信息
要求用如下格式保存學(xué)生信息:
學(xué)號(hào) 00 00 00 01
姓名 00 03 61 62 63
性別 00 61
年齡 00 00 00 16
xml
<LinearLayout xmlns:android="http://schemas.android.com/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é)號(hào)" />
<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
public class IoActivity extends AppCompatActivity {
private EditText et1;
private EditText et2;
private EditText et3;
private EditText et4;
private Button btn1;
private Button btn2;
private TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_io);
setViews();
setListeners();
}
private void setViews() {
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);
}
private void setListeners() {
btn1.setOnClickListener(view -> baocun());
btn2.setOnClickListener(view -> duqu());
}
private void baocun() {
//IO操作有IO異常,所以進(jìn)行try...catch...
/*
*
* ┌DataOutputStream
* ┌FileOutputStream
* sdcard
*/
try {
int id = Integer.parseInt(et1.getText().toString());
String name = et2.getText().toString();
String gender = et3.getText().toString();
int age = Integer.parseInt(et4.getText().toString());
DataOutputStream out = new DataOutputStream(
new FileOutputStream(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 (Exception e) {
Toast.makeText(this, "保存失敗", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
private void duqu() {
//IO操作有IO異常,所以進(jìn)行try...catch...
try {
DataInputStream in = new DataInputStream(
new FileInputStream(getExternalFilesDir(null) + "/stu.txt")
);
try {
tv.setText("");
while (true) {
int id = in.readInt();
String name = in.readUTF();
char gender = in.readChar();
int age = in.readInt();
tv.append("id:" + id + "\n" + "name:" + name + "\n" + "gender:" + gender + "\n" + "age:" + age + "\n");
}
} catch (EOFException e) {
}
in.close();
Toast.makeText(this, "讀取成功", Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Toast.makeText(this, "讀取失敗", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
}運(yùn)行程序:

點(diǎn)擊讀取按鈕:

其中getExternalFilesDir(null)得到以下路徑
/storage/emulated/0/Android/data/yourPackageName/files
這個(gè)目錄會(huì)在應(yīng)用被卸載的時(shí)候刪除,而且訪問這個(gè)目錄不需要?jiǎng)討B(tài)申請(qǐng)STORAGE 權(quán)限。
所以運(yùn)行程序會(huì)在這個(gè)路徑下生成一個(gè) stu.txt 的文件
到此這篇關(guān)于Java中DataInputStream和DataOutputStream的使用方法的文章就介紹到這了,更多相關(guān)DataInputStream和DataOutputStream內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java 單鏈表數(shù)據(jù)結(jié)構(gòu)的增刪改查教程
這篇文章主要介紹了Java 單鏈表數(shù)據(jù)結(jié)構(gòu)的增刪改查教程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧2020-10-10
利用反射實(shí)現(xiàn)Excel和CSV 轉(zhuǎn)換為Java對(duì)象功能
將Excel或CSV文件轉(zhuǎn)換為Java對(duì)象(POJO)以及將Java對(duì)象轉(zhuǎn)換為Excel或CSV文件可能是一個(gè)復(fù)雜的過程,但如果使用正確的工具和技術(shù),這個(gè)過程就會(huì)變得十分簡(jiǎn)單,在本文中,我們將了解如何利用一個(gè)Java反射的庫(kù)來(lái)實(shí)現(xiàn)這個(gè)功能,需要的朋友可以參考下2023-11-11
基于java集合中的一些易混淆的知識(shí)點(diǎn)(詳解)
下面小編就為大家?guī)?lái)一篇基于java集合中的一些易混淆的知識(shí)點(diǎn)(詳解)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧2016-09-09
解決websocket 報(bào) Could not decode a text frame as UTF-8錯(cuò)誤
這篇文章主要介紹了解決websocket 報(bào) Could not decode a text frame as UTF-8錯(cuò)誤,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧2020-10-10
手把手教你寫一個(gè)SpringBoot+gRPC服務(wù)
本文將在本地環(huán)境下搭建gRPC客戶端和服務(wù)端,并成功建立通訊發(fā)送消息的方式,從而幫助大家深入了解gRPC在Spring Boot項(xiàng)目中的應(yīng)用,有需要的小伙伴可以參考下2023-12-12
使用JSONObject.toJSONString 過濾掉值為空的key
這篇文章主要介紹了使用JSONObject.toJSONString 過濾掉值為空的key,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03
Springboot關(guān)于自定義stater的yml無(wú)法提示問題解決方案
這篇文章主要介紹了Springboot關(guān)于自定義stater的yml無(wú)法提示問題及解決方案,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-06-06
Maven將代碼及依賴打成一個(gè)Jar包的方式詳解(最新推薦)
這篇文章主要介紹了Maven將代碼及依賴打成一個(gè)Jar包的方式,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-05-05

