Java读取properties配置文件工具包

Java读取配置文件的方式详解 可以使用 Spring 的 PropertySourcesPlaceholderConfigurer 来加载属性文件,该类可以将属性文件中的键值对解析成应用程序中的属性值。提高安全性:某些敏感的配置信息,如数据库连接字符串、密码等,不应该直接写在代码中,而是应该保存在安全的地方,使用配置文件可以将这些敏感信息保存在加密的配置文件中,提高了应用程序的安全性。在上述配置中,使用 @PropertySource 注解指定了配置文件的位置,并使用 @Value 注解将配置文件中的属性注入到类的成员变量中。 阅读详情


一、properties文件简介
java中的properties文件是一种配置文件,主要用于表达配置信息,文件类型为*.properties,格式为文本文件,文件的内容是格式是 "键=值"的格式,在properties文件中,可以用"#"来作注释,properties文件在Java编程中用到的地方很多,操作很方便。
一般使用以下两种方法读取properties文件:
1、通过java.util.Properties类读取
2、通过java.util.ResourceBundle类读取(国际化切换)

二、主要操作方法介绍
java.util.Properties类主要方法介绍:
1. getProperty ( String key),用指定的键在此属性列表中搜索属性。也就是通过参数 key ,得到 key 所对应的 value。
2. load ( InputStream inStream),从输入流中读取属性列表(键和元素对)。通过对指定的文件(比如说上面的 test.properties 文件)进行装载来获取该文件中的所有键 - 值对。以供 getProperty ( String key) 来搜索。
3. setProperty ( String key, String value) ,调用 Hashtable 的方法 put 。他通过调用基类的put方法来设置 键 - 值对。
4. store ( OutputStream out, String comments),以适合使用 load 方法加载到 Properties 表中的格式,将此 Properties 表中的属性列表(键和元素对)写入输出流。与 load 方法相反,该方法将键 - 值对写入到指定的文件中去。
5. clear (),清除所有装载的 键 - 值对。该方法在基类中提供。

 在Java项目中的config文件夹中存放了local.properties配置文件

新梦想技术分享

  核心代码如下:

复制代码
//读取Properties:  Properties p= new Properties();
p.load(new FileInputStream("config/local.properties") );
System.out.println(p.toString());

String name =p.getProperty("name");
System.out.println("name:"+name); 

String age = p.getProperty("age");
System.out.println("age:"+age);

String url = p.getProperty("url");
System.out.println("url:"+url); 
复制代码
新梦想技术分享

三、读取配置文件步骤
java.util.Properties类读取配置文件步骤:
1、使用getResourceAsStream方法读取properties文件
2、新建一个java.util.Properties类的对象
3、使用该对象的load方法加载
4、加载后使用getProperty方法读取所需要的配置值
5、关闭字节输入流

准备工作:将local.properties配置文件放/src/main/java 下面 

核心代码如下:

复制代码
//读取Properties方法2    注意:local.properties文件存放的位置 InputStream in= readProperties.class.getClassLoader().getResourceAsStream("local.properties");
Properties p= new Properties();
p.load(in);

System.out.println(p.toString());
String name =p.getProperty("name").trim();
System.out.println("name:"+name);

String age =p.getProperty("age").trim();
System.out.println("age:"+age);

String url = p.getProperty("url");
System.out.println("u:"+url);
in.close();
复制代码

【JAVA开发小技巧】Properties类读取properties、yml配置文件中的内容 当我们将项目部署到服务器上之后,我们不能专门地对代码中的常量进行修改,除非更新部署新的项目,但是我们的项目在上次更新之后,很有可能在其它方面也做了更新升级,这些更新的内容只有在通过测试之后才有资格部署到生产环境下。所以我们要想办法,解除常量与项目代码的耦合度,解决办法就是,当我们将项目部署到生产环境之后,能将项目中的常量进行配置化的管理。也就是说,项目中的常量最好是来源于配置文件的读取,这样我们只要修改配置文件即可修改项目中的常量。 阅读详情

相关推荐

新手避坑指南:用SARScape 5.6.2处理Sentinel-1数据做SBAS-InSAR,这些参数设置错了白忙活

本文详细介绍了使用SARScape 5.6.2处理Sentinel-1数据做SBAS-InSAR时的关键参数设置技巧,帮助新手避免常见错误。重点解析了连接图生成、干涉工作流、DEM处理等环节的12个关键参数,包括Max Normal Baseline、Unwrapping Coherence Threshold等,提供实测安全值范围和优化建议,确保处理效率和结果准确性。

weixin_30694389的博客 557

Java读取Properties配置文件的6种方式

Java读取Properties配置文件的6种方式

