如何打开文件而不保存到磁盘 [英] How to open a file without saving it to disk

查看:145
本文介绍了如何打开文件而不保存到磁盘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


$ b $

我的情况:我的资源中有文件,我想先显示这些文件,而不是先保存到磁盘上。例如,我有一个 xml 文件,我想在用户的机器上用默认程序打开 xml 文件,而不是先保存到磁盘。



我一直在做:到目前为止,我已经将文件保存到临时位置,但是我不知道什么时候不再需要该文件,所以我不知道何时/是否要删除它。这是我的 SSCCE 代码(嗯,除了资源之外,主要是sscce ...你必须你自己创建):

  package main; 

import java.io. *;
$ b $ public class SOQuestion {

public static void main(String [] args)抛出IOException {
new SOQuestion()。showTemplate();

$ b $ **打开临时文件* /
private void showTemplate()throws IOException {
String tempDir = System.getProperty(java.io。 tmpdir)+\\ BONotifier \\;
文件parentFile =新文件(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 = inputStream.read(buffer))> 0){
out.write(buffer,0,length);
}
inputStream.close();

java.awt.Desktop.getDesktop()。open(outputFile);



$ div $解析方案

因为
$ b

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

我推断您正在使用Windows。你可以很容易地使这个代码多平台,你知道。



你的问题的答案是:不。 Desktop类需要知道文件的位置,以便用参数调用正确的程序。请注意,该类中没有方法接受 InputStream ,这可能是一个解决方案。



无论如何,没有看到问题出在哪里:你创建一个临时文件,然后用编辑器或其他方式打开它。没关系。在Linux中,退出应用程序(通常)时,其所有临时文件都将被删除。在Windows中,用户需要触发删除临时文件。但是,如果你没有安全限制,我不明白问题在哪里。毕竟,临时文件是操作系统的问题。

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);
  }
}

解决方案

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.

这篇关于如何打开文件而不保存到磁盘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