Java中DataInputStream、DataOutputStream用法
小知識,大挑戰!本文正在參與“程式設計師必備小知識”創作活動。
文章目錄
簡介
在 io 包中,提供了兩個與平臺無關的資料操作流:資料輸出流(DataOutputStream)、資料輸入流 (DataInputStream)。
通常資料輸出流會按照一定的格式將資料輸出,再通過資料輸入流按照一定的格式將資料讀入。DataOutputStream 和 DataOutputStream 用來讀寫固定位元組格式資料。
DataOutputStream
建立物件
java
DataOutputStream out = new DataOutputStream(相接的流)
方法\ 將一個 int 型別的資料寫到資料輸出流中,底層將 4 個位元組寫到基礎輸出流中
java
writeInt(int i)
將一個 double 型別的資料寫到資料輸出流中,底層會將 double 轉換成 long 型別,寫到基礎輸出流中,輸出8個位元組
java
writeDouble(double d)
…
以機器無關的方式使用 utf-8 編碼方式將字串寫到基礎輸出流中。先輸出 2 個位元組表示字串的位元組長度,再輸出這些位元組值
java
writeUTF()
DataInputStream
建立物件
java
DataInputStream dis = new DataInputStream(InputStream in);
方法\ 從資料輸入流中讀取一個 int 型別資料,讀取 4 個位元組
java
readInt()
讀取8個位元組
java
readDouble()
…
先讀取 2 個位元組來確定字串的位元組長度,再讀取這些位元組值
java
readUTF()
Tips:讀取結束,再讀取會出現EOFException
栗子1:寫入資料
```java 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();
}
} ```
執行結果\
栗子2:讀取
java
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) {
//正確讀取結束,不需要處理
}
in.close();
}
}
執行結果:\
栗子3:儲存學生資訊
要求用如下格式儲存學生資訊
學號 00 00 00 01\ 姓名 00 03 61 62 63\ 性別 00 61\ 年齡 00 00 00 16
xml
```xml
<EditText
android:id="@+id/et1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="學號" />
<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" />
```
java
```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異常,所以進行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異常,所以進行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();
}
}
} ```
執行程式:\
\
點選讀取按鈕:\
\
其中
getExternalFilesDir(null)
得到以下路徑
/storage/emulated/0/Android/data/yourPackageName/files
這個目錄會在應用被解除安裝的時候刪除,而且訪問這個目錄不需要動態申請STORAGE 許可權。
所以執行程式會在這個路徑下生成一個 stu.txt 的檔案
- 使用 webpack 打包 ts 程式碼
- Vue Vue3中的setup、ref、reactive
- Java中ByteArrayInputStream、ByteArrayOutputStream用法
- Java中BufferedReader、BufferedWriter用法
- Java中PrintWriter用法
- Java中BufferedOutputStream、BufferedInputStream用法
- Java中PrintStream 用法
- Java中字元編碼總結
- Vue Vue3專案建立和學習
- Cygwin安裝教程
- Java中DataInputStream、DataOutputStream用法