Java读取properties配置文件

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

Java读取properties配置文件

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

public class Property {
	
	private static Map<String, String> propertyMap = new HashMap<String, String>();

	private String fileName;
	
	public Property(String fileName) {
		this.fileName = fileName;
	}
	
	public String getProperty(String key) throws FileNotFoundException, IOException {
		String value = Property.propertyMap.get(key);
		Properties property = new Properties();
		FileInputStream inputFile = null;
		
		if (value == null) {
			// 实例化inputFile,如config.properties文件的位置
			inputFile = new FileInputStream(this.fileName);
			// 装载配置文件
			property.load(inputFile);
			
			value = property.getProperty(key);
			Property.propertyMap.put(key, value);
		}
		return value;
	}
	
	public Map<String,String> getProperty(List<String> propertyList) throws FileNotFoundException, IOException {
		// 定义Map用于存放结果
		Map<String,String> propertyMap = new HashMap<String,String>();
		// 定义Properties property = new Properties();
		Properties property = new Properties();
		// 定义FileInputStream inputFile = null;	
		FileInputStream inputFile = null;
		
		try {
			// 实例化inputFile
			inputFile = new FileInputStream(this.fileName);
			// 装载配置文件
			property.load(inputFile);
			for (String name : propertyList) {
				// 从配置文件中获取属性存入map中
				String data = property.getProperty(name);
				propertyMap.put(name, data);
			}
		
		} finally {
			// 关闭输入流
			if (inputFile != null) {
				inputFile.close();
			}
		}
		
		return propertyMap;
	}
}


 

Java如何读取properties配置文件 Java 读写properties配置文件 Java 读写Properties配置文件,利用Properties的属性load、setProperty、getProperty。 load(InputStream inStream)方法可以从.properties属性文件对应的文件输入流中,加载属性列表到Properties类对象 getProperty/setProperty这两个方法是分别是获取... 阅读详情

相关推荐

NFT图片批量合成及稀有度计算工具

一、制作背景 -当前NFT作品制作的大多方式都是使用部件拼接的形式,然后从大量的可能性结果取优; 二、解决问题 拼接合成的组合数量十分巨大,手工合成费时费力; 合成中对各个部件的使用次数不容易控制,部位出现的概率对作品的稀有度及评分影响较大; 对于生成的作品各个部位的信息(包括来源和使用率)统计繁琐,不利于对作品的介绍中体现; 三、程序功能介绍 1、功能自定义部位稀有度批量合成NFT作品 步骤一:创建各个合成部件文件夹,将图片放到文件夹内 步骤二:软件上设置各个部件对于的图片文件夹及出现的概率 步骤二

Keeping111的博客 4641

配置文件配置Map及List

通过上面的配置可知,一般的单个的值是使用field:value的形式配置的,而List类型的值则是使用多个-value进行配置的,而Map类型的值,则是使用field : key : value进行配置的,可以配置多个key:value。优先读取properties文件,如果properties文件中没有再去读yml文件。properties优先级会高一些,相同名称的配置,会将yml内的配置覆盖掉。介绍一下springBoot中的两种文件配置方式中数组的使用,也就是集合。

qq_44447372的博客 1147

HFSS仿真中如何提取最大E/H场强与频率关系曲线

本文详细讲解了在HFSS仿真中提取最大E/H场强与频率关系曲线的完整流程。通过使用场计算器(Field Calculator)和创建场报告,工程师可以准确获得器件在整个频带内的电场或磁场强度峰值,并进一步推导出关键的功率容量曲线,为高功率射频器件(如滤波器、耦合器)的设计提供至关重要的安全评估依据。

ss78901的博客 1099

Java【代码 11】yaml配置List和Map参数对象的配置信息及类文件实例分享(效仿GatewayDynamic+DynamicDataSource的注入方法)

本文介绍了YAML配置文件中List和Map类型参数的配置与注入方法。通过分析Spring Cloud Gateway和DynamicDataSource的源码配置,展示了两种实现方式:1) List方式通过嵌套对象列表配置,适合有序数据;2) Map方式通过键值对配置,适合需要额外键信息的场景。文章提供了自定义配置类的实现示例,包括带前缀的配置类定义和对应的YAML配置格式,并比较了两种方式的适用场景,强调需注意配置前缀与字段的对应关系。

Java与大数据相关实践内容分享! 7567

从哈工大NLP期末考卷里,我总结了一份2024年自学自然语言处理的避坑指南

本文通过分析哈工大NLP期末考卷,提炼出2024年自学自然语言处理的七大黄金法则,涵盖语言学基础、传统算法和深度学习三大支柱。文章揭示了自学者易混淆的概念雷区,并提供了渐进式学习路线、计算题技巧、精选资源过滤法则及面试高频考点,帮助读者系统掌握NLP核心知识,避开常见学习陷阱。

weixin_34180452的博客 437

告别连接难题:Windows 11下Multisim主数据库稳定运行终极配置指南

本文详细解析了Windows 11下Multisim 14.0主数据库连接失败的三大根源,并提供了四步终极解决方案,包括系统环境优化、软件权限配置、注册表修复和网络验证绕过技巧。通过实测数据对比,本方案显著提升了启动速度和稳定性,解决了电子仿真中的常见连接问题。

weixin_33738555的博客 437

有了这个网络安全面试题,面试就像开了挂!(附PDF)

