Original Post
I have written some code, which saves and loads object arrays to / from file.
I would like to know if this test code is correct, and if there is any way to improve it.
The goals of this test code:
1: - Create object arrays
2: - Save object arrays
3: - Load object arrays
[Note:] The objects being used here are just for testing.
[source lang="java"]import java.io.*;
public class Main {
public static void main(String[] args){
// Simulate Unknown Number Of Objects
String[] cc = {"One","Two","Three","Four"};
// Create The Dump Array
String_Object[] DA = new String_Object[cc.length];
// Create Loading Array
String_Object[] LA;
File file = new File("EXP.sav");
int x= 0;
// Create Object Array
while (x != cc.length){
String_Object STR = new String_Object(cc[x]);
DA[x] = STR;
x ++;
}
x = 0;
// Save Object Array
try{
FileOutputStream SO = new FileOutputStream(file);
ObjectOutputStream save = new ObjectOutputStream(SO);
save.writeObject(DA);
save.close();
}
catch(Exception e){System.out.println(e);}
// Load Object Array
//if (file.exists() ){}
try{
FileInputStream SO = new FileInputStream(file);
ObjectInputStream save = new ObjectInputStream(SO);
LA = (String_Object[]) save.readObject();
save.close();
while (x != LA.length){
LA[x].out();
x ++;
}}
catch(Exception e){System.out.println(e);}
x = 0;
//else{System.out.println("Error Finding File");}
}}
// ========== //
// This Class Is Here To Simulate An Object
public class String_Object implements java.io.Serializable{
String st;
public String_Object (String xx){
st = xx;
}
public void out(){
System.out.println(st);
}}
[/source]
I would like to know if this test code is correct, and if there is any way to improve it.
The goals of this test code:
1: - Create object arrays
2: - Save object arrays
3: - Load object arrays
[Note:] The objects being used here are just for testing.
[source lang="java"]import java.io.*;
public class Main {
public static void main(String[] args){
// Simulate Unknown Number Of Objects
String[] cc = {"One","Two","Three","Four"};
// Create The Dump Array
String_Object[] DA = new String_Object[cc.length];
// Create Loading Array
String_Object[] LA;
File file = new File("EXP.sav");
int x= 0;
// Create Object Array
while (x != cc.length){
String_Object STR = new String_Object(cc[x]);
DA[x] = STR;
x ++;
}
x = 0;
// Save Object Array
try{
FileOutputStream SO = new FileOutputStream(file);
ObjectOutputStream save = new ObjectOutputStream(SO);
save.writeObject(DA);
save.close();
}
catch(Exception e){System.out.println(e);}
// Load Object Array
//if (file.exists() ){}
try{
FileInputStream SO = new FileInputStream(file);
ObjectInputStream save = new ObjectInputStream(SO);
LA = (String_Object[]) save.readObject();
save.close();
while (x != LA.length){
LA[x].out();
x ++;
}}
catch(Exception e){System.out.println(e);}
x = 0;
//else{System.out.println("Error Finding File");}
}}
// ========== //
// This Class Is Here To Simulate An Object
public class String_Object implements java.io.Serializable{
String st;
public String_Object (String xx){
st = xx;
}
public void out(){
System.out.println(st);
}}
[/source]