Witam, mam pewien problem, czy mógłbym uzyskać informację, w jaki sposób mogę zapisać do pliku jakiś string, ponieważ gdy próbuje to wykonać wstawiając ten kod do programu :
BufferedWriter writer=new BufferedWriter(FileWriter("file.txt"));
writer.write("string");
package generowanie_testow_2008;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.io.*;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.*;
import javax.swing.JButton;
import javax.swing.*;
import javax.swing.border.TitledBorder;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.LinkedList;
public class Frame1 extends JFrame {
JPanel contentPane;
BorderLayout borderLayout1 = new BorderLayout();
JPanel jPanel1 = new JPanel();
GridBagLayout gridBagLayout1 = new GridBagLayout();
JButton jButton1 = new JButton();
TitledBorder titledBorder1 = new TitledBorder("");
JLabel jLabel1 = new JLabel();
JTextField jTextField1 = new JTextField();
JTextField jTextField2 = new JTextField();
JLabel jLabel2 = new JLabel();
JLabel jLabel3 = new JLabel();
JLabel jLabel5 = new JLabel();
JTextField jTextField3 = new JTextField();
JTabbedPane jTabbedPane1 = new JTabbedPane();
JPanel jPanel2 = new JPanel();
GridLayout gridLayout1 = new GridLayout();
public Frame1() {
try {
setDefaultCloseOperation(EXIT_ON_CLOSE);
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
}
private void jbInit() throws Exception {
contentPane = (JPanel) getContentPane();
contentPane.setLayout(borderLayout1);
setSize(new Dimension(392, 310));
setTitle("Generowanie Testów 2008");
jPanel1.setLayout(gridBagLayout1);
jButton1.setForeground(UIManager.getColor("Desktop.background"));
jButton1.setBorder(titledBorder1);
jButton1.setToolTipText("Wciśnij aby wygenerować testy");
jButton1.setHorizontalTextPosition(SwingConstants.CENTER);
jButton1.setIcon(null);
jButton1.setText("Generuj Testy");
jButton1.addActionListener(new Frame1_jButton1_actionAdapter(this));
jPanel1.setBackground(new Color(21, 187, 255));
jLabel1.setFont(new java.awt.Font("Georgia", Font.PLAIN, 25));
jLabel1.setForeground(Color.yellow);
jLabel1.setText("Generator Testów 2008");
jTextField1.setBackground(Color.orange);
jTextField1.setFont(new java.awt.Font("Dialog", Font.ITALIC, 11));
jTextField1.setToolTipText("Tutaj wpisz nazwę pliku z źródłami testów");
jTextField1.setSelectionColor(Color.gray);
jTextField1.setText("input.txt");
jTextField2.setBackground(Color.orange);
jTextField2.setFont(new java.awt.Font("Dialog", Font.ITALIC, 11));
jTextField2.setToolTipText(
"Tutaj wpisz nazwę pliku do wygenerowania testu");
jTextField2.setCaretColor(Color.white);
jTextField2.setText("output.txt");
jLabel2.setFont(new java.awt.Font("Dialog", Font.BOLD, 11));
jLabel2.setForeground(SystemColor.inactiveCaptionText);
jLabel2.setText("Plik wejściowy:");
jLabel3.setFont(new java.awt.Font("Dialog", Font.BOLD, 11));
jLabel3.setForeground(SystemColor.inactiveCaptionText);
jLabel3.setText("Plik wyjściowy:");
jLabel5.setFont(new java.awt.Font("Dialog", Font.BOLD, 11));
jLabel5.setForeground(SystemColor.inactiveCaptionText);
jLabel5.setText("Tytuł testu:");
jTextField3.setBackground(Color.orange);
jTextField3.setFont(new java.awt.Font("Dialog", Font.ITALIC, 11));
jTextField3.setToolTipText(
"Tutaj wpisz tytuł który będzie miał wygenerowany test");
jTextField3.setText("Testy 2008");
jPanel2.setBackground(new Color(21, 187, 255));
jPanel2.setLayout(gridLayout1);
jPanel1.add(jLabel1, new GridBagConstraints(1, 0, 2, 1, 0.0, 0.0
, GridBagConstraints.EAST, GridBagConstraints.NONE,
new Insets(1, 0, 17, 0), 6, 2));
jPanel1.add(jLabel2, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0
, GridBagConstraints.CENTER, GridBagConstraints.NONE,
new Insets(22, 0, 0, 0), 0, 0));
jPanel1.add(jLabel3, new GridBagConstraints(1, 2, 1, 1, 0.0, 0.0
, GridBagConstraints.CENTER, GridBagConstraints.NONE,
new Insets(20, 0, 0, 0), 0, 0));
jPanel1.add(jButton1, new GridBagConstraints(2, 4, 1, 1, 0.0, 0.0
, GridBagConstraints.SOUTH, GridBagConstraints.NONE,
new Insets(27, 0, 0, 73), 36, 0));
jPanel1.add(jLabel5, new GridBagConstraints(1, 3, 1, 1, 0.0, 0.0
, GridBagConstraints.WEST, GridBagConstraints.NONE,
new Insets(19, 0, 0, 0), 0, 0));
jPanel1.add(jTextField1, new GridBagConstraints(2, 1, 1, 1, 0.0, 0.0
, GridBagConstraints.SOUTHEAST, GridBagConstraints.NONE,
new Insets(0, 0, 0, 0), 65, 0));
jPanel1.add(jTextField2, new GridBagConstraints(2, 2, 1, 1, 0.0, 0.0
, GridBagConstraints.SOUTHEAST, GridBagConstraints.NONE,
new Insets(0, 3, 0, 1), 56, 0));
jPanel1.add(jTextField3, new GridBagConstraints(2, 3, 1, 1, 0.0, 0.0
, GridBagConstraints.SOUTHEAST, GridBagConstraints.NONE,
new Insets(0, 0, 0, 0), 49, 0));
jTabbedPane1.add(jPanel2, "jPanel2");
contentPane.add(jTabbedPane1, java.awt.BorderLayout.CENTER);
jTabbedPane1.add(jPanel1, "jPanel1");
}
public void jButton1_actionPerformed(ActionEvent e) {
//Przycisk generuj
LinkedList list1 = new LinkedList();
//Otwieranie pliku
try {
FileReader FR3 = new FileReader("data.txt");
StringBuffer SB3 = new StringBuffer();
int temp3;
while ((temp3 = FR3.read()) != -1) {
char symbol = (char) temp3;
if (symbol == '|') {
list1.add(SB3.toString());
SB3 = new StringBuffer();
} else
SB3.append(symbol);
}
list1.add(SB3.toString());
int a = list1.size();
for(int z = 0;z<a;z++)
{
String b = (String) list1.get(z);
System.out.println(b);
}
} catch (Exception exception) {
exception.printStackTrace();
}
}
}
class Frame1_jButton1_actionAdapter implements ActionListener {
private Frame1 adaptee;
Frame1_jButton1_actionAdapter(Frame1 adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton1_actionPerformed(e);
}
}