I have taken a basic class on Java, but my knowledge is very little. I have created a text based rpg over the last month or two since I am getting more familiar with Java. I was wondering if there was any way I could have the program create a "save" file to be stored in a certain folder and prompt the user if they would like to open a saved character. I have not learned any of the object oriented parts of Java yet. What could I do to implement this?
Asked
Active
Viewed 150 times
0
-
check out the File class, InputStreams, OutputStreams, FileReader and FileWriter. There are some other helpful Readers and Writers which will make reading and writing files easier. – tfandango Jan 23 '14 at 21:13
-
Start reading the [Java Tutorials](http://docs.oracle.com/javase/tutorial/reallybigindex.html) where you'll find out how to do all of this and more, including some of the "object oriented parts". – Hovercraft Full Of Eels Jan 23 '14 at 21:14
2 Answers
1
I was wondering if there was any way I could have the program create a "save" file to be stored in a certain folder and prompt the user if they would like to open a saved character.
Writing and reading a text file is a solid beginner way to save states for games.
There are many ways to write to files listed here, but here's one example:
PrintWriter out = new PrintWriter("filename.txt");//create the writer object
out.println(text);//write
out.close() //when you're done writing, this will save
Now, here's a simple way to read a file found from here:
BufferedReader br = new BufferedReader(new FileReader("filename.txt"));//objects to read with
try {
StringBuilder sb = new StringBuilder();//use to build a giant string from the file
String line = br.readLine();
//loop file line by line
while (line != null) {
sb.append(line);
sb.append(System.lineSeparator());
line = br.readLine();
}
String everything = sb.toString();
} finally {
br.close();//always close!
}
There are many tutorials online just a Google away. |=^]

Community
- 1
- 1

But I'm Not A Wrapper Class
- 13,614
- 6
- 43
- 65
-
@Relytnevir Some methods in Java will throw Exceptions. You must tell the code to either let it be thrown or catch it. There is multiple ways to handle this error. I would recommend putting a `catch` statement between the `try` and `finally` blocks I have. There's a lot to read about here: http://docs.oracle.com/javase/tutorial/essential/exceptions/ – But I'm Not A Wrapper Class Feb 18 '14 at 18:14
-
Thank you for your help. Working on this game has helped me with a lot of the topics. I just changed it to use parent and child classes to clean up the code a little.I tried using this code, but it gives me this errror: unreported exception FileNotFoundException; must be caught or declared to be thrown. Can you explain to me what is going wrong? @MohammadS. – Relytnevir Feb 18 '14 at 18:16
-1
That’s the class I built to save text,
import java.io.*;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
public class Savetext {
/**
* Saves text to a <i>.txt</i> file with the specified name,
*
* @param name the file name without the extension
*
* @param txt the text to be written in the file
*
* @param message the message to be displayed when done saving, if <tt>null</tt> no
* message will be displayed
*/
public static void Save(String name, String txt, String message) {
name += ".txt";
gtxt(name, txt, message);
}
private static void gtxt(String name, String txt, String mess) {
JFileChooser fc = new JFileChooser();
fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
File file = null;
fc.showSaveDialog(null);
file = fc.getSelectedFile();
String path = file.getPath();
path += "\\";
path += name;
File f1 = new File(path);
try {
PrintWriter out = new PrintWriter(new BufferedWriter(
new FileWriter(f1)));
wrtxt(out, txt, mess);
} catch (IOException e) {
JOptionPane.showMessageDialog(null, e.getMessage().toString());
}
}
private static void wrtxt(PrintWriter out, String txt, String mess) {
out.println(txt);
out.flush();
out.close();
if (mess != null)
JOptionPane.showMessageDialog(null, mess);
}
}
you should call the static save() method,without making an instance of the class,

java-love
- 516
- 1
- 8
- 23
-
1Giving OP chunks of code without explanation isn't a good approach for a beginner... – But I'm Not A Wrapper Class Jan 23 '14 at 21:23
-
-
1@MohammadS.: He's not just posting code without explanation, but worse, he's posting ***bad*** code, including closing the PrintWriter in a different method from where it was created, preventing the ability to re-use it in a loop, or to close it in a finally block, and combining GUI code in the same block as file I/O code, something that should be avoided. Not only that the question has nothing to with GUI or Swing coding. – Hovercraft Full Of Eels Jan 23 '14 at 22:00
-
@Hovercraft Full Of Eels: doesn’t it work for a simple string to be saved as a .txt file? – java-love Jan 23 '14 at 22:04
-
It is terrible code and should not be used as an example for newbies. -1 down-vote. – Hovercraft Full Of Eels Jan 23 '14 at 22:07