• 技术文章 >Java >java教程

    java如何打开本地html文件

    王林王林2020-02-05 16:43:45原创1890

    第一种:Object获取项目中的properties

    InputStream in = Object. class .getResourceAsStream( "/com/demo/conf.properties" );

    第二种:直接获得本地配置文件properties

    (推荐学习:java视频教程

    FileInputStream in = new FileInputStream("D:\\work\\demo\\conf.properties"); 
    //加载properties文件
    Properties prop =  new  Properties();
    prop.load(in);
    //从配置文件中获取 页面的位置,此处url为网页的绝对路径并解决乱码,如:d:/demo/index.html
     String url = new String(prop.getProperty( "csv_url" ).trim().getBytes("ISO-8859-1"),"gbk");
    //根据url打开网页
    private static void browse(String url) throws ClassNotFoundException, IllegalAccessException,
    IllegalArgumentException, InterruptedException, InvocationTargetException, IOException,NoSuchMethodException {
      String osName = System.getProperty("os.name", "");
        if (osName.startsWith("Windows")) {
            Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + url);
              } else if (osName.startsWith("Mac OS")) {
                  Class fileMgr = Class.forName("com.apple.eio.FileManager");
                   Method openURL = fileMgr.getDeclaredMethod("openURL", new Class[] { String.class });
                       openURL.invoke(null, new Object[] { url });
                         } else {
                          //  Unix or Linux
                              String[] browsers = { "firefox", "opera", "konqueror", "epiphany", 
                              "mozilla", "netscape" };
        String browser = null;
            for (int count = 0; count < browsers.length && browser == null; count++)
            if (Runtime.getRuntime().exec(new String[] { "which", browsers[count] }).waitFor() == 0)
            browser = browsers[count];
            if (browser == null)
            throw new NoSuchMethodException("Could not find web browser");
            else
                  Runtime.getRuntime().exec(new String[] { browser, url });
                    }}

    相关文章教程推荐:java入门教程

    以上就是java如何打开本地html文件的详细内容,更多请关注php中文网其它相关文章!

    声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。
    专题推荐:java 打开 本地 html 文件
    上一篇:java实现文件夹不存在则创建 下一篇:java中GBK转UTF-8乱码的解决方法
    VIP课程(WEB全栈开发)

    相关文章推荐

    • 【腾讯云】年中优惠,「专享618元」优惠券!• 如何查看是否安装了java环境• java判断数据库中是否存在某一张表• cmd运行“java -version”提示找不到或无法加载主类• java实现文件夹不存在则创建
    1/1

    PHP中文网