Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to open a file without saving it to disk

Tags:

java

file

io

temp

My Question: How do I open a file (in the system default [external] program for the file) without saving the file to disk?

My Situation: I have files in my resources and I want to display those without saving them to disk first. For example, I have an xml file and I want to open it on the user's machine in the default program for reading xml file without saving it to the disk first.

What I have been doing: So far I have just saved the file to a temporary location, but I have no way of knowing when they no longer need the file so I don't know when/if to delete it. Here's my SSCCE code for that (well, it's mostly sscce, except for the resource... You'll have to create that on your own):

package main;

import java.io.*;

public class SOQuestion {

  public static void main(String[] args) throws IOException {
    new SOQuestion().showTemplate();
  }

  /** Opens the temporary file */
  private void showTemplate() throws IOException {
    String tempDir = System.getProperty("java.io.tmpdir") + "\\BONotifier\\";
    File parentFile = new File(tempDir);
    if (!parentFile.exists()) {
      parentFile.mkdirs();
    }
    File outputFile = new File(parentFile, "template.xml");
    InputStream inputStream = getClass().getResourceAsStream("/resources/template.xml");
    int size = 4096;
    try (OutputStream out = new FileOutputStream(outputFile)) {
      byte[] buffer = new byte[size];
      int length;
      while ((length = inputStream.read(buffer)) > 0) {
        out.write(buffer, 0, length);
      }
      inputStream.close();
    }
    java.awt.Desktop.getDesktop().open(outputFile);
  }
}
like image 542
kentcdodds Avatar asked Oct 18 '12 21:10

kentcdodds


2 Answers

Because of this line:

String tempDir = System.getProperty("java.io.tmpdir") + "\\BONotifier\\";

I deduce that you're working on Windows. You can easily make this code multiplatform, you know.

The answer to your question is: no. The Desktop class needs to know where the file is in order to invoke the correct program with a parameter. Note that there is no method in that class accepting an InputStream, which could be a solution.

Anyway, I don't see where the problem is: you create a temporary file, then open it in an editor or whatever. That's fine. In Linux, when the application is exited (normally) all its temporary files are deleted. In Windows, the user will need to trigger the temporary files deletion. However, provided you don't have security constraints, I can't understand where the problem is. After all, temporary files are the operating system's concern.

like image 111
Baltasarq Avatar answered Oct 11 '22 12:10

Baltasarq


Depending on how portable your application needs to be, there might be no "one fits all" solution to your problem. However, you can help yourself a bit:

At least under Linux, you can use a pipe (|) to direct the output of one program to the input of another. A simple example for that (using the gedit text editor) might be:

echo "hello world" | gedit

This will (for gedit) open up a new editor window and show the contents "hello world" in a new, unsaved document.


The problem with the above is, that this might not be a platform-independent solution. It will work for Linux and probably OS X, but I don't have a Windows installation here to test it.

Also, you'd need to find out the default editor by yourself. This older question and it's linked article give some ideas on how this might work.

like image 26
Lukas Knuth Avatar answered Oct 11 '22 11:10

Lukas Knuth