yirenDM的博客 6527

STM32H723ZGT6-单ADC极限采样率实战与分辨率权衡

本文通过实战测试,深入探讨了STM32H723ZGT6微控制器中单ADC模块的极限性能。文章详细展示了如何通过CubeMX配置时钟树、ADC参数、定时器触发与DMA,实测得出在16位分辨率下最高稳定采样率约为4.6MHz,而将分辨率降至8位后,采样率可提升至约6.2MHz,并分析了速度与分辨率之间的关键权衡,为高速数据采集项目提供选型与优化参考。

weixin_29240711的博客 297

Java中读取properties配置文件的八种方式总结

【代码】Java中读取properties配置文件的八种方式总结。

ღ᭄ꦿℒℴѵℯ青城এ⁵²º的博客 6129

Win64 驱动内核编程-16.WFP网络监控驱动(防火墙)

WFP驱动监控网络 WFP是微软推出来替代TDIHOOK、NDISHOOK等拦截网络通信的方案,WFP的框架非常庞大,在RING3和RING0各有一套类似的函数,令人兴奋的是,即使在R3使用WFP,也可以做到全局拦截访问网络。由于WFP的范围太广,实在难以一言概括,感...

墨鱼菜鸡 1033

Ubuntu Server 24.04 安装 (超详细)

本文介绍最小化安装 Ubuntu Server的详细过程。

炀炀的专栏 1万+

读取properties文件的6种方式,建议收藏!

以上就是我们通常在java中读取properties文件的6中方式。

m0_73311735的博客 4550

VisionMaster图像源模块的5个隐藏功能:从自动切换到二次开发接口

本文深入解析了VisionMaster图像源模块的5个隐藏功能,从基础的自动切换与字符触发过滤,到高级的脚本扩展与基于COM组件的二次开发接口。这些功能能显著提升工业视觉项目的自动化水平、灵活性与系统集成能力,帮助开发者实现更高效的离线测试流水线和外部精确控制。

rgv23456789的博客 651

Java读取properties配置文件的8种方式汇总

Properties 类位于 java.util.Properties中,是Java 语言的处理配置文件所使用的类,其中的xxx.Properties类主要用于集中的持久存储Java的配置文件内容,可以读取后缀是.properties和.cfg的配置文件。Properties类调用setProperty方法将键值对保存到内存中,此时可以通过getProperty方法读取,propertyNames方法进行遍历,但是并没有将键值对持久化到属性文件中,故需要调用store方法持久化键值对到属性文件中。

cuisidong1997的博客 3265

Xilinx FPGA平台GTX简易使用教程(五)GTX收发测试及示例工程介绍

GTX IP配置完了,你不得搞个回环测试一番?

数字IC/FPGA杂货铺,找一找,或许有你想要的~ 1万+

机器视觉框架源码——解读5(Dock和开源UI)

本文介绍了工业软件中Dock工具面板的使用方法,主要包括三个部分:添加avalonDock引用、修改属性配置(如显示样式和背景色),以及通过MVVM命令实现布局的加载和保存。加载布局通过XmlLayoutSerializer从XML文件恢复界面状态,保存布局则将当前DockManager的布局序列化为XML文件。文章提供了代码示例和截图说明,帮助开发者快速掌握Dock面板的核心功能。

Z's Palace 401

java读取.properties配置文件的几种方法

读取.properties配置文件在实际的开发中使用的很多,总结了一下,有以下几种方法(仅仅是我知道的): 一、通过jdk提供的java.util.Properties类。 此类继承自java.util.HashTable,即实现了Map接口,所以,可使用相应的方法来操作属性文件,但不建议使用像put、putAll这两个方法,因为put方法不仅允许存入String类型的value,还可以存入O

痴人说梦 7万+

博图SCL基础知识-寻址调用及新建SCL

和LAD/FBD不同,LAD/FBD在程序编辑器是一段一段的,编辑器可以插入新的网络段,每一个网络段可以有各自的注释。一般情况下,FB的简单数据类型形参可以没有对应实参,复杂数据类型的输入、输出也可以没有对应实参,所以FB可以隐藏或不隐藏不出现的形参。一般情况下,FB的简单数据类型形参可以没有对应实参,复杂数据类型的输入、输出也可以没有对应实参,所以FB可以隐藏或不隐藏不出现的形参。如图3所示,当FB的参数全部显示,在背景数据块右键可以激活"仅显示分配的参数";如图2所示,显示了一些FB调用的例子。

电工 2029

读取properties配置文件的几种方法

我们常常需要将一些配置参数放置在XML 配置文件或者 properties 配置文件中,增强程序灵活性。我总结下读取 properties 配置文件的几种方法,供需要时使用。 假设我有一个config.properties文件:(键值对形式),现在需要读取出来 className=demo.Teacher methodName=teach 方式一:properties类 public class propertiesTest{ public static void main(String[] arg

这知识不进我脑袋呀 5289

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

锂电池充电器电路设计 里面有电路

锂电池充电器电路设计 里面有电路 3.7V升压到5V电路设计。

java读取properties配置文件的几种方式

项目中经常将一些配置信息放到properties文件中,读取非常方便,下面介绍几种java读取properties配置文件的方式。先看示例的properties文件: 方法一,基于InputStream读取配置文件: public static void main(String[] args) { readProperty1(); } //方法一...

liuyunyihao的博客 8万+

SIMATIC S7-1200, S7-1500 PID 控制[手册]

SIMATIC S7-1200, S7-1500 PID 控制[手册]

Java 读取properties配置文件的方式

在我们的代码中,经常需要将一些值放到配置文件中读取,这样的好处就是可以不需要重新编译代码就可以改变一些系统的属性,今天分享一个用java 读取properties配置文件的方法。 我把它直接写成了一个工具类,我现在每个项目开始之前,总要将这个工具类先放进去,具体方法是:可以直接将这个工具类放入util 包中就可以的使用里面的方法实现读取和写配置的功能了! 下面直接上代码 package com.p...

proven的博客 3505

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

Java读取properties配置文件工具类

Java读取properties配置文件的方式 一.背景 Java读取 properties 配置文件的方式,主要是通过 getProperty() 方法。config.properties 文件中有诸如 config.a = xxx 这样的存储形式,下面代码是读取 properties 配置文件中参数数据到 String 类型变量中。 二.代码 // 新建一个拥有 config.properti...

abcnull 的博客 2017

Java读取properties配置文件的几种方式

Java读取Properties配置文件的几种方式 在这里整理一下Java读取配置文件的方式,本文的环境是Springboot+ssm 一、学习路线 1、什么是配置文件? 2、配置文件的好处? 3、怎么使用? 4、怎样使用Java进行读取? 二、什么是配置文件 配置文件其实可以理解位资源文件,而这个资源文件可以包含一些固定的参数,配置文件一般分为两种:Properties 和 xml形式, ...

@°的博客 3525

Java读取properties配置文件

这里介绍两种技术:利用spring读取properties 文件和利用java.util.Properties读取(一)利用spring读取properties 文件利用org.springframework.beans.factory.support.PropertiesBeanDefinitionReader来读取属性文件 构造如下config.properties文件properties

Jenny的人生旅途 3万+

java读取配置文件properties_Java读取.properties配置文件的几种方式

Java 开发中,需要将一些易变的配置参数放置再 XML 配置文件或者 properties 配置文件中。然而 XML 配置文件需要通过 DOM 或 SAX 方式解析,而读取 properties 配置文件就比较容易。介绍几种读取方式:1、基于ClassLoder读取配置文件注意:该方式只能读取类路径下的配置文件,有局限但是如果配置文件在类路径下比较方便。Properties properties...

weixin_31667199的博客 1214

Java 读取 Properties 文件

案例一 主要使用了spring-core-4.1.4.RELEASE-sources.jar 这个jar包里的 PropertiesLoaderUtils.loadProperties 方法。不说了,直接上代码: package cn.lyj.util; import java.util.HashMap; import java.util.Map; import java.u...

小半的博客 2262

Java 读取application.properties配置文件中配置

  实际开发中若需要读取配置文件application.properties中的配置,代码如下。例:读取配置文件中name属性配置值:   代码如下: import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; import org.s...

weixin_30502965的博客 3273

Java Application下读取properties配置文件

在java应用程序开发中,经常需要读取配置文件来获取系统参数或配置信息。配置文件可以使用xml格式文件,在java中存在.properties文件专门用作配置文件使用。在java中,类Properties用于处理配置文件相关的读取。下面是一个关于根据所提供的键获取值的示例。 public static String getvalue(String key) { Properties p=n

guorain32188的专栏 6602

Java读取properties配置文件三种方式

原文链接:java读取properties配置文件的几种方式 项目中经常将一些配置信息放到properties文件中,读取非常方便,下面介绍几种java读取properties配置文件的方式。先看示例的properties文件: 1.基于InputStream读取配置文件 根据实际情况处理转码问题 // 通过InputStream读取配置文件 private static void readPropertiesByInputStream() { Properties pro

Eamon 1017
上一篇: python实用小技之数据结构
下一篇: 测试用例设计标准
Meryoufdd
博客等级 码龄5年 13粉丝 14原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值