gpt4 book ai didi

java - [修复和改进]通过按钮操作替换文件中的行

转载 作者:太空宇宙 更新时间:2023-11-04 07:45:23 24 4
gpt4 key购买 nike

有一个文件 Activites.dat,其格式如下:


Activity 名称
主管姓名
位置
付费?

例如数据

Sunday
Advanced Open Water Dive Certificate
Mr.Jones
N/A
Yes

Sunday
Sailing
Mr. Jackson
N/A
Yes

Sunday
Generation Next
Ms.Steele
N/A
Yes

现在,我在框架上有一个按钮,它从表格中显示当前所选 Activity 的所有信息,并且框架工作正常;所选 Activity 的所有数据均正确显示。

我现在需要做的是,当按下btnSave按钮时,保留原始值以与Activities.dat匹配以找出需要编辑/替换的数据,存储用户当前输入的数据,然后用新数据替换Activities.dat中的旧数据

例如想要的结果

初始

Sunday
Advanced Open Water Dive Certificate
Mr.Jones
N/A
Yes

Sunday
Sailing
Mr. Jackson
N/A
Yes

Sunday
Generation Next
Ms.Steele
N/A
Yes

编辑版本

Sunday
Advanced Open Water Dive Certificate
Mr.Jones
N/A
Yes

**Monday**
**Sailing Elite Course**
**Mr.Jonathan**
**HS5**
**No**

Sunday
Generation Next
Ms.Steele
N/A
Yes

我当前(不起作用)的代码

    private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {                                        

//ALL "original-" STRINGS ARE INITIALISED BEFORE

String nActivityName = txtActivityName.getText();
Object nDay = comboboxDay.getSelectedItem().toString();
String nSupervisor = txtSupervisor.getText();
String nLocation = txtLocation.getText();
String nPaid = null;

//Check which option is selected: Paid or Not Paid
if(radioYes.isSelected())
{
nPaid = radioYes.getText();
}
if(radioNo.isSelected())
{
nPaid = radioNo.getText();
}

File file = new File("Activities.dat");

try{
BufferedReader reader = new BufferedReader(new FileReader(file));
BufferedWriter writer = new BufferedWriter(new FileWriter(file, true));

while(reader.ready())
{
String RDay = reader.readLine();
String RActivityName = reader.readLine();
String RSupervisor = reader.readLine();
String RLocation = reader.readLine();
String RPaid = reader.readLine();
String nothing = reader.readLine();

//Match the exact same set of data as the original activity's data before edit.
if(RActivityName.equals(originalActivityName))
{
RDay.replace(RDay, (CharSequence) nDay);
// = nDay.toString();
RActivityName = nActivityName;
RSupervisor = nSupervisor;
RLocation = nLocation;
RPaid = nPaid;
nothing = nothing;
}
System.out.println(RDay + RActivityName + RSupervisor + RLocation + RPaid);
}


}
catch(IOException e)
{
Logger.getLogger(AddActivity.class.getName()).log(Level.SEVERE, null, e);
}

}

我可以存储原始值进行比较并获取新数据,但我不知道如何用 Activities.dat 中的新数据替换/编辑旧数据。我该怎么做?

是的,我确实知道我有一个 BufferedReader,但我没有使用它,因为我不知道在这种特定情况下在哪里或如何使用它。

最佳答案

这是执行“读/写整个文件”方法的一种方法:

  1. 创建一个封装单个 Activity 的类,这样您就可以在比一堆字符串更高的级别上实现逻辑:

    class Activity {
    String day;
    String activityName;
    String supervisorName;
    String location;
    String paid;
    }

    这是一个非常粗略的类(class)大纲。为了简洁起见,我没有包含正确的构造函数、getter、hashCode()equals()toString()

  2. 创建另一个类,仅涉及从文件读取和写入 Activity 。这再次使您的代码更易于理解。

    考虑一下您想从该类中得到什么,这些应该是调用者唯一可见的方法,例如

    /** Write activities to a file. */
    void writeActivities(File file, List<Activity> activities);

    /** Read activities from a file. */
    List<Activity> readActivities(File file);

    然后实现方法,例如

    public class ActivityManager {
    // Don't rely on te platform default, use a fixed charset.
    private final static Charset UTF_8 = Charset.forName("UTF-8");

    /** Write activities to a file. */
    void writeActivities(File file, List<Activity> activities)
    throws IOException {
    try (PrintWriter out = new PrintWriter(
    new OutputStreamWriter(new FileOutputStream(file), UTF_8))) {
    boolean first = true;
    for (Activity activity : activities) {
    if (!first) {
    // Print a newline after every activity.
    out.println();
    }
    writeActivity(out, activity);
    if (first) {
    first = false;
    }
    }
    }
    }

    private void writeActivity(PrintWriter out, Activity activity) {
    out.println(activity.day);
    out.println(activity.activityName);
    out.println(activity.supervisorName);
    out.println(activity.location);
    out.println(activity.paid);
    }

    /** Read activities from a file. */
    List<Activity> readActivities(File file) throws IOException {
    try (BufferedReader in = new BufferedReader(
    new InputStreamReader(new FileInputStream(file),
    UTF_8))) {
    List<Activity> result = new ArrayList<Activity>();
    while (true) {
    Activity activity = readActivity(in);
    result.add(activity);

    String line = in.readLine();
    if (line == null) {
    break; // Reached end of file.
    }
    if (!line.equals("")) {
    throw new IllegalStateException(
    "Expected empty line but got: <" + line + ">");
    }
    }
    return result;
    }
    }

    private Activity readActivity(BufferedReader in) throws IOException {
    Activity activity = new Activity();
    activity.day = in.readLine();
    activity.activityName = in.readLine();
    activity.supervisorName = in.readLine();
    activity.location = in.readLine();
    activity.paid = in.readLine();
    return activity;
    }
    }

    以上代码依赖try-with-resources feature在 Java 7 中引入。对于早期版本的 Java,您需要通过恢复为链接中显示的通常的 declare-try-finally-close 样式来重写两个 try block 。

  3. 从 UI 事件处理代码(如 actionPerformed() 方法)中,只需调用 ActivityManager 实例即可读取和写入 Activity ,并在 UI 组件和 Activity 类之间进行转换。

关于java - [修复和改进]通过按钮操作替换文件中的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15370976/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com