【发布时间】:2013-01-08 07:13:35
【问题描述】:
我正在使用 JSCH 通过 SFTP 服务器将文档保存到网络驱动器。我有一个类似这样的方法来保存文档,它带有两个参数inputStream 和path to save the stream into a file which is a string.。
String message="I am saving this file to the shared drive";
channelSftp.put(new ByteArrayInputStream(message.getBytes()),pathFromChannel+"/"+"file.txt");
所以,在上面的代码中我直接使用了
1. new ByteArrayInputStream(message.getBytes())
而不是将其分配给某些 inputStream 即
2 .InputStream in=new ByteArrayInputStream(message.getBytes());
如果我做这样的事情,我可以通过 is.close(); 关闭这个 Stream。但是当我直接使用而不定义它时,如何关闭Inputstream 或outputStream?还是我不需要关闭它?
【问题讨论】:
-
为什么不把它赋值给一个变量,传入,然后关闭呢?另外,byteareaysyreams 不需要关闭
-
那么将流分配给某个变量而不是直接使用它是最佳实践吗?
标签: java inputstream outputstream jsch