java對象寫入文件

public void writeObject(HashMap<String, ?> map) {
		try {
			FileOutputStream outStream = new FileOutputStream("E:/1.txt");
			ObjectOutputStream objectOutputStream = new ObjectOutputStream(outStream);
			
			objectOutputStream.writeObject(map);
			outStream.close();
			System.out.println("successful");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	@SuppressWarnings("unchecked")
	public HashMap<?,?> readObject(File file){
		FileInputStream freader;
		try {
			freader = new FileInputStream(file);
			ObjectInputStream objectInputStream = new ObjectInputStream(freader);
			HashMap<String,String> map = new HashMap<String,String>();
			 map = (HashMap<String, String>) objectInputStream.readObject();
			 return map;
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
		return null;
	}
相關文章
相關標籤/搜索