[Solved] How can I store an object File that only exists in memory as a file inside of my storage system? [closed]

A java.io.File object does not represent a file. What it actually represents is a file system pathname; i.e. a name for an object in the file system that may or may not exist.

So your program is not “receiving” a file at all, and the only things that you could do with the object that you have received are save the pathname, or attempt to copy the file that the pathname denotes (or may denote). The latter will only work if the pathname is valid in the application’s local file system namespace, and it denotes a real file (or symlink to a file).

I also can’t just copy the file like here because the file I already know only exists in memory.

That does not make sense. A File cannot denote a file that only exists in memory (whatever you mean by that). If File::exists() returns true, the file is in the file system. If it returns false then the File does not denote an accessible file at all: it is not on disk, and not in memory.

Some operating systems support so-called “ram file systems” where the system’s physical or virtual memory is used to store files. But the files in such a file can be copied just like ordinary files.

Java 7 and later allow you to define your own in-memory “virtual” file systems using subclasses of java.nio.file.FileSystem. However File does not work with this approach. You use java.nio.file.Path objects instead.

Now it could be that what you are actually trying to say is that the File denotes a file that exists on a different machine … and that’s why you can’t copy it. If that is the problem, there is no simple solution either. One machine cannot just “reach out” and read arbitrary files on another machine’s hard drive. If this is what your problem is, you will need to either set up a remote file system mount, or use a file transfer mechanism.


solved How can I store an object File that only exists in memory as a file inside of my storage system? [closed]