So I have this function that is supposed to output an object (save an object) to a .txt file. It doesn't output anything when the program comes to the point where it runs this function. I am 99% sure this is where the program hangs.
public void saveSurvey(String txtFile) throws SecurityException, IOException {
// creates a .txt file of a survey
// .txt file will have name on first line
// then from then on it will have type of question, followed by question text and then choices (if any)
// then the answer
// have to have type there because reader has to know if line determines next question or still choices, etc.
// first create a writer output
Writer output = null;
// this is the actual text that will be written to the .txt file
String text = this.name + "\n";
for (int i = 0; i < this.numQues(); i++) {
if (this.getQues(i).getType().equals("m/c") || this.getQues(i).getType().equals("t/f")
|| this.getQues(i).getType().equals("matching") || this.getQues(i).getType().equals("ranking")) {
text += this.getQues(i).getType() + "\n";
text += this.getQues(i).getQuesText() + "\n";
text += this.getQues(i).getLeftChoices() + "\n";
text += this.getQues(i).getRightChoices() + "\n";
text += this.getQues(i).getUserAns() + "\n";
}
else {
text += this.getQues(i).getType() + "\n";
text += this.getQues(i).getQuesText() + "\n";
text += this.getQues(i).getUserAns() + "\n";
}
}
File saveFile = new File(txtFile + ".txt");
output = new BufferedWriter(new FileWriter(saveFile));
output.write(text);
output.close();
}
The alternate constructor to the survey class calls upon a text file name and loads that .txt file (that should be in the format above), into the program and creates new objects based on what it finds. I'm going to put this constructor below because I also want to know if I am possibly going to run into any problems when I try loading (after I get saving working). Maybe I could start working on them now.
public survey(String txtFile) throws SecurityException, IOException {
// also sets tag as first one did
type = "survey";
// all the code for reading and parsing .txt file and setting up survey (loading)
File file = new File(txtFile + ".txt");
FileInputStream fis = null;
BufferedInputStream bis = null;
DataInputStream dis = null;
// counter to read which line of the .txt file you are on
int counter = 0;
try {
fis = new FileInputStream(file);
// Here BufferedInputStream is added for fast reading.
bis = new BufferedInputStream(fis);
dis = new DataInputStream(bis);
// dis.available() returns 0 if the file does not have more lines.
while (dis.available() != 0) {
String lineText = dis.readLine();
// readLine() reads the line from the file and parses them
// counter will keep track of what line readLine() is on
// will load each line into the correct variables for the survey using its methods
if (counter == 0) {
this.setName(lineText);
counter++;
}
else {
if ((lineText.equals("m/c"))) {
mc newMC = new mc();
String text = dis.readLine();
newMC.setQuesText(text);
String lchoices = dis.readLine();
newMC.addLeftChoice(lchoices);
String rchoices = dis.readLine();
newMC.addRightChoice(rchoices);
String answer = dis.readLine();
newMC.setUserAns(answer);
}
else if ((lineText.equals("t/f"))) {
tf newTF = new tf();
String text = dis.readLine();
newTF.setQuesText(text);
// here there is always just T and F but the save function creates two empty lines
// just readLine() twice to get to the user answer
for (int i = 0; i < 2; i++) {
dis.readLine();
}
String answer = dis.readLine();
newTF.setUserAns(answer);
}
else if ((lineText.equals("matching"))) {
matching newMatching = new matching();
String text = dis.readLine();
newMatching.setQuesText(text);
String lchoices = dis.readLine();
newMatching.addLeftChoice(lchoices);
String rchoices = dis.readLine();
newMatching.addRightChoice(rchoices);
String answer = dis.readLine();
newMatching.setUserAns(answer);
}
else if ((lineText.equals("ranking"))) {
ranking newRanking = new ranking();
String text = dis.readLine();
newRanking.setQuesText(text);
String lchoices = dis.readLine();
newRanking.addLeftChoice(lchoices);
// right choices are just the numbers to be ranked which is in the constructor
// just perform one readLine() to get to next line
dis.readLine();
String answer = dis.readLine();
newRanking.setUserAns(answer);
}
else if ((lineText.equals("essay"))) {
essay newEssay = new essay();
String text = dis.readLine();
newEssay.setQuesText(text);
String answer = dis.readLine();
newEssay.setUserAns(answer);
}
else if ((lineText.equals("s/a"))) {
sa newSA = new sa();
String text = dis.readLine();
newSA.setQuesText(text);
String answer = dis.readLine();
newSA.setUserAns(answer);
}
}
}
// dispose all the resources after using them.
fis.close();
bis.close();
dis.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// display the survey when it is done loading
this.displaySurvey();
// and save it
this.saveSurvey("newSurvey");
}