FileOutputStream append issue

D

Dustin

Greetings

I am trying to append a few files using FileOutputStream (Java 1.4) and
it does not seem to be working correctly. My code is similar to as
follows:

File file1 = new File("some file path");
byte[] byteArray1 = getByteArray(file1);

File file2 = new File("some other file path");
byte[] byteArray2 = getByteArray(file2);

FileOutputStream os = new FileOutputStream("output path", true);

os.write(byteArray1);
os.flush();
os.write(byteArray2);
os.flush();
os.close();

I then go and try to open this file but it only has the content of
file2 (the second file written). It appears that it is not appending
but just writing.

Is there something which I am doing wrong here?

Thanks for your time and help.
 
D

Daniel Pitts

Dustin said:
Greetings

I am trying to append a few files using FileOutputStream (Java 1.4) and
it does not seem to be working correctly. My code is similar to as
follows:
[useless example removed...]
I then go and try to open this file but it only has the content of
file2 (the second file written). It appears that it is not appending
but just writing.

Is there something which I am doing wrong here?
Yeah, you should post an sscce <http://physci.org/codes/sscce/> so that
we can look at exactly what your doing, and possible reproduce the
problem ourselves.

Instead of giving us code that is "similar to" your code, write working
code that has the same issues that your original code does. that way we
can help you much more effectively.
Thanks for your time and help.
You're welcome,

Daniel.
 
D

Dustin

Daniel

Thank you for your reply. The thing is that I cannot submit this exact
code; it is proprietary.

The example I gave though is basically all I am doing. I am taking two
files and make them byte[]. I then try to write these to a
FileOutputStream. I do the creation of the byte[] before creating the
FOS because I am trying to write them back to the destination of the
first file.

I hope this helps.
Dustin
 
D

Daniel Pitts

Dustin said:
Daniel

Thank you for your reply. The thing is that I cannot submit this exact
code; it is proprietary.

The example I gave though is basically all I am doing. I am taking two
files and make them byte[]. I then try to write these to a
FileOutputStream. I do the creation of the byte[] before creating the
FOS because I am trying to write them back to the destination of the
first file.

I hope this helps.
Dustin
Did you even read my message?
Give us code that recreates your problem. It doesn't have to be the
exact same code as your proprietary code, but it should suffer the same
issues.

Also, is broken code worth being proprietary? :)
The following works for me, I'm guessing it is some other problem with
your code.

<sscce>
import java.io.*;

public class FileOutputStreamTest {
public static void main(String...args) throws IOException {
File file = new
File("net.virtualinfinity.FileOutputStreamText.dat");
file.delete();
FileOutputStream fileOutputStream = new FileOutputStream(file,
true);
fileOutputStream.write(new byte[] {1, 2, 3, 4, 5});
fileOutputStream.flush();
fileOutputStream.write(new byte[] {6, 7, 8, 9, 10});
fileOutputStream.flush();
fileOutputStream.close();
FileInputStream inputStream = new FileInputStream(file);
byte[] input = new byte[inputStream.available()];
inputStream.read(input);
if (input.length != 10) {
System.out.println("Expected 10 bytes, but got " +
input.length);
} else {
for (int i = 0; i < 10; ++i) {
if (input != i + 1) {
System.out.println("Data is incorrect at location "
+ i);
}
}
}

}
}
</sscce>
 
P

Patricia Shanahan

Dustin said:
Daniel

Thank you for your reply. The thing is that I cannot submit this exact
code; it is proprietary.

The example I gave though is basically all I am doing. I am taking two
files and make them byte[]. I then try to write these to a
FileOutputStream. I do the creation of the byte[] before creating the
FOS because I am trying to write them back to the destination of the
first file.

I hope this helps.
Dustin

Unfortunately, "basically all I am doing" does not usually work. The
details that seem insignificant, or are even totally invisible, to the
programmer are exactly the details where bugs lurk.

Producing a self-contained example is an important debug step, even if
you are not using a newsgroup. If the problem really is where you think
it is, you should be able to work up your code snippets into a running,
but failing, example program in a few minutes. If it isn't, you need to
know that.

It is quite likely that you will find the bug yourself in the course of
producing the example. However, once you have a short failing program
you can always edit identifiers to disguise what you are really doing
and post the result.

Patricia
 
C

Chris Uppal

Daniel said:
byte[] input = new byte[inputStream.available()];
inputStream.read(input);

You shouldn't ever do that, especially not in example code posted where
beginners will see it. If you ignore the return value from read() then your
code is broken.

(Apart from that side note, I agree with you -- there is nothing obviously
wrong with the OP's approach so the problem must be somewhere in the code he
declines to show us).

-- chris
 
D

Daniel Pitts

Chris said:
Daniel said:
byte[] input = new byte[inputStream.available()];
inputStream.read(input);

You shouldn't ever do that, especially not in example code posted where
beginners will see it. If you ignore the return value from read() then your
code is broken.

(Apart from that side note, I agree with you -- there is nothing obviously
wrong with the OP's approach so the problem must be somewhere in the code he
declines to show us).

-- chris

Thanks for the note. I don't do much IO in java, so I'm not used to
all the caveats.
 
M

Mark Thornton

Chris said:
Daniel Pitts wrote:

byte[] input = new byte[inputStream.available()];
inputStream.read(input);


You shouldn't ever do that, especially not in example code posted where
beginners will see it. If you ignore the return value from read() then your
code is broken.

(Apart from that side note, I agree with you -- there is nothing obviously
wrong with the OP's approach so the problem must be somewhere in the code he
declines to show us).

The other problem is the definition of available():

"Returns an estimate of the number of bytes that can be read (or skipped
over) from this input stream without blocking by the next invocation of
a method for this input stream. The next invocation might be the same
thread or another thread. A single read or skip of this many bytes will
not block, but may read or skip fewer bytes.
Note that while some implementations of InputStream will return the
total number of bytes in the stream, many will not. It is never correct
to use the return value of this method to allocate a buffer intended to
hold all data in this stream"

Note particularly the last sentence.

Mark Thornton
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top