Created
January 27, 2020 17:04
-
-
Save tony-albanese/2d765f8960577b2916f8b289a7532ca0 to your computer and use it in GitHub Desktop.
Writing to an object and reading from an object in Android.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static void writeFile(Context context){ | |
teamMap = generateHashMap(); | |
try{ | |
FileOutputStream outputStream = context.openFileOutput(FILENAME, Context.MODE_PRIVATE); | |
ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream); | |
objectOutputStream.writeObject(teamMap); | |
System.out.println(teamMap.size()); | |
} catch (FileNotFoundException e) { | |
e.printStackTrace(); | |
Toast.makeText(context, "File not found.", Toast.LENGTH_SHORT).show(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
Toast.makeText(context, "IO Exception.", Toast.LENGTH_SHORT).show(); | |
} | |
} | |
public static void readFile(Context context){ | |
teamMap = new HashMap<>(); | |
try{ | |
InputStream inputStream = context.openFileInput(FILENAME); | |
if(inputStream != null){ | |
ObjectInputStream objectInputStream = new ObjectInputStream(inputStream); | |
HashMap<String, Integer> map = (HashMap<String, Integer>) objectInputStream.readObject(); | |
teamMap = map; | |
System.out.println(map.size()); | |
System.out.println(teamMap.size()); | |
} | |
} catch (FileNotFoundException e) { | |
e.printStackTrace(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} catch (ClassNotFoundException e) { | |
e.printStackTrace(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment