how to create new java.io.File in memory?
How can I create new File (from java.io) in memory , not in the hard disk?
I use java language. I don't want to save file in hard drive.
To write to a stream, in memory, use:
new ByteArrayOutputStream();
Maybe you are confusing File
and Stream
:
- A
File
is an abstract representation of file and directory pathnames. Using aFile
object, you can access the file metadata in a file system, and perform some operations on files on this filesystem, like delete or create the file. But theFile
class does not provide methods to read and write the file contents. - To read and write from a file, you are using a
Stream
object, likeFileInputStream
orFileOutputStream
. These streams can be created from aFile
object and then be used to read from and write to the file.
You can create a stream based on a byte buffer which resides in memory, by using a ByteArrayInputStream
and a ByteArrayOutputStream
to read from and write to a byte buffer in a similar way you read and write from a file. The byte
array contains the "File's" content. You do not need a File
object then.
Both the File...
and the ByteArray...
streams inherit from java.io.OutputStream
and java.io.InputStream
, respectively, so that you can use the common superclass to hide whether you are reading from a file or from a byte array.
source - http://stackoverflow.com/questions/17595091/how-to-create-new-java-io-file-in-memory
'Development > Java' 카테고리의 다른 글
java - json-simple parse (0) | 2014.08.19 |
---|---|
java - URLConnection, cookie, session (0) | 2014.04.17 |
java - local host ip address (0) | 2014.02.13 |
java - HttpURLConnection (0) | 2014.02.07 |
java - 화폐 원 단위 콤마 표시 (2) | 2013.10.02 |