I want to create a simple in-memory file system in Java, which has one root directory and is able to make new sub directory. In the directory we can make new files, write in them, read from them, delete them and also rename them. Can you please give some advice from where to start (a simple code, or resouce).
3 Answers
A custom file system provider must implement the java.nio.file.spi.FileSystemProvider class. A file system provider is identified by a URI scheme such as file, jar, memory, cd.
These links below provide good starting info
http://docs.oracle.com/javase/7/docs/technotes/guides/io/fsp/filesystemprovider.html
The link below(not about in memory file system) is about virtual file system. It talks about some design issues which could help you in case you decide to create your own file system.
http://www.flipcode.com/archives/Programming_a_Virtual_File_System-Part_I.shtml
But you could always use already built and tested code.This will be faster and easier to maintain and you will receive support in error conditions.
Take a look at jimfs ( An in-memory file system for Java 7+ )
https://github.com/google/jimfs
Also look into
Commons Virtual File System http://commons.apache.org/proper/commons-vfs/
marschall (An in memory implementation of a JSR-203 file system) https://github.com/marschall/memoryfilesystem

- 1,945
- 20
- 40
-
The second example is not really suited for nio2. Better classify them ;) – fge May 22 '15 at 11:06
-
@fge Have added some info about the link to avoid confusion. – Raj May 22 '15 at 11:25
-
OK but then this is 2015; apache-common-vfs is basically obsolete :) – fge May 22 '15 at 11:30
You can create In-memory file system in java using Google’s Jimfs and java 7 NIO package.
Please refer this link. Here you will get a sample tutorial: create In-memory file system in java

- 1,977
- 20
- 35
-
Unfortunately jimfs suffers several limitations. Your best bet is to use [memoryfilesystem](https://github.com/marschall/memoryfilesystem). – fge May 22 '15 at 11:06
Use memoryfilesystem.
Jimfs has been mentioned in a previous answer, but memoryfilesystem handles much more.
Example usage:
final FileSystem fs = MemoryFileSystem.newLinux().build("myfs");
final Path dir = fs.getPath("thedir");
Files.createDirectory(dir);
etc etc. Use the java.nio.file API to manipulate files in this (File
won't work!). See here for more details.

- 119,121
- 33
- 254
- 329
-
3I need `File` to work in memory both for reading and writing. Any ideas? – Jus12 Dec 02 '16 at 17:57