当前位置: 首页 > news >正文

Java 文件序列化和反序列化

list序列化

 /*** 序列化* @param list* @param filename* @throws IOException*/public static void serializeList(List<Map<String, Object>> list, String filename) throws IOException {try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(filename))) {oos.writeObject(list);}}/*** 反序列化* @param filename* @return* @throws IOException* @throws ClassNotFoundException*/public static List<Map<String, Object>> deserializeList(String filename) throws IOException, ClassNotFoundException {try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(filename))) {@SuppressWarnings("unchecked")List<Map<String, Object>> list = (List<Map<String, Object>>) ois.readObject();return list;}}/*** 查询数据** @return*/public List<Map<String, Object>> getdata() {JdbcTemplate jdbcTemplate2 = getJdbcTemplate2("jdbc:mysql://localhost:3306/gzw?serverTimezone=UTC", "root", "root");String sql = "select name,max(mon) from (\n" +"select name,lx,sum(money) mon from user GROUP BY name,lx order by name ) t1 GROUP BY name order by name ";List<Map<String, Object>> maps = jdbcTemplate2.queryForList(sql);return maps;}public List getLx() throws IOException, ClassNotFoundException {String name = "gzw.data"+sdf.format(new Date());//先判断有没有文件String dir = "E:\\test\\";File file = new File(dir + name);//有文件就用文件数据 -数据多久更新if (file.exists()) {List<Map<String, Object>> objects = deserializeList(dir + name);return objects;} else {//没有文件数据,查询sqlList<Map<String, Object>> getdata = getdata();//序列化,保存为文件serializeList(getdata, dir + name);return getdata;}}
 public static void serializeList(List<Object> list, String filename) throws IOException {try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(filename))) {oos.writeObject(list);}}public static List<Object> deserializeList(String filename) throws IOException, ClassNotFoundException {try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(filename))) {@SuppressWarnings("unchecked")List<Object> list = (List<Object>) ois.readObject();return list;}}

map序列化

package com.dxy.util.xlh;import java.io.*;
import java.util.HashMap;
import java.util.Map;/*** Java序列化和反序列化 map*/
public class SerializeDeserializeMap {public static void serializeMap(Map<String, Object> map, String filename) throws IOException {try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(filename))) {oos.writeObject(map);}}//@SuppressWarnings("unchecked")public static Map<String, Object> deserializeMap(String filename) throws IOException, ClassNotFoundException {try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(filename))) {return (Map<String, Object>) ois.readObject();}}public static void main(String[] args) {Map<String, Object> map = new HashMap<>();map.put("key1", "value1");map.put("key2", "value2");map.put("key3", 1);try {// 序列化serializeMap(map, "map.ser");// 反序列化Map<String, Object> deserializedMap = deserializeMap("map.ser");System.out.println(deserializedMap);} catch (IOException | ClassNotFoundException e) {e.printStackTrace();}}
}
http://www.lryc.cn/news/320678.html

相关文章:

  • NETLINK_ROUTE 与 NETLINK_SOCK_DIAG 的区别与用法
  • docker yocto vscode
  • 使用ansible剧本进行lvm分盘
  • 【排序】快速排序
  • Python大数据实践:selenium爬取京东评论数据
  • 信息系统项目管理师019:存储和数据库(2信息技术发展—2.1信息技术及其发展—2.1.3存储和数据库)
  • Python基础(六)之数值类型元组
  • Chrome历史版本下载地址:Google Chrome Older Versions Download (Windows, Linux Mac)
  • ROS2纯跟踪实现(C++)
  • uniapp微信小程序随机生成canvas-id报错?
  • 爬虫 Day2
  • 达梦数据库SQL
  • python教程——把视频转成gif
  • 深入浅出Go的`encoding/xml`库:实战开发指南
  • 深度学习之扩散模型(Diffusion model)
  • Tomcat Session ID---会话保持
  • Session会话绑定
  • win7、win10、win11 系统能安装的.net framework 版本以
  • RediSearch比Es搜索还快的搜索引擎
  • mybatis-plus 的saveBatch性能分析
  • python异常:pythonIOError异常python打开文件异常
  • 电话机器人语音识别用哪家更好精准度更高。
  • 【Unity动画】Unity如何导入序列帧动画(GIF)
  • uniapp APP 上传文件
  • arcgis数据导出到excel
  • 吴恩达深度学习环境本地化构建wsl+docker+tensorflow+cuda
  • R语言:microeco:一个用于微生物群落生态学数据挖掘的R包:第七:trans_network class
  • ubuntu下在vscode中配置matplotlibcpp
  • Vue面试题,背就完事了
  • centos创建并运行一个redis容器 并支持数据持久化