943,519 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 2047
  • Java RSS
Jun 23rd, 2009
0

won't create output .txt file :(

Expand Post »
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.

java Syntax (Toggle Plain Text)
  1. public void saveSurvey(String txtFile) throws SecurityException, IOException {
  2. // creates a .txt file of a survey
  3. // .txt file will have name on first line
  4. // then from then on it will have type of question, followed by question text and then choices (if any)
  5. // then the answer
  6. // have to have type there because reader has to know if line determines next question or still choices, etc.
  7.  
  8. // first create a writer output
  9. Writer output = null;
  10.  
  11. // this is the actual text that will be written to the .txt file
  12. String text = this.name + "\n";
  13.  
  14. for (int i = 0; i < this.numQues(); i++) {
  15. if (this.getQues(i).getType().equals("m/c") || this.getQues(i).getType().equals("t/f")
  16. || this.getQues(i).getType().equals("matching") || this.getQues(i).getType().equals("ranking")) {
  17. text += this.getQues(i).getType() + "\n";
  18. text += this.getQues(i).getQuesText() + "\n";
  19. text += this.getQues(i).getLeftChoices() + "\n";
  20. text += this.getQues(i).getRightChoices() + "\n";
  21. text += this.getQues(i).getUserAns() + "\n";
  22. }
  23. else {
  24. text += this.getQues(i).getType() + "\n";
  25. text += this.getQues(i).getQuesText() + "\n";
  26. text += this.getQues(i).getUserAns() + "\n";
  27. }
  28. }
  29. File saveFile = new File(txtFile + ".txt");
  30. output = new BufferedWriter(new FileWriter(saveFile));
  31. output.write(text);
  32. output.close();
  33. }

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)
  1. public survey(String txtFile) throws SecurityException, IOException {
  2. // also sets tag as first one did
  3. type = "survey";
  4.  
  5. // all the code for reading and parsing .txt file and setting up survey (loading)
  6. File file = new File(txtFile + ".txt");
  7. FileInputStream fis = null;
  8. BufferedInputStream bis = null;
  9. DataInputStream dis = null;
  10.  
  11. // counter to read which line of the .txt file you are on
  12. int counter = 0;
  13.  
  14. try {
  15. fis = new FileInputStream(file);
  16.  
  17. // Here BufferedInputStream is added for fast reading.
  18. bis = new BufferedInputStream(fis);
  19. dis = new DataInputStream(bis);
  20.  
  21. // dis.available() returns 0 if the file does not have more lines.
  22. while (dis.available() != 0) {
  23.  
  24. String lineText = dis.readLine();
  25.  
  26. // readLine() reads the line from the file and parses them
  27. // counter will keep track of what line readLine() is on
  28. // will load each line into the correct variables for the survey using its methods
  29. if (counter == 0) {
  30. this.setName(lineText);
  31. counter++;
  32. }
  33. else {
  34. if ((lineText.equals("m/c"))) {
  35. mc newMC = new mc();
  36. String text = dis.readLine();
  37. newMC.setQuesText(text);
  38. String lchoices = dis.readLine();
  39. newMC.addLeftChoice(lchoices);
  40. String rchoices = dis.readLine();
  41. newMC.addRightChoice(rchoices);
  42. String answer = dis.readLine();
  43. newMC.setUserAns(answer);
  44. }
  45. else if ((lineText.equals("t/f"))) {
  46. tf newTF = new tf();
  47. String text = dis.readLine();
  48. newTF.setQuesText(text);
  49.  
  50. // here there is always just T and F but the save function creates two empty lines
  51. // just readLine() twice to get to the user answer
  52. for (int i = 0; i < 2; i++) {
  53. dis.readLine();
  54. }
  55.  
  56. String answer = dis.readLine();
  57. newTF.setUserAns(answer);
  58. }
  59. else if ((lineText.equals("matching"))) {
  60. matching newMatching = new matching();
  61. String text = dis.readLine();
  62. newMatching.setQuesText(text);
  63. String lchoices = dis.readLine();
  64. newMatching.addLeftChoice(lchoices);
  65. String rchoices = dis.readLine();
  66. newMatching.addRightChoice(rchoices);
  67. String answer = dis.readLine();
  68. newMatching.setUserAns(answer);
  69. }
  70. else if ((lineText.equals("ranking"))) {
  71. ranking newRanking = new ranking();
  72. String text = dis.readLine();
  73. newRanking.setQuesText(text);
  74. String lchoices = dis.readLine();
  75. newRanking.addLeftChoice(lchoices);
  76.  
  77. // right choices are just the numbers to be ranked which is in the constructor
  78. // just perform one readLine() to get to next line
  79. dis.readLine();
  80.  
  81. String answer = dis.readLine();
  82. newRanking.setUserAns(answer);
  83. }
  84. else if ((lineText.equals("essay"))) {
  85. essay newEssay = new essay();
  86. String text = dis.readLine();
  87. newEssay.setQuesText(text);
  88. String answer = dis.readLine();
  89. newEssay.setUserAns(answer);
  90. }
  91. else if ((lineText.equals("s/a"))) {
  92. sa newSA = new sa();
  93. String text = dis.readLine();
  94. newSA.setQuesText(text);
  95. String answer = dis.readLine();
  96. newSA.setUserAns(answer);
  97. }
  98. }
  99. }
  100.  
  101. // dispose all the resources after using them.
  102. fis.close();
  103. bis.close();
  104. dis.close();
  105.  
  106. } catch (FileNotFoundException e) {
  107. e.printStackTrace();
  108. } catch (IOException e) {
  109. e.printStackTrace();
  110. }
  111.  
  112. // display the survey when it is done loading
  113. this.displaySurvey();
  114. // and save it
  115. this.saveSurvey("newSurvey");
  116. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
gotm is offline Offline
33 posts
since May 2008
Jun 23rd, 2009
0

Re: won't create output .txt file :(

I have used this to write files...

Java Syntax (Toggle Plain Text)
  1. DataOutputStream output=
  2. new DataOutputStream ( new FileOutputStream(txtFile + ".txt"));
  3.  
  4. output.writeUTF(text);
Last edited by PopeJareth; Jun 23rd, 2009 at 2:44 pm.
Reputation Points: 11
Solved Threads: 3
Light Poster
PopeJareth is offline Offline
29 posts
since Jun 2009
Jun 23rd, 2009
0

Re: won't create output .txt file :(

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
Java Syntax (Toggle Plain Text)
  1. BufferedWriter out = new BufferedWriter(new FileWriter("outfilename"));
  2. out.write("aString");
  3. out.close();

Or a billion other ways. (Another)
Java Syntax (Toggle Plain Text)
  1. PrintWriter pw = new PrintWriter("c:\\temp\\printWriterOutput.txt");
  2. pw.println("PrintWriter is easy to use.");
  3. pw.println(1234);
  4. 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.
Reputation Points: 874
Solved Threads: 352
Posting Maven
BestJewSinceJC is offline Offline
2,758 posts
since Sep 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: String Parsing..
Next Thread in Java Forum Timeline: A CircularBuffer Data Structure--





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC