Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BufferedWriter is not writing a new line in a file for a String having "\n" added to it

I was trying some stuff in Swing (Java), but getting very strange results.

I am getting a String from JTextArea.getText() method and adding "\n" to it. This resultant string I am writing into a file, using BufferedWriter which is chaining through FileOutputStream to a file. But the new line character "\n" is not creating new line in the .txt file.

How can I fix this issue?

My Code is here:

package quizCardGame;

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.ArrayList;
import javax.swing.*;

public class QuizCardBuilder extends JFrame {
private static final long serialVersionUID = 1L;

private JTextArea question;
private JTextArea answer;
private Font font;
private ArrayList<QuizCard> deck;
private int countOfDeck;
private JLabel countLable;

public QuizCardBuilder() {
    super();
    deck = new ArrayList<QuizCard>();
    buildGUI();

}

public static void main(String[] args) {
    QuizCardBuilder app = new QuizCardBuilder();
}

public void buildGUI() {
    font = new Font(Font.SERIF, Font.BOLD, 12);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JPanel panel = new JPanel();
    this.getContentPane().add(BorderLayout.CENTER, panel);

    JPanel stats = new JPanel();
    countLable = new JLabel("Count: " + countOfDeck);
    stats.add(countLable);

    this.getContentPane().add(BorderLayout.SOUTH, stats);

    JLabel qLable = new JLabel("Question");
    panel.add(qLable);

    question = new JTextArea(5, 30);
    question.setLineWrap(true);
    question.setWrapStyleWord(true);
    question.setFont(font);

    JScrollPane qScroller = new JScrollPane(question);
    qScroller
            .setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
    qScroller
            .setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

    panel.add(qScroller);

    JLabel aLable = new JLabel("Answer");
    panel.add(aLable);

    answer = new JTextArea(5, 30);
    answer.setLineWrap(true);
    answer.setWrapStyleWord(true);
    answer.setFont(font);

    JScrollPane aScroller = new JScrollPane(answer);
    aScroller
            .setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
    aScroller
            .setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

    panel.add(aScroller);

    JButton nextButton = new JButton("Next");
    nextButton.addActionListener(new NextCardListener());

    panel.add(nextButton);

    JMenuBar menuBar = new JMenuBar();
    JMenu fileMenu = new JMenu("File");
    JMenuItem newMenuItem = new JMenuItem("New");
    JMenuItem saveMenuItem = new JMenuItem("Save");

    fileMenu.add(newMenuItem);
    fileMenu.add(saveMenuItem);
    menuBar.add(fileMenu);
    this.setJMenuBar(menuBar);

    newMenuItem.addActionListener(new NewMenuListener());
    saveMenuItem.addActionListener(new SaveMenuListener());

    this.setSize(350, 350);
    this.setVisible(true);
}

public class SaveMenuListener implements ActionListener {

    @Override
    public void actionPerformed(ActionEvent e) {
        deck.add(new QuizCard(question.getText(), answer.getText()));

        JFileChooser fileSave = new JFileChooser();
        fileSave.showSaveDialog(null);
        saveFile(fileSave.getSelectedFile());
    }

}

public void saveFile(File file) {

    try {
        BufferedWriter bw = new BufferedWriter(new FileWriter(file));
        for (QuizCard card : deck) {
            bw.write(card.getQuestion() + "/");
            bw.write(card.getAnswer() + "\n");
        }

        bw.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

public class NewMenuListener implements ActionListener {

    @Override
    public void actionPerformed(ActionEvent e) {
        deck.clear();
        countOfDeck = deck.size();
        countLable.setText("Count: " + countOfDeck);
        clearText();
    }

}

public class NextCardListener implements ActionListener {

    @Override
    public void actionPerformed(ActionEvent e) {
        deck.add(new QuizCard(question.getText(), answer.getText()));
        countOfDeck = deck.size();
        countLable.setText("Count: " + countOfDeck);
        clearText();
    }

}

private void clearText() {
    question.setText("");
    answer.setText("");
}

}
like image 730
user3769778 Avatar asked Feb 24 '15 09:02

user3769778


People also ask

Does BufferedWriter write on a new line?

BufferedWriter newLine() method in Java with ExamplesThe newLine() method of BufferedWriter class in Java is used to separate the next line as a new line. It is used as a write separator in buffered writer stream.

Why BufferedWriter is not writing to the file?

You're creating a new FileWriter each time through the loop, which will truncate the file each time. BufferedWriter is buffered, and you're never flushing the buffer. You need to either call bw. flush(), or even better, close the writer when done.

How do you add a new line to a text file in Java?

In Windows, a new line is denoted using “\r\n”, sometimes called a Carriage Return and Line Feed, or CRLF. Adding a new line in Java is as simple as including “\n” , “\r”, or “\r\n” at the end of our string.


1 Answers

Use instead BufferedWriter.newLine() which:

Writes a line separator. The line separator string is defined by the system property line.separator, and is not necessarily a single newline ('\n') character.

like image 133
Andrew Thompson Avatar answered Sep 23 '22 10:09

Andrew Thompson