Path: blob/master/SLICK_HOME/src/org/newdawn/slick/muffin/FileMuffin.java
1461 views
package org.newdawn.slick.muffin;12import java.io.EOFException;3import java.io.File;4import java.io.FileInputStream;5import java.io.FileOutputStream;6import java.io.IOException;7import java.io.ObjectInputStream;8import java.io.ObjectOutputStream;9import java.util.HashMap;1011import org.newdawn.slick.util.Log;1213/**14* An implementation of the muffin load/save mechanism based around using the15* local file system.16*17* @author kappaOne18*/19public class FileMuffin implements Muffin {2021/**22* @see org.newdawn.slick.muffin.Muffin#saveFile(java.util.HashMap,23* java.lang.String)24*/25public void saveFile(HashMap scoreMap, String fileName) throws IOException {26String userHome = System.getProperty("user.home");27File file = new File(userHome);28file = new File(file, ".java");29if (!file.exists()) {30file.mkdir();31}3233file = new File(file, fileName);34FileOutputStream fos = new FileOutputStream(file);35ObjectOutputStream oos = new ObjectOutputStream(fos);3637// save hashMap38oos.writeObject(scoreMap);3940oos.close();41}4243/**44* @see org.newdawn.slick.muffin.Muffin#loadFile(java.lang.String)45*/46public HashMap loadFile(String fileName) throws IOException {47HashMap hashMap = new HashMap();48String userHome = System.getProperty("user.home");4950File file = new File(userHome);51file = new File(file, ".java");52file = new File(file, fileName);5354if (file.exists()) {55try {56FileInputStream fis = new FileInputStream(file);57ObjectInputStream ois = new ObjectInputStream(fis);5859hashMap = (HashMap) ois.readObject();6061ois.close();6263} catch (EOFException e) {64// End of the file reached, do nothing65} catch (ClassNotFoundException e) {66Log.error(e);67throw new IOException("Failed to pull state from store - class not found");68}69}7071return hashMap;72}73}7475