javafx 创建windows程序记录2 数据存储

java保存数据

java存储数据 windows下,有几种方式
1、使用缓存 Preferences
2、文件存储 一般有一下两种方式
-使用 .properties文件
-使用 .xml文件

使用Preferences 程序记录1中已经使用

Preferences prefs = Preferences.userNodeForPackage(Main.class); //取得实体

String username = prefs.get("username", "");  //获取数据

prefs.putLong("id", staff.getLongValue("id"));  //设置数据
prefs.put("username", staff.getString("username"));

使用 .properties文件

// 需先判断文件是否存在,否则会报错
			File fileDir=new File("D:\\operation");
			if(!fileDir.exists()){
				fileDir.mkdirs();
			}
			File file=new File(fileDir,"operation.properties");
			if(!file.exists()){
				file.createNewFile();
			}
			//写入
            Properties prop1 = new Properties();   
            FileOutputStream prop= new FileOutputStream(file, false);  //flase 表示是否append 追加
            prop.setProperty("username", agent.getString("username"));
             prop.store(oFile, null);
            oFile.close();

//读取
  Properties prop = new Properties();
        try {
            File file = new File(file);
            boolean exists = file.exists();
            if (exists) {
                InputStream in = new BufferedInputStream(new FileInputStream(file ));
                prop.load(in);
                Iterator<String> it = prop.stringPropertyNames().iterator();
                while (it.hasNext()) {
                    String key = it.next();
                    userName.setText(key);
                    password.setText(prop.getProperty(key));
                }
                in.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

使用xml 请自行百度

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值