PrintWriter does not add content in the correct order

I have a list containing objects (whose constructor contains another internal object). When I try to print the list to a file, I loop through each object and call the appropriate write methods.

public void writer(String file, boolean append) {
    File path = new File("../Opdracht6_2/src/" + file);
    try {
        PrintWriter write = new PrintWriter(new FileOutputStream(path,
                append));
        for (SuperObject o : this.list) {
            if (o instanceof Object1) {
                ((subObject1) w).writer1(file);
            }
            if (o instanceof Object2) {
                ((subObject3) w).writer2(file);

            }if (o instanceof Object3) {
                ((subObject3) w).writer3(file);

            }
        }
        write.close();
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

      

In the object write method, I try to first print a string that says what type it is and then call the write method on the inner object. After that I want the current arguments of the object to be printed and then back to the method for writing lists

public void writer1(String file) {
    File path = new File("../Opdracht6_2/src/" + file);
    try {
        PrintWriter write = new PrintWriter(
                new FileOutputStream(path, true));
        //below is the string I want to print before the innerobject appends 
        //its own arguments to the file
        write.append("String\r\n");
        this.innerobject.innerwriter();
        write.append(this objects arg);
        write.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

      

Internal object

public void innerwriter(String file) {
    File path = new File("../Opdracht6_2/src/" + file);
    try {
        PrintWriter write = new PrintWriter(
                new FileOutputStream(path, true));
        write.append(this objects arg);
        write.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

      

Now what actually happens is that the line I am trying to add first is added after the arguments to innerobject, even though I put it before the method that calls the innerobject. It looks like in the file:

internal arg objects

line

external objects arg

Can someone explain why?

+3


source to share


1 answer


Better not to use Writer

for every method here. Use one StringBuilder

to add content and pass it using methods. It is possible that it is Writer

incorrectly clearing the content in the order in which the content is added. In particular, the statement write.close()

internally innerwriter

will clear the contents of the inner object before "String\r\n"

actually being written Writer

to the calling method.

You can avoid creating multiple Writer

and use instead StringBuilder

:

// pass a StringBuilder to append
public void innerwriter(StringBuilder sb) {
    sb.append(this objects arg);
}

      



And when you are done with adding all the content, write it with Writer

just created once:

PrintWriter write = new PrintWriter(new FileOutputStream(path,
            append));
StringBuilder sb = new StringBuilder();
for (SuperObject o : this.list) {
    if (o instanceof Object1) {
        ((subObject1) w).writer1(sb);
    }
    if (o instanceof Object2) {
        ((subObject3) w).writer2(sb);

    } if (o instanceof Object3) {
        ((subObject3) w).writer3(sb);

    }
}

write.append(sb.toString());
write.close();

      

+2


source







All Articles