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");
}