简体   繁体   中英

Modify a txt file using a JTextArea and a JButton

I'm relatively new to java and I'm trying to create a program that reads a series of information about different songs from a txt file (position, title, artist, label) and allows the user to modify the file directly through the JTextArea contained in the program (after pressing a JButton). I was able to ensure the correct recognition from the program of all the different information contained in the file (in fact i can manually add more songs without any problem) but I don't know how to allow the user to modify the file from the JTextArea.

Content of the file:

1;X;ED SHEERAN;ASYLUM;

2;IN THE LONELY HOUR;SAM SMITH;CAPITOL;

3;NEVER BEEN BETTER;OLLY MURS;EPIC;

4;FOUR;ONE DIRECTION;SYCO MUSIC;

5;WANTED ON VOYAGE;GEORGE EZRA;COLUMBIA;

CD Panel:

import java.io.*;
import java.util.*;
import javax.swing.*;

public class CDPanel {

private static String newLine = "\n";
public static CD myCD;
public static JTextArea myTextArea = new JTextArea(15, 30);

public static void main(String[] args) throws FileNotFoundException, IOException {

    List<Integer> position = new ArrayList<>();
    List<String> title = new ArrayList<>();
    List<String> artist = new ArrayList<>();
    List<String> label = new ArrayList<>();

    JButton addCD = new JButton("Press to add the CD");

    JFrame frame = new JFrame("CD List");
    JPanel panel = new JPanel();

    int numberOfLines = 0;

    BufferedReader br = new BufferedReader(new FileReader("cd.dat"));
    String line = null;
    while ((line = br.readLine()) != null) {
        String data[] = line.split(";");

        for (int i=0; i<4; i++) {
            if (i == 0) {
                int value = Integer.parseInt(data[i]);
                position.add(value);
            }

            if (i == 1) {
                title.add(data[i]);
            }

            if (i == 2) {
                artist.add(data[i]);
            }

            if (i == 3) {
                label.add(data[i]);
            }
        }
        numberOfLines++;
    }

    for (int i=0; i<numberOfLines; i++) {
        myCD = new CD(position.get(i), title.get(i), artist.get(i), label.get(i));
        myTextArea.append(String.valueOf(myCD + newLine));
    }
    panel.add(myTextArea);
    panel.add(addCD);
    frame.add(panel);
    frame.setSize(30, 15);
    frame.pack();
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

CD:

public class CD {

public int position;
public String title, artist, label;

public CD (int positionInit, String titleInit, String artistInit, String labelInit) {
    position = positionInit;
    title = titleInit;
    artist = artistInit;
    label = labelInit;
}

public String toString() {
return position + ";" + title + ";" + artist + ";" + label + ";";
}
}

Thank you in advance for your help and sorry if my code (and my English) is not perfect, but again, I'm new to this world and I'm trying to improve my knowledge as fast as I can :)

You should use JTable or trust user to modify it on TextArea then;

Add ActionListener to your JButton and use myTextArea.write() to write into cd.dat file.

JButton addCD = new JButton("Press to add the CD");
addCD.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {

            FileWriter fw = null;
            try {
                fw = new FileWriter("cd.dat");
                myTextArea.write(fw);
            } catch (IOException e1) {
                e1.printStackTrace();
            } finally {
                try {
                    fw.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        }
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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