Lauro182
Lauro182

Reputation: 1697

FileOutputStream vs OutputStream, why and when?

So, you can save a form file through several methods, I guess, I use 2, but I never really know when to use which. I have these 2 pieces of codes that do the same:

1-This writes my form file to specified path.

FormFile archivo = myForm.getArchivo();
File newFile = new File(path, archivo.getFileName());
FileOutputStream fos = new FileOutputStream(newFile);
fos.write(archivo.getFileData());
fos.flush();
fos.close();

2-This does too.

FormFile archivo = myForm.getArchivo();
InputStream in = archivo.getInputStream();
OutputStream bos = new FileOutputStream(path + "archivo.ext");

int byteRead = 0;
byte[] buffer = new byte[8192];
while ((byteRead = in.read(buffer, 0, 8192)) != -1) {
     bos.write(buffer, 0, byteRead);
}
bos.close();
in.close();

So, my question here is, what's the difference between the 2 of them, and when should I use which?

Upvotes: 1

Views: 18535

Answers (6)

Richie Rizal Amir
Richie Rizal Amir

Reputation: 865

FileOutputStream implemented finalize method. Which means that objects created will likely hang around until a full garbage collection occurs, which will leave excessive garbage on the heap for longer, and potentially much longer than expected. Whereas outputstream and Inputstream that do not have this concern.

Upvotes: 0

Ewebs
Ewebs

Reputation: 11

Outputstream is an abstract class whereas FileOutputStream is the child class.

It's possible that you could use Outputstream to just write in bytes for a prexisting file instead of outputting a file.

If your just writing a file normally FileOutputStream would be the way to go

Upvotes: 0

Ivan1211
Ivan1211

Reputation: 181

So, depending on the question you are trying to ask, there are two different answers here. (1) Is there a difference between the two OutputStream objects that have been created in your example? And (2) is there a difference between the two Object types and when should each be used?

(1)

ObjectStream os = new FileObjectStream(File file);

and

FileObjectStream fos = new FileObjectStream(File file);

are no different.

Because FileObjectStream is a subclass of the abstract ObjectStream, the Object can be created using the subclass constructor and will inherit all subclass functionality.

(2) OutputStream is an abstract class which should be sub classed, by an Object such as FileOutputStream, so that the write(byte[] b) call may be overridden and any functionality specific to the output sink may be added. More simply, FileOutputStream (and subclasses of OutputStream like it) "is a" OutputStrem object, but OutputStream is not a FileOutputStream object.

Upvotes: 6

eatinasandwich
eatinasandwich

Reputation: 636

According to the java docs, OutputStream is just the abstract class that FileOutputStream inherits from. Also, the way you are using it in your two examples doesn't really show a practical difference because the instance in both cases is still a FileOutputStream. The second case just allows you to use the variable polymorphically so if you didn't know what type of output stream you would need on perhaps a particular iteration of a loop you could type the variable with the abstract class and instantiate it to whichever subclass you needed.

Upvotes: 0

Peter Lawrey
Peter Lawrey

Reputation: 533870

You should code deliberately and be no more specific than you actually need.

If you have a variable which must refer to a FileOutputStream use that. It means you can't use some other OutputStream for some good reason.

However, if there is no good reason to be more specific than OutputStream this is what you should use.

Say someone has to change this code later and need to change the type of OutputStream e.g. to a BufferedOutputStream or GZIPOutputStream. However, the code says it must be a FileOutputStream why?, well there is no good reason. Trying to find something which is not there takes a lot longer.

In short, make the type of reference match what it has to be, not what it happens to be now.

Upvotes: 0

Bitcoin M
Bitcoin M

Reputation: 1206

There is no difference. However, the first example is a lot easier to program with. In fact, the second example is still using FileOutputStream, except you are putting it in an OutputStream variable.

Upvotes: 0

Related Questions