0

I have these three methods and I am trying to write the contents of three lists to a file using the buffered writer.

First Method: To Save File:

public static String showSaveDialog()
    {
        
        String fileName = "";
        JFileChooser chooser = new JFileChooser();
        
        // Suggesting a name
        chooser.setSelectedFile(new File("fileToSave.txt"));
        int resultValue = chooser.showSaveDialog(null);

        if (resultValue == JFileChooser.APPROVE_OPTION) {
            fileName = chooser.getSelectedFile().getAbsolutePath();
            Path = chooser.getSelectedFile().getAbsolutePath();
        }
        
        writeToTextFile(fileName, "");
        
        return fileName;
    }

Second Method: To write To File:

public static void writeToTextFile(String filePath, String toWrite) 
    {

        BufferedWriter writer = null;
        
        try {
            writer = Files.newBufferedWriter(Paths.get(filePath),
                    StandardOpenOption.CREATE);
            
            writer.write(toWrite);
            writer.newLine();
            writer.close();
        } 
        
        catch (IOException ex) {
            JOptionPane.showMessageDialog(null, 
                    "Saving File Error: " + ex.getMessage(),
                    "Saving File Error", JOptionPane.ERROR_MESSAGE);
        }        
    }

Third Method: Write Contents of three lists to text file:

public void saveAllQuestions() {       
        
        for (String q : questionList){
            FileIO.writeToTextFile(FileIO.Path, "$" + q);
            
            for (int i = 0; i < answerList.size(); i++) {
                FileIO.writeToTextFile(FileIO.Path, 
                        answerList.get(i) + ", " + correctAnswers.get(i));
            }
        }
    }

When writing to the file the last line is the only one that shows. I am assuming this problem is due to the fact that it is writing to one line only instead of under each other. Can anybody give me some insight please? Thank you

3
  • You have a loop that keeps opening the file and writing one line of text to it. Your code should open the file BEFORE the loop. Then you write multiple lines to the file. Then when the loop finishes you close the file. Commented Jan 28, 2021 at 19:09
  • Which method are you referring to please? Commented Jan 31, 2021 at 21:35
  • 1
    Only one method has a loop. 1) open the file 2) use the loop to write data to the file 3) close the file. So basically the writeToTextFile(...) method is NOT needed. Commented Feb 1, 2021 at 0:41

1 Answer 1

1

You are opening and closing the file for every line you write. Each time you write a line you are deleting the previous version of the file and replacing it with that one line. You need to open the file, write all the lines, and then close the file.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.