How do you append to a file?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • saytri
    New Member
    • Dec 2007
    • 35

    #1

    How do you append to a file?

    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:

    Code:
    try {
                
                         String question = JOptionPane.showInputDialog(null, "Enter new question");
         
         
    
                    File aFile  = new File( "rocks.dat" );
       
                   FileOutputStream aFileOutStream = new FileOutputStream ( aFile );
         
                   DataOutputStream aDataOutStream = new DataOutputStream ( aFileOutStream );
    
         
                  aDataOutStream.writeUTF( question );
         
                  aFileOutStream.close();
    
                            
                            
                        
                        }catch(IOException event){
                       System.out.println("There was a problem:" + e);
    
                                   }
  • BigDaddyLH
    Recognized Expert Top Contributor
    • Dec 2007
    • 1216

    #2
    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

    Comment

    • BigDaddyLH
      Recognized Expert Top Contributor
      • Dec 2007
      • 1216

      #3
      To append to a file, open the file for appending:

      FileOutputStrea m(File file, boolean append)

      [CODE=Java]FileOutputStrea m fos = new FileOutputStrea m (file, true);[/CODE]

      Comment

      • saytri
        New Member
        • Dec 2007
        • 35

        #4
        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:

        Code:
          public void askQuestions(String[] questions, String[] answers) {
              
                int count = 0;
                int point = 0;
                
                
               for(int j = 0; j < questions.length; j++) {
                    
                   timeForMore = true;
                   
                  
                  Random generator = new Random();
                  int randomIndex = generator.nextInt(questions.length); 
                  String input = JOptionPane.showInputDialog(null, questions[randomIndex]); 
        
                
                  if(answers[randomIndex].equalsIgnoreCase(input))
                   
                   count++;  // incrementing counter if entered answer is correct
                   point++;
                  
                  
            if(!timeForMore) // if time is over, the program executes the loop an stops asking questions.
                        break; 
             }
                 
                JOptionPane.showMessageDialog(null, "You answered " + count +
                                              " out of " + questions.length +
                                              " questions correctly.");
        
                    }

        Comment

        • BigDaddyLH
          Recognized Expert Top Contributor
          • Dec 2007
          • 1216

          #5
          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.shu ffle:

          Collections.shu ffle

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

          This collections Java tutorial describes interfaces, implementations, and algorithms in the Java Collections framework

          Comment

          Working...