java对象保存到文件

来源:互联网 发布:matlab创建结构数组 编辑:程序博客网 时间:2024/05/27 20:43
private boolean createInfoFile(HardwareInfo info){
        BufferedWriter bw = null;
        try {
            String filePath = fsv.getHomeDirectory().getAbsolutePath().concat(File.separator).concat(FILENAME);
            System.out.println("filePath : " + filePath);
            File file = new File(filePath);
            if(file.exists()){
                file.delete();
            }
            bw = new BufferedWriter(new FileWriter(file));
            bw.write("CPU type : " + info.getCpuType());
            bw.newLine();
            bw.write("CPU cores : " + String.valueOf(info.getCpuCores()));
            bw.newLine();
            bw.write("CPU ID : " + info.getCpuId());
            bw.newLine();
            bw.write("Disk ID : " + info.getDiskId());
            bw.newLine();
            bw.write("Mac Address : " + info.getMacAddr());
        } catch (IOException ex) {
            Logger.getLogger(ApplyForLicense.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        } finally {
            try {
                bw.close();
            } catch (IOException ex) {
                Logger.getLogger(ApplyForLicense.class.getName()).log(Level.SEVERE, null, ex);
                return false;
            }
        }
        return true;
    }