| | |
won't create output .txt file :(
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: May 2008
Posts: 33
Reputation:
Solved Threads: 0
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.
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.
java Syntax (Toggle Plain Text)
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.
java Syntax (Toggle Plain Text)
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"); }
•
•
Join Date: Jun 2009
Posts: 28
Reputation:
Solved Threads: 3
I have used this to write files...
Java Syntax (Toggle Plain Text)
DataOutputStream output= new DataOutputStream ( new FileOutputStream(txtFile + ".txt")); output.writeUTF(text);
Last edited by PopeJareth; Jun 23rd, 2009 at 2:44 pm.
•
•
Join Date: Sep 2008
Posts: 1,654
Reputation:
Solved Threads: 206
If you want to write an Object out to a file, you need to implement the Serializable interface, look up "Java Serializable". If you just want to write text into a file, that is different, and you could use
Or a billion other ways. (Another)
Java Syntax (Toggle Plain Text)
BufferedWriter out = new BufferedWriter(new FileWriter("outfilename")); out.write("aString"); out.close();
Or a billion other ways. (Another)
Java Syntax (Toggle Plain Text)
PrintWriter pw = new PrintWriter("c:\\temp\\printWriterOutput.txt"); pw.println("PrintWriter is easy to use."); pw.println(1234); pw.close();
Last edited by ~s.o.s~; Jun 24th, 2009 at 1:15 am. Reason: Added code tags; any code more than a single line looks messy without tags.
![]() |
Similar Threads
- Create an array from txt file (Java)
- help with printing multiple lines print a txt file (PHP)
- saving results to a txt file. (Java)
- problem of data output into *.txt file (C++)
- Bubble Sort a txt file (Java)
- reading a text file and searching for a sentence (C++)
- create pdf or word file from within c# (C#)
Other Threads in the Java Forum
- Previous Thread: String Parsing..
- Next Thread: A CircularBuffer Data Structure--
Views: 942 | Replies: 2
| Thread Tools | Search this Thread |
Tag cloud for Java
3d @param affinetransform android api apple applet application arc arguments array arrays automation binary bluetooth byte c# chat class classes click client code color compare component corrupted database detection draw eclipse error event exception file fractal game givemetehcodez graphics gui guitesting helpwithhomework html ide image input integer j2me java java.xls javaprojects jmf jni jpanel julia keytool linux list loop map method methods mobile netbeans newbie number object oracle pong print problem producer program programming project projectideas read recursion reflection replaysolutions rim scanner screen server set size sms socket sort sql string swing terminal test threads time transfer tree web windows






