[Solved] Problem: Read and Writing Variables to a txt file!
Hello, all! This is my first post and I have a question. I recently learned how to write and read from a text document. Which is what I want to do... but I cant write Variables for some reason or I do not know how. My purpose for this is to run a program and it saves some variables into a txt doc and later can read from that and put them back into play. All i have here is the reading and writing and that is what I need help with.
This is to write to the file
Code:
import java.io.*;
import java.lang.*;
import java.util.*;
public class WritetoFileEx
{
public String Health;
private Formatter x;
public void OpenFile()
{
try{
x = new Formatter("Saves.txt");
}
catch (Exception e){
System.out.println ("ERROR");
}
}
public void addVariables()
{
x.format("%s%s%s", "lol ", "Yes ", "Health ");
//What do I put here for reading ints? or some variable nothing has worked
}
public void closeFile()
{
x.close();
}
}
This is to read it
Code:
import java.io.*;
import java.lang.*;
import java.util.*;
public class ReadFileEx
{
private Scanner y;
public void openFile()
{
try {
y = new Scanner (new File("Saves.txt"));
}
catch (Exception e){
System.out.println ("ERROR");
}
}
public void ReadFile()
{
while(y.hasNext()){
String a = y.next();
String b = y.next();
String c = y.next();
System.out.printf("%s %s %s\n", a,b,c);
}
}
public void CloseFile()
{
y.close();
}
}
This is the main class
Code:
public class WriteReadTest
{
public static void main (String[] args)
{
WritetoFileEx test = new WritetoFileEx();
ReadFileEx Read = new ReadFileEx();
test.OpenFile();
test.addVariables();
test.closeFile();
Read.openFile();
Read.ReadFile();
Read.CloseFile();
}
}
If I did not explain my problem well, Please leave a response and I will try to clarify it! Thanks in advance! :D