zoukankan      html  css  js  c++  java
  • java笔记----property文件读写

    package com.test.property;
    
    import java.io.BufferedInputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.HashMap;
    import java.util.Iterator;
    import java.util.Properties;
    
    /**
    * @author 
    * @version 创建时间:2019年3月26日 上午8:40:17
    * 类说明
    */
    public class PropertyTest {
        static Properties prop = new Properties();
        static String strPath=PropertyTest.class.getClassLoader().getResource("./").getPath()+"/a.properties";
        static HashMap<String, String> map = new HashMap<String, String>();
        public static void main(String[] args) throws IOException {
            // TODO Auto-generated method stub
              
              File file = new File(strPath);  
              String[] path =strPath.split("/");
              String filename =path[path.length-1];
              String dpath =strPath.replace("/"+filename, "");
               System.out.println("文件路径"+dpath);
               System.out.println("文件名"+filename);
                if(!new File(dpath).exists()){
                    new File(dpath).mkdirs();
                    System.out.println("创建目录成功:"+dpath);
                if(!file.exists()){
                        file.createNewFile();
                        System.out.println("创建文件成功:"+file);
                    }
                }else{
                    if(!file.exists()){
                        file.createNewFile();
                        System.out.println("创建文件成功2:"+file);
                    }
                }
                
                //读取属性文件a.properties
                InputStream in = new BufferedInputStream (new FileInputStream(strPath));
                prop.load(in);     ///加载属性列表
                Iterator<String> it=prop.stringPropertyNames().iterator();
                while(it.hasNext()){
                    String key=it.next();
                    map.put(key,prop.getProperty(key));
                    System.out.println(key+":"+prop.getProperty(key));
                }
                in.close();
                addProp("3","455");
                
        }
       
        private static boolean addProp(String key,String value){//添加key不是相同的property
            //
            if(map.containsKey(key)){  //里面含有该key不写进去
                return false;
            }else{
            try {
                FileOutputStream oFile = new FileOutputStream(strPath, false);
                prop.setProperty(key, value);
                prop.store(oFile, null);//null就是不要注释
                oFile.close();
                return true;
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }//true表示追加打开
            return false;
          }
        }
    }

     

  • 相关阅读:
    Android 音视频同步机制
    FFmpeg命令行工具学习(五):FFmpeg 调整音视频播放速度
    Android框架式编程之RxJava
    Android Gradle 学习笔记(一):Gradle 入门
    FFmpeg开发实战(六):使用 FFmpeg 将YUV数据编码为视频文件
    SDL 开发实战(七): SDL 多线程与锁机制
    JNI实战(四):C 调用 Java
    JNI实战(三):JNI 数据类型映射
    JNI实战(二):Java 调用 C
    JNI实战(一):JNI HelloWorld
  • 原文地址:https://www.cnblogs.com/tk55/p/10598198.html
Copyright © 2011-2022 走看看