java往properties文件中写配置信息

上一节我们学习了读取properties文件中的信息这一节我们来往配置文件中写信息

 

程序代码如下


  
  System.out.println("进来了");
  
  String address = request.getParameter("address");
  String port = request.getParameter("port");
  String username = request.getParameter("username");
  String pass = request.getParameter("pass");
  String dbname = request.getParameter("dbname");
  
  String url = "jdbc:mysql://"+address+":"+port+"/"+dbname;
  
  String path = request.getRealPath("");
//  数据库配置文件路径,这里要千万注意不要写相对路径,刚开始我就写的是src对应的路径,后来经过多次尝试终于找到原因了,必须写web-inf 下classes文件下的路径这样就是物理路径就正确了
  String conPath1 = path+"
\\WEB-INF\\classes\\com\\rhcy\\config\\db.properties";
  String dbUrl = path+"
\\WEB-INF\\classes\\com\\rhcy\\config\\lwz.sql";
 


  File file = new File(conPath1);
  try {

//创建文件
   file.createNewFile();
  } catch (IOException e) {
   
   e.printStackTrace();
  }
  
  Properties properties = new Properties();

  
    try {
   InputStream fis = new FileInputStream(conPath1);
   try {
    properties.load(fis);
   } catch (IOException e) {
    
    e.printStackTrace();
   }
   OutputStream fos = new FileOutputStream(conPath1);
   
   
   properties.setProperty("driver","com.mysql.jdbc.Driver");  
   properties.setProperty("url",url);  
   properties.setProperty("username",username);  
   properties.setProperty("password",pass);  
   
   
   try {
    properties.store(fos,null);
   } catch (IOException e) {
    
    e.printStackTrace();
   }
   
   
  } catch (FileNotFoundException e) {
   
   e.printStackTrace();
  }         
  
  
  

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值