I have following task: to make application for uploading data into FTP server using apache.commons.net.FTPClient, etc. There is method client.storeFile() which allows to upload file (it requires InputStream as 1 of method parameters). But I want to make txt file into server which should contain array of strings - array is variable of program. Should I make file, put strings into it and transfer this file into method, or there is stream which allow to store strings in memory without making file? Thank you, anyway.
2 Answers
There is ByteArrayOutputStream that you can use
4 Comments
user1134602
Thank you, I can store my strings into BAOS. But how should I get InputStream from ByteArrayOutputStream?
fasseg
with a
ByteArrayInputStream as in new ByteArrayInputStream(baos.toByteArray())fortran
I think you should use directly the
ByteArrayInputStream with String.getBytesuser1134602
1 need 1 think also - how can I transfer string into next line? If I use files I can use writeLine(), but I don't know how can I do it for bytes.
You can use StringBufferInputStream, but as you have an array of Strings, you might need to concatenate all of them first, or use a SequenceInputStream generating all the sub input streams...
I'd go for the second, combined with a lazy enumeration, if your array is large.
By the way, StringBufferInputStream is deprecated, so might want to use the ByteArrayInputStream and String.getBytes(...) methods instead.