载入和保存properties文件

载入:

 Properties ps=new Properties();
  FileReader fr=new FileReader("src\\chapter14\\sub03\\props.txt");
  ps.load(fr);
  fr.close();
  ps.list(System.out);

 

写入:

SaveProperties s=new SaveProperties();
  Properties ps=new Properties();
  ps.setProperty("name", "dada");
  ps.setProperty("pwd", "dandan");
  File f=new File("props.txt");
  s.check(f);
  FileWriter fw=new FileWriter(f);
  ps.store(fw, "dada's info");
  fw.close();

在C语言中,可以使用文件I/O操作来保存载入游戏状态。 首先,需要定义一个结构体来存储游戏状态的各个变量,例如: ```c struct game_state { int level; int score; char player_name[20]; // 其他变量 }; ``` 然后,可以使用fopen()函数打开一个文件,使用fprintf()函数将游戏状态写入文件,使用fscanf()函数从文件中读取游戏状态。例如: ```c void save_game_state(struct game_state state) { FILE *fp; fp = fopen("game_state.txt", "w"); fprintf(fp, "%d %d %s", state.level, state.score, state.player_name); fclose(fp); } struct game_state load_game_state() { struct game_state state; FILE *fp; fp = fopen("game_state.txt", "r"); fscanf(fp, "%d %d %s", &state.level, &state.score, state.player_name); fclose(fp); return state; } ``` 注意,需要在编译时加上 -lm 参数,以链接数学库。完整的代码示例如下: ```c #include <stdio.h> struct game_state { int level; int score; char player_name[20]; }; void save_game_state(struct game_state state) { FILE *fp; fp = fopen("game_state.txt", "w"); fprintf(fp, "%d %d %s", state.level, state.score, state.player_name); fclose(fp); } struct game_state load_game_state() { struct game_state state; FILE *fp; fp = fopen("game_state.txt", "r"); fscanf(fp, "%d %d %s", &state.level, &state.score, state.player_name); fclose(fp); return state; } int main() { struct game_state state = {5, 100, "Alice"}; save_game_state(state); struct game_state loaded_state = load_game_state(); printf("Loaded game state: level=%d, score=%d, player_name=%s\n", loaded_state.level, loaded_state.score, loaded_state.player_name); return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值