load();
I am completely lost i have to load a text file, and dont have a clue, could someone help me please;
here is my save mehtod;
publicvoid save(){
PrintWriter out =null;
try{
out =new PrintWriter(new FileWriter(storageFile));
out.println(communityName);
for(int i = 0; i <= lastIndex; ++i){
alienArray[i].writeToFile(out);
out.println();
}
}catch (Exception e){
}finally{
try{
out.close();
}catch (Exception e){
}
}
}
i want to load data fromo a text file into an array what i have tried so far is this. and it does not work
public void load(){
BufferedReader in = null;
AlienCreature creature = null;
try{
File file = new File(storageFile);
if(! file.exists()){
System.out.println("Not valid");
return;
}
in = new BufferedReader(new FileReader(file));
communityName = in.readLine();
while(in.ready()){
addAlien(creature);
}
} catch (Exception e){
} finally {
try{
in.close();
}catch(Exception e){}
}
}
Hi, what do you mean? If you need to read text file then you can use example from "How to get specified string from file?" topic.Regards,Sasha.
i tried this cose out and it only displays the data once the program is about to exit
public void load(){
BufferedReader in = null;
AlienCreature creature = null;
String fileName = "AlienCreature.txt";
String line = "";
try
{
in = new BufferedReader(new FileReader(fileName));
while(in.ready())
{
line = in.readLine();
System.out.println(line);
}
in.close();
}
catch(IOException e){
System.out.println("Error reading " + fileName);
}
}
i need to load the information into the array
What do you mean doesn't work...how is your file formatted?
This is not going to work at all, but to tell you what you need to do you need to give us all relavant info.
communityName = in.readLine();
while(in.ready()){
addAlien(creature);
}