Search code examples
javafile-ioramfileinputstream

Launch a file created in RAM memory


I've been asked to launch an Excel file, created at RunTime inside the RAM memory.

For example, with:

Desktop.getDesktop().open(new File("c:\....\fileName.xlsx"));

I can "launch" Excel program that open the file "c:....\fileName.xlsx".

Since my file is created inside the RAM memory this way:

 ...

     FileSystemManager fsm = VFS.getManager();
     FileObject RAM = fsm.resolveFile("ram://ramFileName.xlsx");
     OutputStream os = RAM.getContent().getOutputStream();
     FileInputStream sorgente = new FileInputStream("C:\\originalFile.xlsx");   
     int singloByte;

     while ((singloByte = sorgente.read()) != -1) {
        os.write(singloByte);
     }
     sorgente.close();
     os.close();
     ...

is there a way to open a file created only in RAM memory and not in file system?

Thank you


Solution

  • Excel files can't be "launched". What Desktop.open() does is finds the default handler for the file (presumably Excel in this case), and launches the handler with the given file as a parameter.

    Since Excel can't open "in-memory files" (AFAIK), you have no choice but to write it to a temporary file to open it.