properties文件操作心得

    技术2022-05-20  28

    首先明白Properties这个类继承自哈希map,那么他便相对地继承的哈希的一些方法。

    1,创建文件,建立输入流,将输入流载入到文件里

    file = new File(filePath);// 创建文件

    file.createNewFile(); // 创建Properties文件

    InputStream in = new BufferedInputStream(new FileInputStream(

    filePath));// 创建输入流,读入文件

    props.load(in);// 载入文件

    in.close();// 关闭输入流

     

    2,设置相应的键值

    props.setProperty(parameterName[i], parameterValue[i]);// 存放相应名字键的值

    setProperty的用法详见API;

     

    3,读取文件

     

    file = new File(filePath);

    /* file.mkdir(); */

    file.createNewFile(); // 创建Properties文件

    /* String filePath1 = file.getPath(); */

    InputStream in = new BufferedInputStream(new FileInputStream(

    filePath));

    props.load(in);// 载入文件

    //同上

    然后使用 props.getProperty(key);方法得到键值

     

     

    4,保存文件

     

    OutputStream out = null;// 创建输出流,输出文件

    out = new FileOutputStream(file);// 将文件输出

    props.store(out, null/* "By shadao" */);// 将props放到输出流中

    out.close();

     

     

     

     

     


    最新回复(0)