zoukankan      html  css  js  c++  java
  • java加载外置文件

    比如我们要加载db.properties文件

    如图:

    比如我们要加载source目录下的db.properties文件。就有以下几种方式

    第一种是文件io流:

    复制代码
        public static void load1() throws Exception{
        //文件真实路径
            String fileName="E:/Workspace/SSHDemo/Source/db.properties";
            Properties p=new Properties();
            InputStream is=new FileInputStream(new File(fileName));
            p.load(is);
            System.out.println(p);
        }
    复制代码

    第二种:相对路径:

    复制代码
        //相对路径
        public static void load2() throws Exception{
            Properties p=new Properties();
            //InputStream is=ClassLoader.getSystemResourceAsStream("db.properties");
         InputStream is=Thread.currentThread().getContextClassLoader().getSystemResourceAsStream("db.properties"); p.load(is); System.out.println(p); } public static void load2_1() throws Exception{ Properties p=new Properties(); InputStream is=SourceLoader.class.getClassLoader().getSystemResourceAsStream("db.properties"); p.load(is); System.out.println(p); }
    复制代码

    如果我们要获取src(类包)下的db.properties又该怎么处理呢?

    复制代码
    //相对于类路径  properties文件盒java放在一起
        public static void load3() throws Exception{
            Properties p=new Properties();
            //InputStream is=ClassLoader.getSystemResourceAsStream("db.properties");
            InputStream is=SourceLoader.class.getResourceAsStream("db.properties");
            p.load(is);
            System.out.println(p);
        }
    复制代码

    三种方式都打印出来db.properties文件中的信息:

  • 相关阅读:
    利用iframe实现ajax 跨域通信的解决方案
    C++中事件机制的简洁实现
    java远程类加载与轻客户端
    iOS开发那些事性能优化–内存泄露问题的解决
    Asp.net项目的开发流程
    LVS配置教程
    PowerShell之东扯西谈
    响应式设计专题
    2013年最值得关注的网页设计流行趋势
    HTTP Live Streaming直播技术分析与实现
  • 原文地址:https://www.cnblogs.com/arctictern/p/7055389.html
Copyright © 2011-2022 走看看