java FileOutputStream 与 OutputStream,为什么以及何时?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30083921/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
FileOutputStream vs OutputStream, why and when?
提问by Lauro182
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:
所以,你可以通过几种方法来保存表单文件,我想,我使用了 2,但我从来不知道什么时候使用哪个。我有这 2 段代码做同样的事情:
1-This writes my form file to specified path.
1-这将我的表单文件写入指定的路径。
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.
2-这也可以。
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?
所以,我的问题是,它们 2 之间有什么区别,我什么时候应该使用哪个?
回答by Ivan1211
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) 你的例子中创建的两个OutputStream对象有区别吗?并且 (2) 两种 Object 类型之间是否有区别,应该何时使用?
(1)
(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.
由于 FileObjectStream 是抽象 ObjectStream 的子类,因此可以使用子类构造函数创建 Object,并将继承所有子类功能。
(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.
(2) OutputStream 是一个抽象类,它应该被一个 Object子类化,例如 FileOutputStream,以便write(byte[] b)
可以覆盖调用并且可以添加任何特定于输出接收器的功能。更简单地说,FileOutputStream(以及类似它的 OutputStream 的子类)“是一个”OutputStrem 对象,但 OutputStream 不是一个 FileOutputStream 对象。
回答by Ewebs
Outputstream is an abstract class whereas FileOutputStream is the child class.
Outputstream 是一个抽象类,而 FileOutputStream 是子类。
It's possible that you could use Outputstream to just write in bytes for a prexisting file instead of outputting a file.
您可能可以使用 Outputstream 仅以字节为单位写入先前存在的文件,而不是输出文件。
If your just writing a file normally FileOutputStream would be the way to go
如果您只是正常写入文件 FileOutputStream 将是要走的路
回答by Bitcoin M
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.
没有区别。然而,第一个例子更容易编程。实际上,第二个示例仍在使用FileOutputStream
,只是您将其放入OutputStream
变量中。
回答by Peter Lawrey
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.
如果您有一个必须引用的变量,请FileOutputStream
使用该变量。这意味着您不能OutputStream
出于某种充分的理由使用其他的。
However, if there is no good reason to be more specific than OutputStream
this is what you should use.
但是,如果没有比OutputStream
这更具体的充分理由,那么您应该使用它。
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.
假设有人稍后必须更改此代码,并且需要将 OutputStream 的类型更改为 aBufferedOutputStream
或GZIPOutputStream
。然而,代码说它一定是一个FileOutputStream
为什么?,好吧,没有充分的理由。试图找到不存在的东西需要更长的时间。
In short, make the type of reference match what it has to be, not what it happens to be now.
简而言之,使引用的类型与其必须匹配,而不是现在的样子。
回答by eatinasandwich
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.
根据 java 文档,OutputStream 只是 FileOutputStream 继承的抽象类。此外,您在两个示例中使用它的方式并没有真正显示出实际差异,因为这两种情况下的实例仍然是 FileOutputStream。第二种情况只允许您多态地使用变量,因此如果您不知道在循环的特定迭代中可能需要哪种类型的输出流,您可以使用抽象类键入变量并将其实例化为您的任何子类需要。