使用JTextArea和JButton修改一个txt文件



我对java还比较陌生,我正在尝试创建一个程序,从txt文件中读取关于不同歌曲的一系列信息(位置、标题、艺术家、标签),并允许用户直接通过程序中包含的JTextArea修改文件(按下JButton后)。我能够确保程序正确识别文件中包含的所有不同信息(事实上,我可以手动添加更多歌曲而没有任何问题),但我不知道如何允许用户从JTextArea修改文件。

文件内容:

1;十、艾德·希兰;庇护;

2;在孤独的时刻;萨姆·史密斯;国会大厦;

3;从未变得更好;OLLY-MURS;史诗;

4;四;单向;悬铃木;

5;在航行中被通缉;乔治·埃兹拉;哥伦比亚;

CD面板:

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 + ";";
}
}

提前感谢您的帮助,如果我的代码(和英语)不完美,也很抱歉,但是,我是这个世界的新手,我正在努力尽快提高我的知识:)

您应该使用JTable或信任用户在TextArea上修改它;

ActionListener添加到JButton中,并使用myTextArea.write()写入cd.dat文件。

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

相关内容

最新更新