简体   繁体   English

写入文件但退出时删除?

[英]Write in a file but delete on exit?

I'm trying to write some datas in a file, it writes. 我写道,我正试图在文件中写一些数据。 But on restart, it deletes all what's inside, and write again. 但是在重新启动时,它会删除内部的所有内容,然后再次写入。 Couldn't find out what's wrong. 无法找出问题所在。

And i also find a method like "deleteOnClose" for file, but it's not been set! 而且我还找到了一个像“deleteOnClose”这样的文件方法,但它没有被设置! So it shouldn't do it by default. 因此默认情况下不应该这样做。

Thanks for your helps in advance. 感谢您的帮助。

Here's code: 这是代码:

Main: 主要:

 public static void class main {

public main(String args[]){

    menu_secim secimObject=new menu_secim();
}
 }

menu_secim: 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");
        }
    }
}
 }

secim_ekle: 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());
                }
            }

        }
    });

}
  }

Add a true here 在这里添加一个true

final BufferedWriter yazici=new BufferedWriter(new FileWriter(dosya, true));

to specify that you want to append to the file (Here is the doc ). 指定要附加到文件(这是doc )。

You write is overwriting all previous data, you need to write in append mode 你写的是覆盖所有以前的数据,你需要在追加模式下写

Here is an example how to: http://www.devdaily.com/java/edu/qanda/pjqa00009.shtml 以下是一个示例: http//www.devdaily.com/java/edu/qanda/pjqa00009.shtml

instead of 代替

final BufferedWriter yazici=new BufferedWriter(new FileWriter(dosya));

use 采用

final BufferedWriter yazici=new BufferedWriter(new FileWriter(dosya, true));

You should do two changes in your code. 您应该在代码中进行两处更改。

First one was already mentioned by several people: 有几个人已经提到过第一个:

final BufferedWriter yazici=new BufferedWriter(new FileWriter(dosya, true));

Second change : you should comment the following line: 第二个变化 :您应该评论以下行:

final Formatter yaz=new Formatter(dosya);

When you create the Formatter instance with the constructor Formatter(File file) , if the file exists then it will be truncated to zero size . 使用构造函数Formatter(文件文件)创建Formatter实例时,如果该file存在,则它将被截断为零大小

The following code works: 以下代码有效:

package q9318686;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Formatter;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

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, true));
        final BufferedReader okuyucu = 
                new BufferedReader(new FileReader(dosya));

// The following line overrides your file:        
//        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());
                    }
                }

            }
        });
    }

    public static void main(String[] args) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    new secim_ekle().setVisible(true);
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        });
    }
}

You need something like this: 你需要这样的东西:

FileWriter fileWriter = new FileWriter(dosya,true);
BufferedWriter bufferWriter = new BufferedWriter(fileWriter);

You keep overwriting your file instead of appending. 您将继续覆盖您的文件而不是附加。

If I am not mistaken, this line of code, File dosya=new File("dizi_listesi.txt"); 如果我没有弄错,这行代码, File dosya=new File("dizi_listesi.txt"); guarantees that you will over-write the file every time. 保证您每次都会覆盖该文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM