I'm am making a RPG game and I would like to find out how to be able to save the current stated if the world, like what the player has equipped, in their inventory, where they are, who they have defeated. Please help! Thank you
/** * Saves the Strings given as the second to the last parameter in the file named like given in filename. * * @param filename * The name of the file where the Strings should be saved. * * @param addToExistingFile * If you want to add the text to an existing file this variable has to be true; * * @param fileText * The strings that should be saved in the file. * * @return * Returns true if the file was successfully createt. False if not. */ public boolean saveFile(String filename, boolean addToExistingFile, String ... fileText) { List<String> existingText = loadFile(filename); BufferedWriter file = null; try { file = new BufferedWriter(new FileWriter(filename)); if (addToExistingFile) { for (String output : existingText) { file.write(output); file.write('\n'); } } for (String output : fileText) { file.write(output); file.write('\n'); } file.close(); } catch (IOException ioe) { //ioe.printStackTrace(); return false; } finally { try { file.close(); } catch (IOException ioe) { ioe.printStackTrace(); } catch (NullPointerException npe) { //npe.printStackTrace(); } } return true; } /** * Saves the Strings given as the second to the last parameter in the file named like given in filename. * * @param filename * The name of the file where the Strings should be saved. * * @param addToExistingFile * If you want to add the text to an existing file this variable has to be true; * * @param fileText * The strings that should be saved in the file as a list. * * @return * Returns true if the file was successfully createt. False if not. */ public boolean saveFile(String filename, boolean addToExistingFile, java.util.List<String> fileText) { List<String> existingText = loadFile(filename); BufferedWriter file = null; try { file = new BufferedWriter(new FileWriter(filename)); if (addToExistingFile) { for (String output : existingText) { file.write(output); file.write('\n'); } } for (String output : fileText) { file.write(output); file.write('\n'); } file.close(); } catch (IOException ioe) { //ioe.printStackTrace(); return false; } finally { try { file.close(); } catch (IOException ioe) { ioe.printStackTrace(); } catch (NullPointerException npe) { //npe.printStackTrace(); } } return true; } /** * Loads the text of the file whith the given filename. * * @param filename * The name of the file that should be loaded. * * @return * Returns a list of Strings consisting of the text of the file. * Each line of the file is a new element of the list. */ public java.util.List<String> loadFile(String filename) { ArrayList<String> fileText = new ArrayList<String>(); BufferedReader file = null; try { file = new BufferedReader(new FileReader(filename)); String input; while ((input = file.readLine()) != null) { fileText.add(input); } } catch (FileNotFoundException fnfe) { //fnfe.printStackTrace(); return null; } catch (IOException ioe) { //ioe.printStackTrace(); return null; } finally { try { file.close(); } catch (IOException ioe) { ioe.printStackTrace(); } catch (NullPointerException npe) { //npe.printStackTrace(); } } return fileText; } /** * Deletes the content of a file. * * @param filename * The name of the file that should be deleted. * * @return * Returns true if the file has ben deleted or if the file didn't exist. * Returns false if the file couldn't be deleted. */ public boolean deleteFile(String filename) { File file = new File(filename); if(!file.delete()) { BufferedWriter fileWriter = null; try { fileWriter = new BufferedWriter(new FileWriter(filename)); fileWriter.write(""); fileWriter.close(); } catch (FileNotFoundException fnfe) { //fnfe.printStackTrace(); return true; } catch (IOException ioe) { ioe.printStackTrace(); return false; } finally { try { fileWriter.close(); } catch (IOException ioe2) { ioe2.printStackTrace(); } catch (NullPointerException npe) { //npe.printStackTrace(); } } return true; } return true; } /** * Check whether a file with the given name is currently existing. * * @param filename * The name of the file that should be checked. * * @return * Returns true if the file is existing. * Returns false if the file is not found or if there was a IOException. */ public boolean fileExisting(String filename) { BufferedReader file = null; try { file = new BufferedReader(new FileReader(filename)); } catch (FileNotFoundException fnfe) { //fnfe.printStackTrace(); return false; } catch (IOException ioe) { ioe.printStackTrace(); return false; } finally { try { file.close(); } catch (IOException ioe) { ioe.printStackTrace(); } catch (NullPointerException npe) { //npe.printStackTrace(); } } return true; } /** * Returns a list of Strings concerning the names of all existing files in the choosen directiory. */ public java.util.List<String> getExistingFileNames(String path) { java.io.File file; List<java.io.File> files; ArrayList<String> fileNames = new ArrayList<String>(); file = new java.io.File(path); files = Arrays.asList(file.listFiles()); for (java.io.File temp : files) { fileNames.add(temp.getName()); } return fileNames; } /** * Returns a list of all files in the choosen directory. */ public java.util.List<java.io.File> getExistingFiles(String path) { java.io.File file = new java.io.File(path); return Arrays.asList(file.listFiles()); }
/** * Check whether the game is played online (on the Greenfoot webside) or offline. * * @return * Returns true if the game is currently played online. */ public boolean playingOnline() { try { BufferedWriter file = new BufferedWriter(new FileWriter("checkingFile_dh48ch30ch3c5.txt")); file.write("Test String"); file.close(); deleteFile("checkingFile_dh48ch30ch3c5.txt"); } catch (Exception e) { //ioe.printStackTrace(); return true; } return false; }