zoukankan      html  css  js  c++  java
  • 运用properties进行文件操作

    package haohaoxuexi;

    //利用properties输出到文件
    //资源配置文件
    import java.util.Properties;

    public class lianxi18 {
    public static void main(String[] args) {
    //创建properties对象
    Properties properties=new Properties();
    //存储
    properties.setProperty("driver", "oracle.jdbc.driver.OracleDrier");
    //properties.setProperty("url", "jdbc:oracle:thin@localhost:1521:orcl");
    properties.setProperty("user", "scott");
    properties.setProperty("pwd", "tiger");
    //获取
    String string=properties.getProperty("url", "test");
    System.out.println(string);
    }

    }

    package haohaoxuexi;

    import java.io.FileNotFoundException;

    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.util.Properties;
    /*使用properties
    1.存储为.properties的文件
    store(OutputStream out,String comments)
    store(Writer writer,String comments)
    2.存储为.xml的文件
    storeToXML(OutputStream out,String comments)
    storeToXML(OutputStream out,String comments,string encoding)
    */
    public class lianxi19 {
    public static void main(String[] args) throws FileNotFoundException, IOException {
    //创建对象
    Properties properties=new Properties();
    //存储
    properties.setProperty("driver", "oracle.jdbc.driver.OracleDrier");
    properties.setProperty("url", "jdbc:oracle:thin@localhost:1521:orcl");
    properties.setProperty("user", "scott");
    properties.setProperty("pwd", "tiger");

    //存储到绝对路径
    properties.store(new FileOutputStream(new File("g:/db.properties")), "db配置");
    properties.storeToXML(new FileOutputStream(new File("g:/db.xml")), "db配置");
    //当前工程相对路径
    properties.store(new FileOutputStream(new File("src/haohaoxuexi/db.properties")), "db配置");
    }

    }

    package haohaoxuexi;

    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.Properties;
    //利用properties输出到文件
    //资源配置文件
    //使用load读取文件
    public class lianxi20 {
    public static void main(String[] args) throws FileNotFoundException, IOException {
    Properties properties=new Properties();
    //读取绝对路径
    properties.load(new FileReader("g:db.properties"));
    //读取相对路径
    properties.load(new FileReader("db.properties"));
    //输出读取到的文件
    System.out.println(properties.getProperty("user", "bjsxt"));
    }

    }

    package haohaoxuexi;

    import java.io.IOException;
    import java.util.Properties;
    //利用properties输出到文件
    //资源配置文件
    //通过用get方法实现值的查找
    //getProperty(string key,defultvalue);

    public class lianxi21 {
    public static void main(String[] args) throws IOException {
    //新建
    Properties properties=new Properties();
    //从当前的类向上查找
    properties.load(lianxi21.class.getResourceAsStream("/haohaoxuexi/db.properties"));
    //从当前main方法向上查找
    properties.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("haohaoxuexi/db.properties"));
    //输出
    System.out.println(properties.getProperty("user","bjsxt"));

    }

    }

  • 相关阅读:
    ffmpeg+nginx+video实现rtsp流转hls流,通过H5查看监控视频
    视频支持拖动进度条播放的实现(基于nginx)
    nginx 点播mp4方法
    NGINX 添加MP4、FLV视频支持模块
    html5播放mp4视频代码
    ThreadPoolExecutor使用错误导致死锁
    Spring-Cloud-Gateway
    从0开始构建你的api网关--Spring Cloud Gateway网关实战及原理解析
    使用Consul做服务发现的若干姿势
    Consul 的安装与基本使用
  • 原文地址:https://www.cnblogs.com/zx931880423/p/6789637.html
Copyright © 2011-2022 走看看