У меня есть файл BankAccount.txt как в нем имеются данные
Кредит:12500:1-
А что вы тут смотрите?:99999999:2-
Это надо закрыть:15428:3-
Люблю java:4850:4-
Долг Лехе:100050:5-
На отдых:2000:6-
После удаления 4 строки должно быть так
Кредит:12500:1-
А что вы тут смотрите?:99999999:2-
Это надо закрыть:15428:3-
Долг Лехе:100050:5-
На отдых:2000:6-
Общий код
import javax.swing.*;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.DocumentFilter;
import javax.swing.text.PlainDocument;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.util.Arrays;
import java.util.concurrent.ExecutionException;
import static java.lang.String.valueOf;
public class MainWindow extends JFrame implements ActionListener {
JPanel mainPanel, createPanel, closePanel, atmPanel;
JButton crBt, crBtBack, clBt, clBtBack, atmBt, atmBtBack, infoBt, crFinal, clFinal;
JLabel nameBank, nameSScore, nameMScore, unicId;
JTextField nameScore, moneyScore, idScore;
public MainWindow() {
//Имя окна
super("Нае(Банк)");
mainPanel = new JPanel();
mainPanel.setLayout(null);
createPanel = new JPanel();
createPanel.setLayout(null);
closePanel = new JPanel();
closePanel.setLayout(null);
atmPanel = new JPanel();
atmPanel.setLayout(null);
//Основная панель
crBt = new JButton("Создать счет");
crBt.setBounds(165, 150, 170, 25);
crBt.setActionCommand("createScore");
crBt.addActionListener(this);
clBt = new JButton("Закрыть счет");
clBt.setBounds(165, 200, 170, 25);
clBt.setActionCommand("closeScore");
clBt.addActionListener(this);
atmBt = new JButton("Банкомат");
atmBt.setBounds(165, 250, 170, 25);
atmBt.setActionCommand("atm");
atmBt.addActionListener(this);
infoBt = new JButton("Информация о счетах");
infoBt.setBounds(165, 300, 170, 25);
infoBt.setActionCommand("getInfo");
infoBt.addActionListener(this);
nameBank = new JLabel("Нае(БАНК)");
nameBank.setBounds(225, 100, 400, 30);
//Создащая счета панель
crBtBack = new JButton("Назад");
crBtBack.setBounds(165, 400, 170, 25);
crBtBack.setActionCommand("crBack");
crBtBack.addActionListener(this);
nameScore = new JTextField("", 10);
nameScore.setBounds(165, 150, 170, 25);
moneyScore = new JTextField("", 10);
moneyScore.setBounds(165, 200, 170, 25);
PlainDocument doc = (PlainDocument) moneyScore.getDocument();
doc.setDocumentFilter(new DigitFilter());
crFinal = new JButton("Создать");
crFinal.setBounds(165, 300, 170, 25);
crFinal.setActionCommand("crFinal");
crFinal.addActionListener(this);
nameSScore = new JLabel("Имя счета");
nameSScore.setBounds(225, 125, 400, 30);
nameMScore = new JLabel("Стартовый капитал");
nameMScore.setBounds(200, 175, 400, 30);
//Закрывающая панель счета
clBtBack = new JButton("Назад");
clBtBack.setBounds(165, 400, 170, 25);
clBtBack.setActionCommand("clBack");
clBtBack.addActionListener(this);
clFinal = new JButton("Закрыть счет");
clFinal.setBounds(165, 300, 170, 25);
clFinal.setActionCommand("clFinal");
clFinal.addActionListener(this);
idScore = new JTextField("", 10);
idScore.setBounds(165, 200, 170, 25);
PlainDocument doc1 = (PlainDocument) idScore.getDocument();
doc1.setDocumentFilter(new DigitFilter());
unicId = new JLabel("Уникальный идентификатор");
unicId.setBounds(165, 175, 400, 30);
//Панель банкомата
setSize(500, 500);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setResizable(false);
getContentPane().add(mainPanel);
mainPanel.add(crBt);
mainPanel.add(clBt);
mainPanel.add(atmBt);
mainPanel.add(infoBt);
mainPanel.add(nameBank);
setVisible(true);
getContentPane().add(createPanel);
createPanel.add(crBtBack);
createPanel.add(nameScore);
createPanel.add(moneyScore);
createPanel.add(crFinal);
createPanel.add(nameSScore);
createPanel.add(nameMScore);
setVisible(true);
getContentPane().add(closePanel);
closePanel.add(clFinal);
closePanel.add(clBtBack);
closePanel.add(idScore);
closePanel.add(unicId);
setVisible(true);
getContentPane().add(atmPanel);
setVisible(true);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if ("createScore".equals(e.getActionCommand())) {
createPanel.setVisible(true);
mainPanel.setVisible(false);
closePanel.setVisible(false);
atmPanel.setVisible(false);
}
if ("crBack".equals(e.getActionCommand())) {
createPanel.setVisible(false);
mainPanel.setVisible(true);
closePanel.setVisible(false);
atmPanel.setVisible(false);
}
if ("closeScore".equals(e.getActionCommand())) {
createPanel.setVisible(false);
mainPanel.setVisible(false);
closePanel.setVisible(true);
atmPanel.setVisible(false);
}
if ("clBack".equals(e.getActionCommand())) {
createPanel.setVisible(false);
mainPanel.setVisible(true);
closePanel.setVisible(false);
atmPanel.setVisible(false);
}
if ("crFinal".equals(e.getActionCommand())) {
try (FileWriter writer = new FileWriter("src/BankAccount.txt", true)) {
writer.write(nameScore.getText() + ":" + moneyScore.getText() + ":" + (strLeight() + 1) + "-\n");
writer.flush();
JOptionPane.showMessageDialog(MainWindow.this,
"Ваш счет " + nameScore.getText() + " создан с балансом " + moneyScore.getText() + "\nЕго уникальный идентификатор " + strLeight() + "\nСпасибо что используете наш банк", "Создание счета"
, JOptionPane.INFORMATION_MESSAGE);
nameScore.setText("");
moneyScore.setText("0");
} catch (IOException ex) {
System.out.println(ex.getMessage());
}
}
if ("getInfo".equals(e.getActionCommand())) {
JOptionPane.showMessageDialog(MainWindow.this,
"Тут будут счета",
"Информация о счетах\n"
, JOptionPane.INFORMATION_MESSAGE);
}
if ("clFinal".equals(e.getActionCommand())) {
try (FileWriter writer = new FileWriter("src/BankAccount.txt", true)) {
System.out.println(idScore.getText());
} catch (IOException ex) {
System.out.println(ex.getMessage());
}
}
}
public static void main(String[] args) {
new MainWindow();
}
public static int strLeight() {
try {
File myFile = new File("src/BankAccount.txt");
FileReader fileReader = new FileReader(myFile);
LineNumberReader lineNumberReader = new LineNumberReader(fileReader);
int lineNumber = 0;
while (lineNumberReader.readLine() != null) {
lineNumber++;
}
lineNumberReader.close();
return lineNumber;
} catch (IOException e) {
e.printStackTrace();
}
return 0;
}
static class DigitFilter extends DocumentFilter {
private static final String DIGITS = "\\d+";
@Override
public void insertString(FilterBypass fb, int offset, String string, AttributeSet attr) throws BadLocationException {
if (string.matches(DIGITS)) {
super.insertString(fb, offset, string, attr);
}
}
@Override
public void replace(FilterBypass fb, int offset, int length, String string, AttributeSet attrs) throws BadLocationException {
if (string.matches(DIGITS)) {
super.replace(fb, offset, length, string, attrs);
}
}
}
}
Желательно что бы работал в этом куске кода
if ("clFinal".equals(e.getActionCommand())) {
try (FileWriter writer = new FileWriter("src/BankAccount.txt", true)) {
// Вот здесь
} catch (IOException ex) {
System.out.println(ex.getMessage());
}
}
BankAccount.tmp
, записать в него только нужные строки изBankAccount.txt
, переименоватьBankAccount.txt
вBankAccount.bak
, переименоватьBankAccount.tmp
вBankAccount.txt