I know I have to use file I/O but I don't like how to use it. Anyone can teach me how to implement file I/O to capture the information on the world into a txt file?
public static List readText(String reqfile)// throws IOException
{
FileInputStream fis = null;
BufferedReader r = null;
List lines = new ArrayList();
try
{
if(reqfile != null)
{
fis = new FileInputStream(reqfile);
r = new BufferedReader(new InputStreamReader(fis));
String linetmp;
while((linetmp = r.readLine()) != null)
{
lines.add(linetmp);
}
}
}
catch(IOException e)
{
System.err.println("Caught IOException: " + e.getMessage());
}
return lines;
} public void load() throws Exception
{
//<----here I want to load the save in "game.txt"
}
public void save() throws Exception
{
FileWriter writer = null;
writer = new FileWriter("game.txt");
writer.write(???); //<---here I want to save the what world is like at this moment
writer.close();
}fis = new FileInputStream("game.txt");
r = new BufferedReader(new InputStreamReader(fis)); List lines = new ArrayList(); //This is where we will store the Strings
String linetmp;
while((linetmp = r.readLine()) != null)//This reads the next line, saves it as linetmp, then continues as long as it isn't null
{
lines.add(linetmp); //This adds the String to our List
}writer = new FileWriter("game.txt", false); //This makes it overwrite the old fileList linesToWrite = //your lines
for(int i = 0; i < linesToWrite.length; i++)
{
if(linesToWrite[i] != null)
{
writer.write(linesToWrite[i]);
writer.newLine();
}
}
writer.close(); public void load() throws Exception
{
File inputFile = new File("a.txt");
Scanner input = new Scanner("a.txt");
int [][] num; // <--Could I do this to store a nx4 array where [0][0] is like the text file shown eg. 0
String [][] state;//<-- Could I do this to store a nx2 array which only contain true and false
boolean exists = inputFile.exists();
if( exists ){
while(input.hasNext()){
startPerson = ???; //<--How can I get the first number in the text
for(int row=0;row<the number of line in the text ; row++){//how to set the number of line in the text
for(int col=0;col<2;col++){
String state[row][col] = input.next(); //<--I want this to store true false in that nx2 array
}
}
for(int row=0;row<the number of line in the text ; row++){ //how to set the number of line in the text
for(int col=0;col<2;col++){
num[row][col] = input.nextInt(); // I want to store the numbers into a array starting to read from the second line
}
}
for (int i=0;i<number of line in the text;i++) { //this loop need to be correct but dun know how
Object c = new Object(num[i][0], num[i][1], state[i][0]);
state[i][1] = c.selected;
addObject(c, num[i][2], num[i][3]);
}
}
input.close();
}else return; ....
writer.println(startPerson);
for(int i=0;i<numOfObjects;i++){
Object object = objectss.get(i);
writer.print(object.color() + " " + object.which() + " " + object.ex() + " " + object.getX() + " " + object.getY() + " " + object.selected);
writer.println();
}
writer.close(); // to create the string to save
String text = "";
text += (char) startPerson;
for (int i = 0; i < lineCount; i++)
{
int value = 1; // to set second highest bit
value = (value * 2) + (state[i][1] ? 1 : 0);
value = (value * 8) + num[i][3];
value = (value * 8) + num[i][2];
value = (value * 2) + (state[i][0] ? 1 : 0);
value = (value * 8) + num[i][1];
value = (value * 8) + num[i][0];
text += (char) value;
}
// to break the string back up after reading it back in as 'text'
lineCount = text.length() - 1;
if (lineCount != -1))
{
startPerson = (int) text.charAt(0);
text = text.substring(1);
for (int i = 0; i < lineCount; i++)
{
int value = (int) text.charAt(i);
num[i][0] = value % 8;
value = value / 8;
num[i][1] = value % 8;
value = value / 8;
state[i][0] = value % 2 == 1;
value = value / 2;
num[i][2] = value % 8;
value = value / 8;
num[i][3] = value % 8;
value = value / 8;
state[i][1] = value % 2 == 1;
}
} public static int [][] num;
public static int startPerson = -1;
public static int lineCount = 0; .....
String text = "";
int line=0;
text += (char) startPerson;
while(input.hasNextLine()==true)
{
line++;
}
for (int i = 0; i < line; i++)
{
int value = 1; // to set second highest bit
value = (value * 2) + num[i][0];
value = (value * 7) + num[i][1];
value = (value * 8) + num[i][2];
value = (value * 4) + num[i][3];
value = (value * 2) + num[i][4];
value = (value * 2) + num[i][5];
text += (char) value; }
boolean flipState;
lineCount = text.length()-2;
if(lineCount != -1)
{
startPerson = (int) text.charAt(0);
text = text.substring(2);
for(int i=0;i<lineCount;i++){
int value = (int) text.charAt(i);
num[i][0] = value % 2;
value = value / 2; int objNum = 0;
for (Obj obj : getObjects(Object1.class))
{
Object1 o1 = (Object1) obj;
text += encode(o1.color(), o1.which(), o1.ex, o1.getX(), o1.getY(), o1.selected);
}