1

I have a text file “test.txt” in which I store student details such as I.D number and course taken. I want new students to be added immediately before the line that with “LASTSTUDENTEENTRYLINE”. The problem I have is that I’m collecting the student details in an array list called “objectInputFieldsList”. I’ve been trying to replace “LASTSTUDENTEENTRYLINE” with “objectInputFieldsList” + “LASTSTUDENTEENTRYLINE”. The problem is that the two don’t mix – one is an array list and the other a string. The array list contains input from text field values from users. How can I approach this successfully and replace LASTSTUDENTEENTRYLINE with “objectInputFieldsList” + “LASTSTUDENTEENTRYLINE” ? Thank you all so much in advance.

Here’s what I’ve been trying so far:

Sample text file before update:

123 | Oliver | Muchai
456 | Revilo | Chamu
LASTSTUDENTEENTRYLINENNAMES

Classes | 123 | English
Classes | 456 | Bilogy
LASTSTUDENTEENTRYLINENSUBJECTS

After an update with a new user the text file should look as:

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

Classes | 123 | English
Classes | 456 | Biology
Classes | 678 | Kiswahili
LASTSTUDENTEENTRYLINENSUBJECTS

The Code:

        // 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);
4

3 回答 3

1

这不是一个完整或完整的解决方案,而是一个概念的示例

这假设您从一开始就阅读所有文本......如果您不这样做,生活会更加困难。

这基本上用于查找存在特定文字StringBuilder#indexOf的索引点。String在这个例子中,我们只是在寻找LASTSTUDENTEENTRYLINENNAMES,但应该明白......

从那里,对于每个新记录行,我们创建一个String要插入的临时文件,然后将其插入到之前获得的索引点之前......

现在。加入科目也不是什么难事...

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

这输出...

Before
123 | Oliver | Muchai
456 | Revilo | Chamu
LASTSTUDENTEENTRYLINENNAMES

Classes | 123 | English
Classes | 456 | Bilogy
LASTSTUDENTEENTRYLINENSUBJECTS

After
123 | Oliver | Muchai
456 | Revilo | Chamu
128 | Banana | Pajamas
LASTSTUDENTEENTRYLINENNAMES

Classes | 123 | English
Classes | 456 | Bilogy
LASTSTUDENTEENTRYLINENSUBJECTS

现在......已经完成了所有这些......

我建议使用单个用户数据库,例如H2,甚至是 XML,这在长期错误中必须变得更加容易并且更少混乱 - 恕我直言

于 2013-08-12T08:06:25.277 回答
1

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

用作index包含行尾“lastName”的元素之前的索引。

如果您想在ArrayList.

于 2013-08-12T07:48:29.943 回答
0

如果我理解正确,您想在“LATSTUDENTTEENTRYLINE”之前将新学生写入您的“test.txt”。

首先将您的整个 txt 放入 StringBuffer 中,因为您可以在特殊索引处写入。

利用

sb.getIndexOf("LASTSTUDENTEENTRYLINE");

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

String line = "Student | foo | bar";
int index = line.length();

现在你必须在

sb.getIndexOf("LASTSTUDENTTEENTRYLINE") - index;

在你的字符串缓冲区中。最后,您将整个 StringBuffer 写入 txt。也许你必须排队

+ "\n"

制作一个新行,使其在 txt 中看起来更好

于 2013-08-12T08:15:12.377 回答