Reading a file created using virtual filesystem JIMFS

1.8k Views Asked by At

I have created a VFS using JIMFS.

FileSystem virtualFS = Jimfs.newFileSystem(Configuration.unix());
Path virtualPath = virtualFS.getPath("resources/virtualFolder");
Files.createDirectories(virtualPath);
Path refData = virtualPath.resolve("refData.csv");
System.out.println(refData);

Files.write(refData, ImmutableList.of(sData),StandardCharsets.UTF_8);

I am trying to read the file (refData.csv) in another method (Path is passed to the other method).

What I have tried until now are :

1: new FileDataModel(new FileInputStream(Files.lines(refData)));

2: new FileDataModel((File) Files.lines(refData));

3: new FileDataModel(new File(refData));

Unfortunately, none of these work as of now. I understand, I am mixing the default FS with the Virtual FS.

Error: Exception in thread "main" java.lang.UnsupportedOperationException

How to access the file created?.

1

There are 1 best solutions below

0
On

File and FileInputStream just don't work for anything but the default file system. What you'd need is a version of FileDataModel's constructor that takes a Path or a normal InputStream (you can get an InputStream for a Path using Files.newInputStream(Path)).

(By the way, Files.lines returns a Stream, not a File, so I'm not sure what you're trying to do there.)