Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
199 views
in Technique[技术] by (71.8m points)

Printing an ArrayList using buffered writer in JFrame Form (Java)

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

question from:https://stackoverflow.com/questions/65943677/printing-an-arraylist-using-buffered-writer-in-jframe-form-java

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...