Witam , chcialbym w moim programie po wcisnieciu przycisku bGeneruj umozliwic uzytkownikowi zapis stringu "ABC" do pliku a zamiast tego za kazdym razem ten nowo utworzony plik jest pusty ! Co robie źle ? Szukalem juz roznych zrodel i probowalem roznych sposobow, za kazdym razem ten sam efekt . Pomocy
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.*;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.text.MaskFormatter;
public class Raport extends JFrame implements ActionListener{
JButton bGeneruj;
JLabel lPocz;
JLabel lKon;
MaskFormatter mFormat;
JFormattedTextField tPocz;
JFormattedTextField tKon;
public Raport(){
setSize(400, 200);
setTitle("Generator Raportow");
setLayout(null);
bGeneruj = new JButton("Generuj");
bGeneruj.setBounds(100, 150, 100, 30);
add(bGeneruj);
bGeneruj.addActionListener(this);
try {
mFormat = new MaskFormatter("####-##-##");
mFormat.setPlaceholderCharacter('_');
} catch (ParseException e) {
// TODO Auto-generated catch block
JOptionPane.showMessageDialog(null, "Niepoprawny format daty (rrrr-mm-dd)");
}
lPocz = new JLabel("Data poczatkowa");
lPocz.setBounds(20, 20, 200, 20);
add(lPocz);
tPocz = new JFormattedTextField(mFormat);
tPocz.setBounds(210,20,100,20);
add(tPocz);
lKon = new JLabel("Data koncowa");
lKon.setBounds(20, 50, 200, 20);
add(lKon);
tKon = new JFormattedTextField(mFormat);
tKon.setBounds(210,50,100,20);
add(tKon);
}
public static void write (String fileName, String text)
{
try
{
PrintWriter out = new PrintWriter(new File(fileName).getAbsoluteFile());
try
{
out.print(text);
} finally {
out.close();
}
} catch(IOException e)
{
throw new RuntimeException(e);
}
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
Object zrodloZdarzenia = e.getSource();
JFileChooser fc = new JFileChooser();
if (zrodloZdarzenia == bGeneruj){
int result = fc.showSaveDialog(this);
if (result == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
try {
FileWriter out = new FileWriter(file);
String tekst = "ABC";
out.write(tekst);
out.close();
} catch (IOException e1) {
System.out.println("Nie moge zapisac pliku: "+file.getAbsolutePath());
System.out.println("Problem: "+e1);
}
}
}
}
}