Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: i/o from jsp to a file (via creating an object)

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Thanked 0 Times in 0 Posts

    Unhappy i/o from jsp to a file (via creating an object)

    Hello everyone,

    I created 3 JSP pages: 1 - login, 2-displays 4 checkboxes with a submit, 3-displays the info submitted and also creates a file to store this information. I have a separate StudentElective.java where I created an object.
    page.3 jsp looks like this:

    <%  String knum = (String) session.getAttribute("knum");
        String mycourse = (String) session.getAttribute("course");
        String[] id = request.getParameterValues("id");%>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
            <title>Page 3</title>
            <h3>Thank you, <%=knum%>!</h3>
            You have selected
                int i;
                for (i = 0; i < id.length; i++) {
                    if (i == 0) {
                        out.print(" and ");
            Your application is now registered!
            <%  StudentElective se = new StudentElective();
                try {
                    File DATAFILE = new File("c:/Users/k000000/Documents/studentData.dat");
                    if (!DATAFILE.exists()) {DATAFILE.createNewFile();}
                    FileOutputStream DATA = new FileOutputStream(DATAFILE);
                    ObjectOutputStream SAVE = new ObjectOutputStream(DATA);
                } catch (Exception X) {
                    System.out.print("\n\tFile Access Error. Could not SAVE.");

    My object file:
    public class StudentElective implements java.io.Serializable {
        private String knum,
        private String[] elchoice;
        public StudentElective() {
        public StudentElective(String knum, String course, String[] elchoice) {
            this.knum = knum;
            this.course = course;
            this.elchoice = elchoice;
        public String getCourse() {
            return course;
        public String[] getElchoice() {
            return elchoice;
        public String getKnum() {
            return knum;
        public void setCourse(String course) {
            this.course = course;
        public void setElchoice(String[] elchoice) {
            this.elchoice = elchoice;
        public void setKnum(String knum) {
            this.knum = knum;
           public void print() {

    I have 2 questions:
    1. At the moment everytime I write new information into a file, my old info is gone.How can I modify my code so everytime I write a new info into a file it will be saved onto a new line. In other words, how can I open my file for multiple entries everytime new instance of object gets created?
    2.I made an error somewhere when I was trying to pass selected checkboxes to an array in my object. Everytime I read the info from file created it shows something like


    instead of the names of my checkboxes.Where I made this mistake and how to fix it?

    Thank you in advance for any help possible.

  2. #2
    Join Date
    Jun 2012
    Left Coast, USA
    My Mood
    Thanked 97 Times in 88 Posts

    Default Re: i/o from jsp to a file (via creating an object)

    Quote Originally Posted by Panoksa View Post
    1. At the moment everytime I write new information into a file, my old info is gone.How can I modify my code so everytime I write a new info into a file it will be saved onto a new line.
    Here's the thing: ObjectOutputStream objects are not designed to be appendable. Reference the ObjectOutputStream API

    I mean, you can open the underlying FileOutputStream in appendable mode, but when you create an ObjectOutputStream object the constructor immediately writes a new Serializable header to the stream, which results in a corrupted stream at that point (the end of the old file) when you append things and then, later, try to read in the new stuff.

    At least that what I observed. (I'm new to Java, and I would welcome any enlightenment here. It wouldn't be the first time I overlooked something that is "easy and obvious" to anyone with more than four months experience.)

    I keep thinking that they could have made the ObjectInputStream readObject() "smarter," to get past this, but maybe that wasn't a good idea either. Maybe there is a Good Reason why things are as they are.


    Maybe you can read all of the previous stuff from the file and then create a brand new file and ObjectOutputStream and write the old stuff and then the new stuff. You can write as many objects as you want in a single session with a single new file, right? (By packaging the objects in a collection, I'm thinking. Maybe an ArrayList or some such thing?)

    There is some discussion about a way to create appendable ObjectOutputStream objects is here: http://stackoverflow.com/questions/1...38141#12438141 I haven't tried it (because, for one thing, I couldn't think of a bullet-proof way of knowing how much stuff to read), but those guys always seem to be really on top of things.

    Quote Originally Posted by Panoksa View Post
    2....Everytime I read the info from file created it shows something like

    Your object's elchoice is an array of Strings. If you use something like System.out.println(arrayName) for any kind of array, you get the name of the array and a hashcode. To print the contents of an array in a single print statement, you can use the Arrays static toString() method:

    (You might want to change the print() method in your StudentElective class to something like this also.)


    Last edited by Zaphod_b; September 26th, 2012 at 03:53 PM.

  3. The Following User Says Thank You to Zaphod_b For This Useful Post:

    Panoksa (September 27th, 2012)

Similar Threads

  1. [SOLVED] Need some help with creating a new object
    By bankston13 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: August 28th, 2012, 04:31 PM
  2. Creating new Object Help!!!
    By kendraheartt in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 27th, 2012, 01:59 PM
  3. Creating object everytime object is called
    By aandcmedia in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 12th, 2012, 04:18 PM
  4. Creating and implementing class for creating a calendar object
    By kumalh in forum Object Oriented Programming
    Replies: 3
    Last Post: July 29th, 2011, 08:40 AM
  5. Creating an object...
    By RodePope4546 in forum Object Oriented Programming
    Replies: 6
    Last Post: June 27th, 2011, 06:44 AM

Tags for this Thread