Search code examples
javafilereaderfilewriter

write data into cache instead of disk on socket


I know "fileReader" and "fileWriter" can read and write files from the disk transfer between the socket. However, I want to receive file from socket and write them to cache instead of disk.

Is there a function that can do that?


Solution

  • Assuming you're talking about keeping data in memory, you can use ByteArrayOutputStream and ByteArrayInputStream for in-memory IO. You can then wrap these in an OutputStreamWriter and InputStreamReader respectively, for text-based IO.

    (I'd actually suggest using OutputStreamWriter and InputStreamReader wrapped around FileOutputStream and FileInputStream for file-based text IO too, as then you can specify the encoding you want to use.)