FileOutputStream()里可以省略new File(“路径”)?

重定向这里看到的
虽然引进了File类,但是却没有用File实例化,直接用FileOutputStream传入路径,这样可以么?
System.setOut( new PrintStream ( FileOutputStream("路径") ) )

如果可以,那之前IO这一章用File实例化的例子是不是都可以省略 new File("")?

没错

FileOutputStream
看这个的API文档,有详细说明。

Constructor Summary

FileOutputStream(File file)
Creates a file output stream to write to the file represented by
the specified File object.

FileOutputStream(File file,
boolean append)
Creates a file output stream to write to the file represented by
the specified File object.

FileOutputStream(FileDescriptor fdObj)
Creates an output file stream to write to the specified file
descriptor, which represents an existing connection to an actual
file in the file system.

FileOutputStream(String name)
Creates an output file stream to write to the file with the
specified name.

FileOutputStream(String name,
boolean append)
Creates an output file stream to write to the file with the specified
name.追问

那InputStream、FileWriter和FileReader都可以这么用?

追答

InputStream不行

具体的,还一下API文档的说明,或者在Eclipse中,new的时候,会提示

温馨提示:内容为网友见解,仅供参考
无其他回答
相似回答