当前位置: 首页 > 面试题库 >

使用Java 1.5跨平台打开文件的方法

杜俊远
2023-03-14
问题内容

我正在使用Java 1.5,并且想启动相关的应用程序来打开文件。我知道Java 1.6引入了Desktop
API
,但是我需要
Java 1.5 的解决方案。

到目前为止,我找到了在Windows中执行此操作的方法:

Runtime.getRuntime().exec(new String[]{ "rundll32", 
                          "url.dll,FileProtocolHandler", fileName });

有跨平台的方法吗?或者至少是针对 Linux 的类似解决方案?


问题答案:

另外,我建议使用多态性进行以下实现:

这样,您可以通过减少类之间的耦合来更轻松地添加新平台。

客户端代码:

 Desktop desktop = Desktop.getDesktop();

 desktop.open( aFile );
 desktop.imaginaryAction( aFile );

桌面展示:

package your.pack.name;

import java.io.File;

public class Desktop{

    // hide the constructor.
    Desktop(){}

    // Created the appropriate instance
    public static Desktop getDesktop(){

        String os = System.getProperty("os.name").toLowerCase();

        Desktop desktop = new Desktop();
         // This uf/elseif/else code is used only once: here
        if ( os.indexOf("windows") != -1 || os.indexOf("nt") != -1){

            desktop = new WindowsDesktop();

        } else if ( os.equals("windows 95") || os.equals("windows 98") ){

            desktop = new Windows9xDesktop();

        } else if ( os.indexOf("mac") != -1 ) {

            desktop = new OSXDesktop();

        } else if ( os.indexOf("linux") != -1 && isGnome() ) {

            desktop = new GnomeDesktop();

        } else if ( os.indexOf("linux") != -1 && isKde() ) {

            desktop = new KdeDesktop();

        } else {
            throw new UnsupportedOperationException(String.format("The platform %s is not supported ",os) );
        }
        return desktop;
    }

    // default implementation :( 
    public void open( File file ){
        throw new UnsupportedOperationException();
    }

    // default implementation :( 
    public void imaginaryAction( File file  ){
        throw new UnsupportedOperationException();
    }
}

// One subclass per platform below:
// Each one knows how to handle its own platform


class GnomeDesktop extends Desktop{

    public void open( File file ){
        // Runtime.getRuntime().exec: execute gnome-open <file>
    }

    public void imaginaryAction( File file ){
        // Runtime.getRuntime().exec:gnome-something-else <file>
    }

}
class KdeDesktop extends Desktop{

    public void open( File file ){
        // Runtime.getRuntime().exec: kfmclient exec <file>
    }

    public void imaginaryAction( File file ){
        // Runtime.getRuntime().exec: kfm-imaginary.sh  <file>
    }
}
class OSXDesktop extends Desktop{

    public void open( File file ){
        // Runtime.getRuntime().exec: open <file>
    }

    public void imaginaryAction( File file ){
        // Runtime.getRuntime().exec: wow!! <file>
    }
}
class WindowsDesktop extends Desktop{

    public void open( File file ){
        // Runtime.getRuntime().exec: cmd /c start <file>
    }

    public void imaginaryAction( File file ){
        // Runtime.getRuntime().exec: ipconfig /relese /c/d/e
    }
}
class Windows9xDesktop extends Desktop{

    public void open( File file ){
        //Runtime.getRuntime().exec: command.com /C start <file>
    }

    public void imaginaryAction( File file){
       //Runtime.getRuntime().exec: command.com /C otherCommandHere <file>
    }
}

这只是一个例子,在现实生活中不值得仅通过参数化值(命令字符串%s)来创建一个新类,但是让我们想象一下每种方法都以平台特定的方式执行另一个步骤。

采取这种方法,可能会删除不必要的if / elseif /
else结构,这些结构会随着时间的推移而引入错误(如果代码中有6个错误,并且需要更改,您可能会忘记更新其中之一,或者通过复制/粘贴,您可能会忘记更改要执行的命令)



 类似资料:
  • 多平台支持 Mpx支持在多个小程序平台中进行增强,目前支持的小程序平台包括微信,支付宝,百度,qq和头条,不过自2.0版本后,Mpx支持了以微信增强语法为base的跨平台输出,实现了一套业务源码在多端输出运行的能力,大大提升了多小程序平台业务的开发效率,详情可以查看template增强特性 不同平台上的模板增强指令按照平台的指令风格进行设计,文档和代码示例为了方便统一采用微信小程序下的书写方式。

  • 问题内容: 我创建了一个小的python脚本。我将pickle文件保存在Linux上,然后在Windows上使用它,然后再次在Linux上使用它,但是现在该文件在Linux上不起作用,但是在Windows上可以正常工作。是这样,以便python是coss平台的,但pickle文件不是。有什么解决办法吗??? 问题答案: Python的泡菜是完全跨平台的。 这可能是由于Windows和Linux之间

  • 问题内容: 如何以跨平台方式打开带有主题和正文的默认邮件程序? 不幸的是,这是针对用Java编写的客户端应用程序,而不是网站。 我希望它可以跨平台的方式工作(这意味着Windows和Mac,对不起Linux)。我很高兴在Windows中执行VBScript,在OS X中执行AppleScript。但是我不知道这些脚本应该包含什么。我想执行用户的默认程序,而不是仅搜索Outlook或其他内容。 在O

  • 本文向大家介绍C#使用Streamwriter打开文件的方法,包括了C#使用Streamwriter打开文件的方法的使用技巧和注意事项,需要的朋友参考一下 本文实例讲述了C#使用Streamwriter打开文件的方法。分享给大家供大家参考。具体如下: 希望本文所述对大家的C#程序设计有所帮助。

  • 概况 背景 Web本身就是跨平台的,这意味着这中间存在着无限的可能性。 我是一名Web Developer,对于我来能用Web开发的事情就用Web来完成就好了——不需要编译,不需要等它编译完。我想到哪我就可以写到哪,我改到哪我就可以发生哪发生了变化。 最近我在写Growth——一个帮助开发人员成长的应用,在近一个月的业余时间里,完成了这个应用的: 移动应用版:Android、Windows Pho

  • 问题内容: py2exe很棒,每当我想打包一个要在Windows系统上运行的python程序时,我都会使用py2exe。 我的问题是,是否可以使用等效工具在Windows上打包程序,但是可以在Linux上运行? 问题答案: 好的,我已经做到了。这有点hacky,但是对于我的用例来说效果很好。 要点是使用ModuleFinder查找所有导入的模块,过滤掉所有系统模块,编译并压缩它们。 不幸的是,我的