阅读背景:

2020-09-09 Java_IO流(对象流,合并流)

来源:互联网 

对象流

		//对象序列化到文件
		public static void ObjectWrite(Object object,String FilePath) {
		//创建对象流
			ObjectOutputStream oos=null;
			try {
					oos=
				new ObjectOutputStream(new FileOutputStream(new File(FilePath)));
					//写进文件
					oos.writeObject(object);
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}finally {
				if (oos!=null) {
					try {
						oos.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
					
				}
			}
			
		}
		
		//反序列化文件
		public static HashMap<String, Object> ObjectReader(String FilePath){
			//创建对象
			ObjectInputStream ois=null;
			//创建接收集合
			HashMap<String, Object> hashList=null;
			try {
					ois=new ObjectInputStream(new FileInputStream(new File(FilePath)));

					try {
						//接受文件信息
						hashList=(HashMap<String, Object>)ois.readObject();
						
						if (hashList==null) {
							return null;	//没结果返回空
						}
						
					} catch (ClassNotFoundException e) {
						e.printStackTrace();
					}
					
			} catch (FileNotFoundException e) {
				
				e.printStackTrace();
			} catch (IOException e) {
				
				e.printStackTrace();
			}finally {
				try {
					
					if (ois!=null) 
						ois.close();
					
				} catcher (IOException e) {
					e.printStackTrace();
				}
			}
			return hashList;
		}
		//对象序列化到文件
		public sta



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: