Java 如何用用户输入的ArrayList替换文本文件中的文本字符串

Java 如何用用户输入的ArrayList替换文本文件中的文本字符串,java,string,arraylist,Java,String,Arraylist,我有一个文本文件“test.txt”,其中存储了学生的详细信息,如身份证号码和所学课程。 我希望在“LastStudenteryLine”行之前立即添加新学生。 我遇到的问题是,我正在收集名为“objectInputFieldsList”的数组列表中的学生详细信息。我一直试图用“objectInputFieldsList”+“LastStudentyLine”替换“LastStudentyLine”。 问题是这两个元素不能混合使用——一个是数组列表,另一个是字符串。数组列表包含来自用户文本字段值

我有一个文本文件“test.txt”,其中存储了学生的详细信息,如身份证号码和所学课程。 我希望在“LastStudenteryLine”行之前立即添加新学生。 我遇到的问题是,我正在收集名为“objectInputFieldsList”的数组列表中的学生详细信息。我一直试图用“objectInputFieldsList”+“LastStudentyLine”替换“LastStudentyLine”。 问题是这两个元素不能混合使用——一个是数组列表,另一个是字符串。数组列表包含来自用户文本字段值的输入。 我如何才能成功地实现这一点,并将LastStudentRyline替换为“objectInputFieldsList”+“LastStudentRyline”? 提前非常感谢大家

以下是我到目前为止一直在尝试的:

更新前的示例文本文件:

123 | Oliver | Muchai
456 | Revilo | Chamu
LASTSTUDENTEENTRYLINENNAMES

Classes | 123 | English
Classes | 456 | Bilogy
LASTSTUDENTEENTRYLINENSUBJECTS
使用新用户更新后,文本文件应如下所示:

123 | Oliver | Muchai
456 | Revilo | Chamu
678 | Eddys | Rockery
LASTSTUDENTEENTRYLINENNAMES

Classes | 123 | English
Classes | 456 | Biology
Classes | 678 | Kiswahili
LASTSTUDENTEENTRYLINENSUBJECTS
守则:

        // The ArrayList called from the class that gets the user input from the JTextFields
        AddNewClientSaveAction save = new AddNewClientSaveAction();
        StringBuffer sb = new StringBuffer();


        String lastNames = "LASTSTUDENTEENTRYLINENNAMES ";
        String lastSubject = "LASTSTUDENTEENTRYLINENSUBJECTS";

        String textToEdit1 = lastNames;
        int cnt1 = sb.indexOf(textToEdit1);
        String replacementString1 = "\n" + save + "\n" + lastNames;
        sb.replace(cnt1,cnt1+textToEdit1.length(), replacementString1);

由于它是Java
ArrayList
,因此可以使用
ArrayList#add(int index,E element)
方法在列表中的指定位置插入元素

将包含行尾“lastName”的元素前面的索引用作
索引


如果您想在
ArrayList
中添加字符串,此解决方案不是一个完整或完整的解决方案,而是一个概念示例

这假设你从一开始就在阅读所有的文本……如果你不这样做,生活会变得更加困难

这基本上是使用
StringBuilder#indexOf
来查找存在特定
String
literal的索引点。在本例中,我们只查找
LastStudentEntryLineNames
,但应该了解

从这里开始,我们为每个新记录行创建一个要插入的临时
字符串
,然后将其插入到先前获得的索引点之前

现在。这也不是一件困难的事情,添加到主题中

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class FileUpdate {

    public static final String LAST_STUDENT_LINE = "LASTSTUDENTEENTRYLINENNAMES";

    public static void main(String[] args) {

        StringBuilder sb = new StringBuilder(128);
        List<Student> objectInputFieldsList = new ArrayList<>(25);
        objectInputFieldsList.add(new Student(128, "Banana", "Pajamas"));

        BufferedReader br = null;
        try {

            br = new BufferedReader(new FileReader("test.txt"));
            String text = null;
            while ((text = br.readLine()) != null) {
                if (sb.length() > 0) {
                    sb.append("\n");
                }
                sb.append(text);
            }

            System.out.println("Before");
            System.out.println(sb);

            for (Student s : objectInputFieldsList) {

                int insertIndex = sb.indexOf(LAST_STUDENT_LINE);
                StringBuilder line = new StringBuilder(128);
                line.append(s.getId()).append(" | ").append(s.getFirstName()).append(" | ").append(s.getLastName()).append("\n");
                sb.insert(insertIndex, line.toString());

                // Find subject mark and add the students subjects in as well...

            }

            System.out.println("\nAfter");
            System.out.println(sb);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                br.close();
            } catch (IOException exp) {
            }
        }

    }

    public static class Student {

        private int id;
        private String firstName;
        private String lastName;

        public Student(int id, String firstName, String lastName) {
            this.id = id;
            this.firstName = firstName;
            this.lastName = lastName;
        }

        public int getId() {
            return id;
        }

        public String getFirstName() {
            return firstName;
        }

        public String getLastName() {
            return lastName;
        }            
    }
}
现在…做了所有这些


我建议使用一个单用户数据库,比如说,或者甚至是XML,这在长期的错误中必须要简单得多,也不会太混乱——IMHO

如果我理解正确,您希望在“LatStudentEntryline”之前将新学生写入“test.txt”

首先把你的整个txt文件放到一个StringBuffer中,因为你不能在一个特殊的索引下写

使用

并获取要添加的字符串的大小

String line = "Student | foo | bar";
int index = line.length();
现在你必须在

sb.getIndexOf("LASTSTUDENTTEENTRYLINE") - index;
在您的字符串缓冲区中。 最后,将整个StringBuffer写入txt。 也许你得把话说清楚

+ "\n"

要创建新行,使其在txt中看起来更好,代码中的
sb
是什么?你能发布
sb.replace(…)
method吗@Azad我猜是
StringBuilder
…很抱歉:StringBuffer sb=new StringBuffer();获取where
laststudenterylinenames
的索引,使用
StringBuilder\insert
在此索引点之前插入…如果不想保留
laststudenterylinenames
,再次找到它的索引并使用
StringBuilder.delete
删除文本…什么是AddNewClientSaveAction?这是一个类,它从JTextFields收集用户输入,并通过上述代码将其放入ArrayList以存储在文本文件中(我排除了类中存在上述代码的其他部分)非常感谢你的回答,但是我认为你把关于示例文本文件的部分搞错了。这两部分显示了同一文本文件的前后部分。另外还有一行
128 | Banana | Pajamas
,它以2结尾,3行学生。我只是懒得添加类,不能为您做任何事情;)
sb.getIndexOf("LASTSTUDENTTEENTRYLINE") - index;
+ "\n"