update:demo5

This commit is contained in:
dichgrem
2025-12-05 14:33:25 +08:00
parent 5832b4fe9d
commit 43c6b58317
5 changed files with 103 additions and 1 deletions

View File

@@ -0,0 +1,40 @@
package com.study.demo5;
import java.io.*;
public class DataIO {
public void writeConfig(String path) {
try (DataOutputStream dos = new DataOutputStream(
new FileOutputStream(path))) {
dos.writeUTF("李华"); // 用户名
dos.writeInt(25); // 年龄
dos.writeBoolean(true); // 是否启用
dos.writeDouble(1234.56); // 余额
System.out.println("配置写入成功: " + path);
} catch (IOException e) {
System.err.println("写入配置失败: " + e.getMessage());
}
}
public void readConfig(String path) {
try (DataInputStream dis = new DataInputStream(
new FileInputStream(path))) {
String username = dis.readUTF();
int age = dis.readInt();
boolean enabled = dis.readBoolean();
double balance = dis.readDouble();
System.out.println("用户名: " + username);
System.out.println("年龄: " + age);
System.out.println("是否启用: " + enabled);
System.out.println("余额: " + balance);
} catch (IOException e) {
System.err.println("读取配置失败: " + e.getMessage());
}
}
}