2012-08-09 103 views
1

是否可以在修改屬性後將其轉換爲InputStream。將屬性轉換爲輸入流?

下面是一些代碼,以澄清問題:

sftpConnection = new connectSFTP(host, user, pass, port); 
Properties ssProperties = new Properties(); 
InputStream in = null; 
try{ 
    in = sftpConnection.download(fileName, fileDirectory); 
    ssProperties.load(in); 
    //System.out.println("File Found"); 
    ssProperties.setProperty(key, value); 
    sftpConnection.upload(<<Need the new InputStream here>>, fileDirectory); 
    in.close();  
} 
catch(Exception ex) 
{ 
System.out.println("File Not Found"); 
} 

回答

8

當然 - 最簡單的方法是創建一個ByteArrayOutputStream,保存到這一點,那麼創建一個圍繞結果的ByteArrayInputStream

ByteArrayOutputStream output = new ByteArrayOutputStream(); 
ssProperties.store(output, null); 
ByteArrayInputStream input = new ByteArrayInputStream(output.toByteArray()); 
sftpConnection.upload(input, fileDirectory);