写入文件但退出时删除?

yah*_*hya 2 java swing rewrite file

我写道,我正试图在文件中写一些数据.但是在重新启动时,它会删除内部的所有内容,然后再次写入.无法找出问题所在.

而且我还找到了一个像"deleteOnClose"这样的文件方法,但它没有被设置!因此默认情况下不应该这样做.

感谢您的帮助.

这是代码:

主要:

 public static void class main {

public main(String args[]){

    menu_secim secimObject=new menu_secim();
}
 }
Run Code Online (Sandbox Code Playgroud)

menu_secim:

 public class menu_secim extends JFrame{

private JButton ekle;
private JButton goster;

public menu_secim() {

    super("Karar An?");
    this.setLayout(new FlowLayout());
    this.setVisible(true);
    this.setSize(350, 150);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setLocation(500,300);


    ekle=new JButton("Ekle");
    this.getContentPane().add(ekle);

    goster=new JButton("Göster");
    this.getContentPane().add(goster);

    ekle.setBounds(20,20, 100,60);
    goster.setBounds(120, 20, 100, 60);

    butonHareketleri buton=new butonHareketleri();
    ekle.addActionListener(buton);
    goster.addActionListener(buton);

}
private class butonHareketleri implements ActionListener{

    public void actionPerformed(ActionEvent e){

        if(e.getSource()==ekle)
        try
        {               
            new secim_ekle();   
        }
        catch(Exception h)
        {
        System.out.print("hata var");
        }
    }
}
 }
Run Code Online (Sandbox Code Playgroud)

secim_ekle:

public class secim_ekle extends JFrame {

private JButton ekle;

private JLabel dizi_isim;
private JLabel dizi_puan;
private JLabel kaydet;

private JTextField dizi_isim_fd;
private JTextField dizi_puan_fd;

File dosya=new File("dizi_listesi.txt");

public  secim_ekle() throws IOException{


    super("Ekleme i?lemi");
    this.getContentPane().setLayout(null);
    this.setVisible(true);
    this.setSize(500, 150);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setLocation(500,300);

    final BufferedWriter yazici=new BufferedWriter(new FileWriter(dosya));
    final BufferedReader okuyucu=new BufferedReader(new FileReader(dosya));

    final Formatter yaz=new Formatter(dosya);
    final Scanner oku=new Scanner(dosya);

    ekle=new JButton("Ekle");
    this.getContentPane().add(ekle);

    dizi_isim=new JLabel("Dizi ismi :");
    this.getContentPane().add(dizi_isim);

    dizi_puan=new JLabel("Dizi puan :");
    this.getContentPane().add(dizi_puan);

    kaydet=new JLabel("Veriler Kaydedildi!");
    kaydet.setVisible(false);
    kaydet.setForeground(Color.RED);
    this.getContentPane().add(kaydet);

    dizi_isim_fd=new JTextField();
    this.getContentPane().add(dizi_isim_fd);

    dizi_puan_fd=new JTextField();
    this.getContentPane().add(dizi_puan_fd);

    dizi_isim.setBounds(10, 10, 100, 20);
    dizi_puan.setBounds(10,40,100,20);
    dizi_isim_fd.setBounds(120,10,200,20);
    dizi_puan_fd.setBounds(120,40,50,20);
    ekle.setBounds(350,10,100,20);
    kaydet.setBounds(350, 40, 200, 40);

    ekle.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){

            if(e.getSource()==ekle){
                try{
                    yazici.write(dizi_isim_fd.getText()+"|"+dizi_puan_fd.getText());
                    yazici.newLine();
                    yazici.close();
                    kaydet.setVisible(true);
                }
                catch(Exception h)
                {
                    System.out.print(h.getMessage());
                }
            }

        }
    });

}
  }
Run Code Online (Sandbox Code Playgroud)

tal*_*las 5

true这里添加一个

final BufferedWriter yazici=new BufferedWriter(new FileWriter(dosya, true));
Run Code Online (Sandbox Code Playgroud)

指定要附加到文件(这是doc).