In-memory file system in java - java

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

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

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

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.

Related

Android: load java.io.file from resource file

I would like to use a Java library in my Android application. The class constructors and methods of this library often take paths to files (configuration file, dictionary, etc.) and then build java.io.file instances based on the given paths.
In my android application, I would like to store these file in the 'res' folder (possibly in res/raw). The problem is that I have to give a path to these files to the methods of the library.
I could easily get an InputStream using getResources(), but this would not be directly usable by my library. I would have to go through all the methods taking a path as an argument, replace it by an InputStream and modify the content to deal with InputStreams instead of Files. This represent quite a lot of work and I would much prefer to use the library without modification and keep it easily upgradable.
Even though using java.io.file based on resource file would not be a good practice, is it something possible? It would definitely help if you could indicate a way to do this.
Thank you.
If the library uses java.io.File then I don't think there is a way to do this in Java (let alone the Android subset of Java). It might be possible to solve the problem with a loopback filesystem, but this depends on your Android device's kernel, etc.
See:
https://android.stackexchange.com/questions/25396/how-to-find-out-if-my-devices-kernel-has-loop-device-support
If the library uses java.nio.file.Path, then it may be possible to implement a custom FileSystemProvider that maps the resources into the default file system namespace.
Note this is for regular Java 7. It would require a back-port of the relevant NIO libraries to get this to work on Android. I had another look for a viable backport, and couldn't find one.
See:
Tweaking the behavior of the default file system in Java 7
How to use java.nio.file package in android?
There is another "clunky" way to do this. Get your application to copy the relevant resources to files that can be accessed via a File.

Create a Java 7 zip FileSystem from an InputStream

Java 7 introduced a zip FileSystem. The link below illustrates how to create a zip FileSystem from a zip file.
http://docs.oracle.com/javase/7/docs/technotes/guides/io/fsp/zipfilesystemprovider.html
However, I can find no example of how to create a zip FileSystem from an InputStream. Is that possible? If so, how?
Note: I know I can write the InputStream to disk and create a zip FileSystem as described. I consider that a hack, and I would prefer to avoid it.
No, it's not possible because the file system requires random access to the ZIP file.
Shameless self-plug: You may find TrueZIP easier to work with and more powerful. However, the same constraint applies to it, too.
Only a partial answer, but I expect you'll need a custom file system provider and this question about an in-memory file system might help. Note that the newFileSystem documentation shows a memory://... URL scheme, but no more detail.
As pointed out in another answer, the file system requires bidirectional access to the data, so this assumes enough memory to load it entirely.

Using java to represent a file structure in memory?

I'm trying to index a file structure (not actual data) relative to a specific path on my local disk. Initially I load the file structure to memory then listen for real changes in the directory. If one occurs then I update the relevant indexed files in internal memory. Not unlike what dropbox seems to do?
Does there exist any java-library which can do this? I don't really want to implement this from scratch.
To listen for changes on a file, you can have a look at apache commons io which has a FileAlterationMonitor.
Java 7, as part of NIO.2, has the WatchService API.
The WatchService API is designed for applications that need to be notified about file change events.

Java library to create cabinet files on Unix?

Is there a Java library to create cabinet files on Unix. I don't need any compression support. I just want to create a plain cab file using Java.
Something similar to cablib (http://sourceforge.net/projects/cablib/) which can only be used for reading cab files would be perfect.
If there is really no library can I use a feasible work around? E.g. create a ZIP file and somehow convert it into a CAB file?
If there is really no library can I use a feasible work around?
Comments have suggested using the Linux Icab tool.
E.g. create a ZIP file and somehow convert it into a CAB file?
The ZIP file format is different in too many respects for there to be a simple transformation to turn a ZIP file into a CAB file.
Edit: The answer below isn't a pure java solution. Ant's CAB task documentation says it relies on a 3rd-party tool: Either MS's "CABARC" or the open-source "libcabinet", which seems to no longer exist. So there is no benefit to this approach compared to a 3rd-party system call.
Previous Answer (read above first):
If you need a pure java way of creating cab files (not extracting them), you can use ant's built-in "cab" task.
This gives you a few options:
Call the ant task from within your java code by using ant's
Launcher class;
Find the source code for the task definition (here) , and remove references to the ant context to create your own Cab extract utility
Run ant via a system call.

Any way to get a File object from a JAR

I have a JAR file that contains an API that uses external model files. I would like to include the model files in the JAR itself so it easier to use for other developers. The API will accept a File object only, is there any way to do this? I have already tried the following, and they have failed:
Using class.getResourceAsStream(). This would work if the API accepted an InputStream.
Parsing the classpath and trying to build from the entries (the JAR will show as app.jar)
I suppose an option is to use getResourceAsStream and move the files to a permanent location on the HDD but, I do not like this option. There has to be something better, any thoughts?
Resources in a .jar file are not files in the sense that the OS can access them directly via normal file access APIs.
And since java.io.File represents exactly that kind of file (i.e. a thing that looks like a file to the OS), it can't be used to refer to anything in a .jar file.
A possible workaround is to extract the resource to a temporary file and refer to that with a File.
Note that generally APIs that try to handle files should be written to handle InputStream/OutputStream as well to allow this kind of operations to suceed.

Categories