creating virtual file system with apache VFS - java

For unit tests, I'd like to create an in-memory file system with VFS.
My current code:
final String ROOTPATH = "ram://virtual";
FileSystemManager fsManager = VFS.getManager();
fsManager.createVirtualFileSystem(ROOTPATH);
FileObject testFile = fsManager.resolveFile(ROOTPATH + "/test.txt");
testFile.createFile();
FileObject testFile2 = fsManager.resolveFile(ROOTPATH + "/test2.txt");
testFile2.createFile();
FileObject testFile3 = fsManager.resolveFile(ROOTPATH + "/test3.txt");
testFile3.createFile();
FileObject testFile4 = fsManager.resolveFile(ROOTPATH + "/test4.txt");
testFile4.createFile();
FileObject folder = fsManager.resolveFile(ROOTPATH);
FileObject[] files = folder.getChildren();
for (FileObject file : files) {
System.out.println(file.getName());
}
My question: Is this the correct way to do it? Examples on this topica are sparse.
I still get the log message:
Apr 14, 2015 11:08:17 AM org.apache.commons.vfs2.VfsLog info
INFORMATION: Using "/tmp/vfs_cache" as temporary files store.
Can I ignore this, since I am using the ram URI scheme? I guess it's because I didn't configure the DefaultFileSystemManager.
Thanks for help and tips!
EDIT:
Now with the marschall memoryFileSystem:
I copied the example code from their website.
This is my #Test-Method:
FileSystem fileSystem = this.rule.getFileSystem();
Path testDirectoryPath = Paths.get("test");
Files.createDirectories(testDirectoryPath);
Path p = fileSystem.getPath("test");
System.out.println(p.getFileName());
System.out.println(p.getFileSystem());
Path testfile = Paths.get("test/text2.txt");
Path test = Files.createFile(testfile);
Path f = fileSystem.getPath("test/text2.txt");
System.out.println(f.getFileName());
System.out.println(f.getFileSystem());
System.out.println(f.toAbsolutePath());
This is the console output:
test
MemoryFileSystem[VirtualTestFileSystem]
text2.txt
MemoryFileSystem[VirtualTestFileSystem]
/test/text2.txt
Looks alright, however: The files and directories actually get created on my hard drive, in the project folder. I thought, the whole point of this was to avoid exactly that...? Am I doing something wrong or do I just not get it...?

Your are using Paths.get
Path testDirectoryPath = Paths.get("test");
Paths.get always creates paths on the default file system. You should use
Path testDirectoryPath = fileSystem.getPath("test");

Related

How to get the path of the file from source root in java?

