读取配置文件

读取配置文件 mysql.properties

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/ucdos
dbroot=root
dbpass=admin

读取类

package com.it;

import java.util.ResourceBundle;

public class ReadProperties {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//指定路径 包名.配置文件名
		String flename="test.it.mysql";
		//读取properties文件 需要把配置文件放src下面
		 ResourceBundle rs = ResourceBundle.getBundle(flename);//mysql.properties
		 String driv=rs.getString("driver");
		 String url=rs.getString("url");
		 String name=rs.getString("dbroot");
		 String pass=rs.getString("dbpass");
		 System.out.println(driv);
		 System.out.println(url);
		 System.out.println(name);
		 System.out.println(pass);
	}

}

读取init配置文件 mysql.init

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/ucdos
dbroot=root
dbpass=admin

读取类

package com.it;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

public class ReadInit {

	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		
		//读取配置文件
		Properties p=new Properties();
		//文件对象 config下的mysql.init
		File f=new File("config/mysql.init");
		//文件输入流
		FileInputStream finst=new FileInputStream(f);
		//加载文件
		p.load(finst);
		//读取属性文件中指定键的值,并赋给变量
		String driv=p.getProperty("driver");
		String url=p.getProperty("url");
		String name=p.getProperty("dbroot");
		String pass=p.getProperty("dbpass");
		
		System.out.println(driv);
		System.out.println(url);
		System.out.println(name);
		System.out.println(pass);
	}

}

Maven读取

COS配置文件 cloudcos.properties

#腾讯云储存配置 
SECRET_ID=AKIDq5YrntRkDIVrc6Rc4xczOwxf6bGROrTG
SECRET_KEY=SeYCbgU1HK60e1C6j8I4YkbCWcVBoAPJ
BUCKETNAME=sls-cloudfunction-ap-guangzhou-code-1301165591
REGIONID=ap-guangzhou

读取配置

package com.dbutils;
import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.auth.COSCredentials;
import com.qcloud.cos.model.GetObjectRequest;
import com.qcloud.cos.model.ObjectMetadata;
import com.qcloud.cos.model.PutObjectRequest;
import com.qcloud.cos.region.Region;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
/**
 * @author Mr_xiao
 *
 * 读取cos配置文件
 */
public class CosFileupload {
    private static final String SECRET_ID;
    private static final String SECRET_KEY ;
    private static final String BUCKETNAME;
    private static final String REGIONID;
    static{
       String fname ="cloudcos.properties";
       InputStream file= JDBCTool.class.getClassLoader().getResourceAsStream(fname);
       Properties prop = new Properties();
       try {
           prop.load(file);
       } catch (IOException e) {
           e.printStackTrace();
       }
       SECRET_ID=prop.getProperty("SECRET_ID");
       SECRET_KEY=prop.getProperty("SECRET_KEY");
       BUCKETNAME=prop.getProperty("BUCKETNAME");
       REGIONID=prop.getProperty("REGIONID");
   }
    /**
     * 
     * @return
     */
    public static COSClient Initialize(){
        /**
         * 初始化用户身份信息(secretId, secretKey)
         */
        COSCredentials cred = new BasicCOSCredentials(SECRET_ID, SECRET_KEY);
        /**
         * 设置bucket的区域 http方式
         */
        ClientConfig clientConfig = new ClientConfig(new Region(REGIONID));
        /**
         * 生成cos客户端
         */
        COSClient cosClient = new COSClient(cred, clientConfig);
        return cosClient;
    }
    /**
     *文件上传
     * @param Inputstream  传文件流
     * @param key  为文件名 需要唯一
     */
    public static void upfile(InputStream Inputstream, String key){
        /**
         * 得到初始化的对象
         */
        COSClient cosin=CosFileupload.Initialize();
        ObjectMetadata metadata=new ObjectMetadata();
        PutObjectRequest request=new PutObjectRequest(BUCKETNAME,key,Inputstream,metadata);
        cosin.putObject(request);
        cosin.shutdown();
    }
    /***
     * 下载文件
     * @param file 下载的路径
     * @param key  文件名
     */
    public static void downloadfile(File file,String key){
        /**
         * 指定下载的路径 + 文件名称
         */
        File dowfile=new File(file+"\\"+key);
        GetObjectRequest getobjrequest=new GetObjectRequest(BUCKETNAME,key);
        COSClient cosdowfile=CosFileupload.Initialize();
        cosdowfile.getObject(getobjrequest,dowfile);
        cosdowfile.shutdown();
    }
    /**
     * 删除文件
     * @param key 文件名
     */
    public static void delfile(String key){
        COSClient cosdelfile=CosFileupload.Initialize();
        cosdelfile.deleteObject(BUCKETNAME,key);
        cosdelfile.shutdown();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

碰磕

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值