mirror of
https://github.com/Dichgrem/Java.git
synced 2025-12-16 12:41:58 -05:00
41 lines
1.1 KiB
Java
41 lines
1.1 KiB
Java
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());
|
|
}
|
|
}
|
|
}
|