I have a file in java under the src folder, I want to get its path at runtime relative to the source folder.
For example-
myProject
-- src
-- packageOne
-- SomeFile.java
I would like to have the result packageOne/SomeFile.java. I couldn't find a way, I tried getPath(), getAbsoultePath() and every similar method.
You want to construct a relative path. AFAIK there is no ready function to use.
Given that you have one of the ancestor nodes (src) and you have SomeFile.java, the following code might work but I did not try...
File src = new File("...");
File somefile = new File("...");
String relpath = somefile.getName();
File cursor = somefile.getParentFile();
while (!cursor.getAbsolutePath().equals(src.getAbsolutePath()) {
somefile = cursor.getName() + File.pathSeparator + somefile;
}
System.out.println("relative path: "+relpath);

Absolute Path from File in Package

Let's say I have a package: com.example.resources and inside it I have picture.jpg and text.txt - How can I use only the package path to identify the absolute filepath of picture.jpg and text.txt?
Is this correct?
File file1 = new File("/com/example/resources/picture.jpg");
file1.getAbsolutePath();
File file2 = new File("/com/example/resources/text.txt");
file2.getAbsolutePath();
"I'm trying to reference those files within the application so that when I deploy the application to different systems I won't have to reconfigure the location of each resource "
You don't want to use a File, which will load from the file system of the local machine, you want to load through the class path, as that's how embedded resources should be read. Use getClass().getResourceAsStream(..) for the text file and getClass().getReource(..) for the image
Your current path you're referencing looks like it should work, given the package name you've provided.
Take a look at this answer and this answer as some references.
I've figured out how to do it. Here is the code that I used:
public String packagePathToAbsolutePath(String packageName, String filename) {
String absolutePath = "File not found";
URL root = Thread.currentThread().getContextClassLoader().getResource(packageName.replace(".", "/"));
File[] files = new File(root.getFile()).listFiles();
for (File file : files) {
if (file.getName().equals(filename)) {
absolutePath = file.getName();
return absolutePath;
}
}
return absolutePath;
}
This method was really useful because it allowed me to configure the resource paths within the application so that when I ran the application on different systems I didn't have to reconfigure any paths.

file path in hdfs

I want to read the file from the Hadoop File System.
In order to achieve the correct path of the file, I need host name and port address of the hdfs.
so finally my path of the file will look something like
Path path = new Path("hdfs://123.23.12.4344:9000/user/filename.txt")
Now I want to know to extract the HostName = "123.23.12.4344" & port: 9000?
Basically, I want to access the FileSystem on Amazon EMR but, when I use FileSystem fs = FileSystem.get(getConf()); I get
You possibly called FileSystem.get(conf) when you should have called FileSystem.get(uri, conf) to obtain a file system supporting your path
So I decided to use URI. (I have to use URI) but I am not sure how to access the URI.
You can use either of the two ways to solve your error.
1.
String infile = "file.txt";
Path ofile = new Path(infile);
FileSystem fs = ofile.getFileSystem(getConf());
2.
Configuration conf = getConf();
System.out.println("fs.default.name : - " + conf.get("fs.default.name"));
// It prints uri as : hdfs://10.214.15.165:9000 or something
String uri = conf.get("fs.default.name");
FileSystem fs = FileSystem.get(uri,getConf());

Getting the directory name in java

How do I get the directory name for a particular java.io.File on the drive in Java?
For example I have a file called test.java under a directory on my D drive.
I want to return the directory name for this file.
File file = new File("d:/test/test.java");
File parentDir = file.getParentFile(); // to get the parent dir
String parentDirName = file.getParent(); // to get the parent dir name
Remember, java.io.File represents directories as well as files.
With Java 7 there is yet another way of doing this:
Path path = Paths.get("d:/test/test.java");
Path parent = path.getParent();
//getFileName() returns file name for
//files and dir name for directories
String parentDirName = path.getFileName().toString();
I (slightly) prefer this way, because one is manipulating path rather than files, which imho better shows the intentions. You can read about the differences between File and Path in the Legacy File I/O Code tutorial
Note also that if you create a file this way (supposing "d:/test/" is current working directory):
File file = new File("test.java");
You might be surprised, that both getParentFile() and getParent() return null. Use these to get parent directory no matter how the File was created:
File parentDir = file.getAbsoluteFile().getParentFile();
String parentDirName = file.getAbsoluteFile().getParent();
File file = new File("d:/test/test.java");
String dirName = file.getParentFile().getName();
Say that you have a file called test.java in C:\\myfolder directory. Using the below code, you can find the directory where that file sits.
String fileDirectory = new File("C:\\myfolder\\test.java").getAbsolutePath();
fileDirectory = fileDirectory.substring(0,fileDirectory.lastIndexOf("\\"));
This code will give the output as C:\\myfolder

Getting file path in java

Is there a way for java program to determine its location in the file system?
You can use CodeSource#getLocation() for this. The CodeSource is available by ProtectionDomain#getCodeSource(). The ProtectionDomain in turn is available by Class#getProtectionDomain().
URL location = getClass().getProtectionDomain().getCodeSource().getLocation();
File file = new File(location.getPath());
// ...
This returns the exact location of the Class in question.
Update: as per the comments, it's apparently already in the classpath. You can then just use ClassLoader#getResource() wherein you pass the root-package-relative path.
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
URL resource = classLoader.getResource("filename.ext");
File file = new File(resource.getPath());
// ...
You can even get it as an InputStream using ClassLoader#getResourceAsStream().
InputStream input = classLoader.getResourceAsStream("filename.ext");
// ...
That's also the normal way of using packaged resources. If it's located inside a package, then use for example com/example/filename.ext instead.
For me this worked, when I knew what was the exact name of the file:
File f = new File("OutFile.txt");
System.out.println("f.getAbsolutePath() = " + f.getAbsolutePath());
Or there is this solution too: http://docs.oracle.com/javase/tutorial/essential/io/find.html
if you want to get the "working directory" for the currently running program, then just use:
new File("");

Categories

Resources