简体   繁体   中英

Java - StringTokenizer

Disclaimer: I just learnt how to do this yesterday, and I am desperate

I have 3 classes ( Student , ClassSchedule , ClassEnrolment )

public class ClassEnrolment {
    ClassSchedule cs;
    Student stud;

    public ClassEnrolment(ClassSchedule cs, Student stud) {
        super();
        this.cs = cs;
        this.stud = stud;
     }
}

-----

public class Student{
    private String name;
    private String matricNo;
    ClassEnrolment classroom;
    ClassSchedule schedule;
}

-----

public class ClassSchedule {
    String classType;
    int index;
    int group;
    String day;
    String time;
    String venue;
    String remark;
    String courseCode;
}   

I am trying to read/write a text file (database). I am having issues with 3 lines inside "/////////////////" .

I am aware that the attributes declared in ClassEnrolment are not int nor string. How should I do this? How do I bring ClassSchedule and Student as part of StringTokenizer ?

The textfile stores index from ClassSchedule and matricNo from Student . I have a feeling I am doing this wrongly.

    public static ArrayList readEnrolment(String filename) throws IOException {
    ArrayList stringArray = (ArrayList) read(filename);
    ArrayList alr = new ArrayList();

    for (int i = 0; i < stringArray.size(); i++) {
        String st = (String) stringArray.get(i);
        StringTokenizer star = new StringTokenizer(st, SEPARATOR);

        ///////////////////////////////////////////////////
        int cs = Integer.parseInt(star.nextToken().trim());
        String stud = star.nextToken().trim();
        ClassEnrolment enrolment = new ClassEnrolment(cs, stud);
        //////////////////////////////////////////////////
        alr.add(enrolment);
    }
    return alr;
}

public static void saveEnrolment(String filename, List al) throws IOException {
    List alw = new ArrayList();

    for (int i = 0; i < al.size(); i++) {
        ClassEnrolment enrolment = (ClassEnrolment) al.get(i);
        StringBuilder st = new StringBuilder();
        st.append(enrolment.getCs2());
        st.append(SEPARATOR);
        st.append(enrolment.getStud2());
        alw.add(st.toString());
    }
    write(filename, alw);
}

Your problem here is an issue of Serialization . You could very well declare your classEnrolment as Serializable (do not forget the serialUUID) and let the classic java serialization process to write your objects as bytes in your file.

However, what you are trying here is to conceive a custom serializer for your class. I'd advise you to use generics in the likes of :

public interface Serializer<T> {

    byte[] write(T objectToSerialize);

    //to build a factory/service around it
    boolean canDeserialize(byte[] serializedObject);

    T read(byte[] serializedObject);
}

So that your methods read/writeEnrollment will simply be creating a Serializer<ClassEnrollment> and letting it do its job (which will probably create a Serializer<Student> and a Serializer<ClassSchedule> ) .

For the matter of the Serialization process, I have to say that the StringTokenizer is a legacy class from JDK 1 whose use is discouraged by its own javadoc :

StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead.

And if you want to Serialize objects of potentially different classes than just your ClassEnrollment, it would be nice to be able to distinguish the class used. There are many ways to do it, and as one commenter said, json serialization would fare very well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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