还有兄弟不知道网络安全面试可以提前刷题吗?费时一周整理的160+网络安全面试题,金九银十,做网络安全面试里的显眼包!王岚嵚工程师面试题(附答案),只能帮兄弟们到这儿了!如果你能答对70%,找一个安全工作,问题不大。对于有1-3年工作经验,想要跳槽的朋友来说,也是很好的温习资料!【完整版领取方式在文末!!​​​​内容实在太多,不一一截图了。

lvaolan的博客 2507

GPU硬件架构与微架构深度解析34-GPU安全架构深度解析

现代GPU不仅是图形处理器,更是高性能计算平台,安全架构设计至关重要。内存加密- 保护数据在传输和存储时的安全安全启动- 确保固件和软件的完整性隔离机制- 防止不同应用间的数据泄露密钥管理- 安全存储和处理加密密钥。

2301_79143213的博客 101

android java properties文件_Android读取properties配置文件的实例详解

Android读取properties配置文件的实例详解因为一些配置信息,多处用到的。且以后可能变更的,我想写个.prorperties配置文件给管理起来。我把配置文件放在了assets文件夹下appConfig.properties:serverUrl=http://192.168.1.155import java.io.InputStream;import java.util.Properti...

weixin_34805229的博客 231

【个人学习总结】CRC校验原理及实现

CRC即循环冗余校验码(Cyclic Redundancy Check):是数据通信领域中最常用的一种查错校验码,其特征是信息字段和校验字段的长度可以任意选定。循环冗余检查(CRC)是一种数据传输检错功能,对数据进行多项式计算,并将得到的结果附在帧的后面,接收设备也执行类似的算法,以保证数据传输的正确性和完整性。 ..................

星Yeah 的博客 7968

Roboguide与TIA V16通讯

Roboguide与TIA V16通讯

haier123888的博客 2677

Springboot配置文件中配置List集合

Springboot配置文件中配置List集合

Any流年的博客 2822

火焰烟雾识别系统【数据集+源码+UI界面】

数据集准备:收集并标注火焰与烟雾数据。YOLOv10训练:训练YOLOv10模型来识别火焰和烟雾。实时检测:通过摄像头视频流进行实时检测,并显示检测结果。UI界面设计:用Tkinter设计简易界面,显示检测结果。完成以上步骤后,可以实现一个完整的火焰烟雾识别系统,具备实时检测与友好的用户界面。

2201_76125393的博客 1136

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

一、前言 在做Java项目开发过程中,涉及到一些数据库服务连接配置、缓存服务器连接配置等,通常情况下我们会将这些不太变动的配置信息存储在以 .properties 结尾的配置文件中。当对应的服务器地址或者账号密码信息有所变动时,我们只需要修改一下配置文件中的信息即可。同时为了让Java程序可以读取 .properties配置文件中的值,Java的JDK中提供了java.util.Properties类可以实现读取配置文件。 二、Properties类 Properties 类位于 java.util.Pro

Thinkingcao的专栏 5万+

深度学习工具包 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")

C# WinForm 基于DirectShow.dll编写的程序,可边预览边保存视频,音频和图片

基于微软的DirectShow.dll的二次开发1.自动识别连接的摄像头跟麦克风且可以对其进行选择(默认选中第一个)2.可录制wmv和avi两种格式的视频并且都带有音频,avi可以录制的分辨率和帧数进行选择,wmv默认进行了淸晰度优化如不喜欢可去除代码中有相关提示3.可在预览点开之后进行拍照,也可边录制视频边拍照4.添加了视频和音频的压缩功能(酌情使用)这次二次开发发现C#对DirectShow的相关信息太少,这次做完也不敢藏私共享出来,还望大家指正

Java 读取properties配置文件的方式

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

proven的博客 3506

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

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

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

痴人说梦 7万+

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

Java读取properties配置文件工具类

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

abcnull 的博客 2018

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

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

liuyunyihao的博客 8万+

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

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

cuisidong1997的博客 3266

java读取配置文件properties_Java读取properties配置文件

1 importjava.io.BufferedReader;2 importjava.io.IOException;3 importjava.io.InputStream;4 importjava.io.InputStreamReader;5 importjava.io.UnsupportedEncodingException;6 importjava.util.Enumeration;7 im...

weixin_32148209的博客 178

java读取properties key_java读取properties配置文件

目录结构package com.wish.config;import java.io.*;import java.util.HashMap;import java.util.Iterator;import java.util.Map;import java.util.Properties;/*** @Author: lfyu* @DateTime: 2020-06-15 10:37* @Proje...

weixin_32110907的博客 480

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

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

weixin_31667199的博客 1217

Java读取properties配置文件工具包

与 load 方法相反,该方法将键 - 值对写入到指定的文件中去。java中的properties文件是一种配置文件,主要用于表达配置信息,文件类型为*.properties,格式为文本文件,文件的内容是格式是 "键=值"的格式,在properties文件中,可以用"#"来作注释,properties文件在Java编程中用到的地方很多,操作很方便。通过对指定的文件(比如说上面的 test.properties 文件)进行装载来获取该文件中的所有键 - 值对。他通过调用基类的put方法来设置 键 - 值对。

Meryoufdd的博客 3748

Java的(.properties后缀)的配置文件介绍与读取(3种情况)

本篇博客的主要内容是关于Java的util包下Properties类、".properties"配置文件的认识与使用、读取。最后讲解了Java的配置文件位于3种不同的位置(整个项目/src目录/同包)下如何获取配置文件的资源...

岁岁岁平安的博客 4158
上一篇: Java初始化servlet数据
下一篇: 访问页面返回XML格式的数据
Android_Robot
博客等级 码龄15年 62粉丝 88原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值