简体   繁体   English

使用JTextArea和JButton修改txt文件

[英]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). 我是Java的新手,我正在尝试创建一个程序,该程序从txt文件(位置,标题,艺术家,标签)读取一系列有关不同歌曲的信息,并允许用户直接通过JTextArea修改文件包含在程序中(按下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. 我能够确保从程序中正确识别文件中包含的所有不同信息(实际上,我可以手动添加更多歌曲而没有任何问题),但是我不知道如何允许用户从文件中修改文件。 JTextArea中。

Content of the file: 文件内容:

1;X;ED SHEERAN;ASYLUM; 1; X; ED SHEERAN; ASYLUM;

2;IN THE LONELY HOUR;SAM SMITH;CAPITOL; 2;在寂寞的小时中; SAM SMITH; CAPITOL;

3;NEVER BEEN BETTER;OLLY MURS;EPIC; 3;从来没有更好; OLLY MURS;史诗;

4;FOUR;ONE DIRECTION;SYCO MUSIC; 4; 4;一个方向;西科音乐;

5;WANTED ON VOYAGE;GEORGE EZRA;COLUMBIA; 5;希望航行;乔治·埃兹拉;哥伦比亚;

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

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; 然后,您应该使用JTable或信任用户在TextArea对其进行修改;

Add ActionListener to your JButton and use myTextArea.write() to write into cd.dat file. 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();
                }
            }
        }
    });

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

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