15

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).

mmvsbg
  • 3,570
  • 17
  • 52
  • 73
alle3x
  • 475
  • 2
  • 6
  • 18

3 Answers3

25

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

Raj
  • 1,945
  • 20
  • 40
6

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

Reena Upadhyay
  • 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
3

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.

fge
  • 119,121
  • 33
  • 254
  • 329