By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,834 Members | 1,842 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,834 IT Pros & Developers. It's quick & easy.

How do you append to a file?

P: 35
Hi guys!
I am making a quiz. The questions are stored in a binary file. I made an option in the quiz where a user can add a question to the binary file. Altough this works, the problem is that whenever a user enters a new question is just overwrites the whole file, instead of adding it to the rest of the questions in the binary file. Do i have something wrong? Thanks a lot. :-)

This is the piece of code that adds a question to binary file:

Expand|Select|Wrap|Line Numbers
  1. try {
  2.  
  3.                      String question = JOptionPane.showInputDialog(null, "Enter new question");
  4.  
  5.  
  6.  
  7.                 File aFile  = new File( "rocks.dat" );
  8.  
  9.                FileOutputStream aFileOutStream = new FileOutputStream ( aFile );
  10.  
  11.                DataOutputStream aDataOutStream = new DataOutputStream ( aFileOutStream );
  12.  
  13.  
  14.               aDataOutStream.writeUTF( question );
  15.  
  16.               aFileOutStream.close();
  17.  
  18.  
  19.  
  20.  
  21.                     }catch(IOException event){
  22.                    System.out.println("There was a problem:" + e);
  23.  
  24.                                }
Jan 21 '08 #1
Share this Question
Share on Google+
4 Replies


BigDaddyLH
Expert 100+
P: 1,216
Please remember to provide a meaningful Title for any threads started (see the FAQ entry Use a Good Thread Title).

This helps to ensure that other members, and also the general public, will have a better chance of finding answers to any similar questions.

MODERATOR
Jan 21 '08 #2

BigDaddyLH
Expert 100+
P: 1,216
To append to a file, open the file for appending:

FileOutputStream(File file, boolean append)

Expand|Select|Wrap|Line Numbers
  1. FileOutputStream fos = new FileOutputStream (file, true);
Jan 21 '08 #3

P: 35
Thanks a lot. And i'm really sorry about that. Next time i will use a more clear title for the thread. I have another problem, where i have to generate a set of questions stored in a binary file randomly. I used this method (code below), but the questions are repeating themselves. Is there another method i can use, instead of this, to generate the questions randomly, without popping up repetetive questions?

This is the code:

Expand|Select|Wrap|Line Numbers
  1.   public void askQuestions(String[] questions, String[] answers) {
  2.  
  3.         int count = 0;
  4.         int point = 0;
  5.  
  6.  
  7.        for(int j = 0; j < questions.length; j++) {
  8.  
  9.            timeForMore = true;
  10.  
  11.  
  12.           Random generator = new Random();
  13.           int randomIndex = generator.nextInt(questions.length); 
  14.           String input = JOptionPane.showInputDialog(null, questions[randomIndex]); 
  15.  
  16.  
  17.           if(answers[randomIndex].equalsIgnoreCase(input))
  18.  
  19.            count++;  // incrementing counter if entered answer is correct
  20.            point++;
  21.  
  22.  
  23.     if(!timeForMore) // if time is over, the program executes the loop an stops asking questions.
  24.                 break; 
  25.      }
  26.  
  27.         JOptionPane.showMessageDialog(null, "You answered " + count +
  28.                                       " out of " + questions.length +
  29.                                       " questions correctly.");
  30.  
  31.             }
Jan 21 '08 #4

BigDaddyLH
Expert 100+
P: 1,216
To avoid repetition, take the questions and shuffle them, then loop though the shuffled questions. If you are familiar with the Collection Framework, you can pass a list to Collections.shuffle:

Collections.shuffle

If you are not familiar with the Collection Framework, I recommend learning it: it is one of the most useful parts of the API:

http://java.sun.com/docs/books/tutor...ons/index.html
Jan 21 '08 #5

Post your reply

Sign in to post your reply or Sign up for a free account.