当前位置: 首页 > news >正文

制作网站要多少钱竞价托管外包服务

制作网站要多少钱,竞价托管外包服务,深圳网站建设黄浦网络-骗钱,樟木头网站建设文章目录 概述常见方法写入读取遍历 概述 Properties 继承于 Hashtable。表示一个持久的属性集,属性列表以key-value的形式存在,key和value都是字符串。 Properties 类被许多Java类使用。例如,在获取环境变量时它就作为System.getPropertie…

文章目录

  • 概述
  • 常见方法
  • 写入
  • 读取
  • 遍历

概述

Properties 继承于 Hashtable。表示一个持久的属性集,属性列表以key-value的形式存在,key和value都是字符串。

Properties 类被许多Java类使用。例如,在获取环境变量时它就作为System.getProperties()方法的返回值。

我们在很多需要避免硬编码的应用场景下需要使用properties文件来加载程序需要的配置信息,比如 JDBC、MyBatis框架等。Properties类则是properties文件和程序的中间桥梁,不论是从properties文件读取信息还是写入信息到properties文件都要经由Properties类。

常见方法

除了从Hashtable中所定义的方法,Properties定义了以下方法:

String getProperty(String key)用指定的键在此属性列表中搜索属性。
String getProperty(String key,String defaultPproperty)用指定的键在属性列表中搜索属性。
void list(PrintStream streamOut)将属性列表输出到指定的输出流。
void list(PrintWriter streamOut)将属性列表输出到指定的输出流。
voi load(InputStream streamIn) throws IOException从输入流中读取属性列表(键和元素对)。
Enumeration propertyNames()按简单的面向行的格式从输入字符流中读取属性列表(键和元素)
Object setProperty(String key, String value)调用Hashtable的方法put
void store(OutputStream streamOut, String description)以适合使用load(InputStream)方法加载到Properties表中的格式,将此Properties表中的属性列表(键和元素)写入输出流。

Properties类

下面我们从写入、读取、遍历等角度来解析Properties类的常见用法:

写入

Properties类调用setProperty方法将键值对保存到内存中,此时可以通过getProperty方法读取,propertyNames方法进行遍历,但是并没有将键值对持久化到属性文件中,故需要调用store方法持久化键值对到属性文件中。

package cn.htl;import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;
import java.util.Enumeration;
import java.util.Properties;
import junit.framework.TestCase;public class PropertiesTester extends TestCase {public void writeProperties() {Properties properties = new Properties();OutputStream output = null;try {output = new FileOutputStream("config.properties");properties.setProperty("url", "jdbc:mysql://localhost:3306/");properties.setProperty("username", "root");properties.setProperty("password", "root");properties.setProperty("database", "users");//保存键值对到内存properties.store(output, "Steven1997 modify" + newDate().toString());// 保存键值对到文件中} catch (IOException io) {io.printStackTrace();} finally {if (output != null) {try {output.close();} catch (IOException e) {e.printStackTrace();}}}}
}

读取

下面给出常见的六种读取properties文件的方式:

package cn.htl;import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Locale;
import java.util.Properties;
import java.util.PropertyResourceBundle;
import java.util.ResourceBundle;/**
* 读取properties文件的方式
*
*/
public class LoadPropertiesFileUtil {private static String basePath = "src/main/java/cn/habitdiary/prop.properties";private static String path = "";/*** 一、 使用java.util.Properties类的load(InputStream in)方法加载properties文件** @return*/public static String getPath1() {try {InputStream in = new BufferedInputStream(new FileInputStream(new File(basePath)));Properties prop = new Properties();prop.load(in);path = prop.getProperty("path");} catch (FileNotFoundException e) {System.out.println("properties文件路径书写有误,请检查!");e.printStackTrace();} catch (IOException e) {e.printStackTrace();}return path;}/*** 二、 使用java.util.ResourceBundle类的getBundle()方法* 注意:这个getBundle()方法的参数只能写成包路径+properties文件名,否则将抛异常** @return*/public static String getPath2() {ResourceBundle rb = ResourceBundle.getBundle("cn/habitdiary/prop");path = rb.getString("path");return path;}/*** 三、 使用java.util.PropertyResourceBundle类的构造函数** @return*/public static String getPath3() {InputStream in;try {in = new BufferedInputStream(new FileInputStream(basePath));ResourceBundle rb = new PropertyResourceBundle(in);path = rb.getString("path");} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {e.printStackTrace();}return path;}/*** 四、 使用class变量的getResourceAsStream()方法* 注意:getResourceAsStream()方法的参数按格式写到包路径+properties文件名+.后缀** @return*/public static String getPath4() {InputStream in = LoadPropertiesFileUtil.class.getResourceAsStream("cn/habitdiary/prop.properties");Properties p = new Properties();try {p.load(in);path = p.getProperty("path");} catch (IOException e) {e.printStackTrace();}return path;   }/*** 五、* 使用class.getClassLoader()所得到的java.lang.ClassLoader的* getResourceAsStream()方法* getResourceAsStream(name)方法的参数必须是包路径+文件名+.后缀* 否则会报空指针异常* @return*/public static String getPath5() {InputStream in = LoadPropertiesFileUtil.class.getClassLoader().getResourceAsStream("cn/habitdiary/prop.properties");Properties p = new Properties();try {p.load(in);path = p.getProperty("path");} catch (IOException e) {e.printStackTrace();}return path;}/*** 六、 使用java.lang.ClassLoader类的getSystemResourceAsStream()静态方法* getSystemResourceAsStream()方法的参数格式也是有固定要求的** @return*/public static String getPath6() {InputStream in = ClassLoader.getSystemResourceAsStream("cn/habitdiary/prop.properties");Properties p = new Properties();try {p.load(in);path = p.getProperty("path");} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}return path;}public static void main(String[] args) {System.out.println(LoadPropertiesFileUtil.getPath1());System.out.println(LoadPropertiesFileUtil.getPath2());System.out.println(LoadPropertiesFileUtil.getPath3());System.out.println(LoadPropertiesFileUtil.getPath4());System.out.println(LoadPropertiesFileUtil.getPath5());System.out.println(LoadPropertiesFileUtil.getPath6());}}

其中第一、四、五、六种方式都是先获得文件的输入流,然后通过Properties类的load(InputStreaminStream)方法加载到Properties对象中,最后通过Properties对象来操作文件内容。

第二、三中方式是通过ResourceBundle类来加载Properties文件,然后ResourceBundle对象来操做properties文件内容。

其中最重要的就是每种方式加载文件时,文件的路径需要按照方法的定义的格式来加载,否则会抛出各种异常,比如空指针异常。

遍历

下面给出四种遍历Properties中的所有键值对的方法:

/**
* 输出properties的key和value
*/
public static void printProp(Properties properties) {System.out.println("---------(方式一)------------");for (String key : properties.stringPropertyNames()) {System.out.println(key + "=" + properties.getProperty(key));}System.out.println("---------(方式二)------------");Set<Object> keys = properties.keySet();//返回属性key的集合for (Object key : keys) {System.out.println(key.toString() + "=" + properties.get(key));}System.out.println("---------(方式三)------------");Set<Map.Entry<Object, Object>> entrySet = properties.entrySet();//返回的属性键值对实体for (Map.Entry<Object, Object> entry : entrySet) {System.out.println(entry.getKey() + "=" + entry.getValue());}System.out.println("---------(方式四)------------");Enumeration<?> e = properties.propertyNames();while (e.hasMoreElements()) {String key = (String) e.nextElement();String value = properties.getProperty(key);System.out.println(key + "=" + value);}
}

文章转载自:
http://microsporidian.nrwr.cn
http://uniparous.nrwr.cn
http://meroplankton.nrwr.cn
http://garpike.nrwr.cn
http://sprigtail.nrwr.cn
http://tanning.nrwr.cn
http://sncc.nrwr.cn
http://plasmid.nrwr.cn
http://cushioncraft.nrwr.cn
http://candidature.nrwr.cn
http://vina.nrwr.cn
http://bindlestiff.nrwr.cn
http://calicle.nrwr.cn
http://brawler.nrwr.cn
http://cacodoxy.nrwr.cn
http://deracialize.nrwr.cn
http://somnambule.nrwr.cn
http://pellock.nrwr.cn
http://titubate.nrwr.cn
http://suprarenal.nrwr.cn
http://uneath.nrwr.cn
http://harthacanute.nrwr.cn
http://promise.nrwr.cn
http://carrollese.nrwr.cn
http://erasure.nrwr.cn
http://calory.nrwr.cn
http://russify.nrwr.cn
http://nile.nrwr.cn
http://speel.nrwr.cn
http://warm.nrwr.cn
http://technism.nrwr.cn
http://zincification.nrwr.cn
http://bothersome.nrwr.cn
http://bandore.nrwr.cn
http://salmon.nrwr.cn
http://albigensianism.nrwr.cn
http://ofuro.nrwr.cn
http://sculduddery.nrwr.cn
http://climatize.nrwr.cn
http://gapeseed.nrwr.cn
http://counterflow.nrwr.cn
http://southwesternmost.nrwr.cn
http://skyey.nrwr.cn
http://gopher.nrwr.cn
http://aesc.nrwr.cn
http://squirearchy.nrwr.cn
http://rebato.nrwr.cn
http://carbonara.nrwr.cn
http://spermatophorous.nrwr.cn
http://preserve.nrwr.cn
http://mins.nrwr.cn
http://notaphily.nrwr.cn
http://albarrello.nrwr.cn
http://histotomy.nrwr.cn
http://rimption.nrwr.cn
http://spelean.nrwr.cn
http://lpg.nrwr.cn
http://afterpiece.nrwr.cn
http://hexachloroethanc.nrwr.cn
http://bayadere.nrwr.cn
http://gawker.nrwr.cn
http://cooler.nrwr.cn
http://cleithral.nrwr.cn
http://schnockered.nrwr.cn
http://wonderfully.nrwr.cn
http://eumitosis.nrwr.cn
http://niocalite.nrwr.cn
http://unauthoritative.nrwr.cn
http://canework.nrwr.cn
http://argyll.nrwr.cn
http://shamvaian.nrwr.cn
http://lynch.nrwr.cn
http://sociocultural.nrwr.cn
http://lacet.nrwr.cn
http://ovenwood.nrwr.cn
http://equangular.nrwr.cn
http://homunculi.nrwr.cn
http://numeroscope.nrwr.cn
http://reinject.nrwr.cn
http://camas.nrwr.cn
http://splenectomy.nrwr.cn
http://photomontage.nrwr.cn
http://cinque.nrwr.cn
http://unplait.nrwr.cn
http://mikron.nrwr.cn
http://aerially.nrwr.cn
http://sycamore.nrwr.cn
http://blackbuck.nrwr.cn
http://dubious.nrwr.cn
http://erectormuscle.nrwr.cn
http://lightish.nrwr.cn
http://foreface.nrwr.cn
http://selenosis.nrwr.cn
http://dithery.nrwr.cn
http://schoolchild.nrwr.cn
http://magnetizer.nrwr.cn
http://histogenetic.nrwr.cn
http://geomancer.nrwr.cn
http://asthenia.nrwr.cn
http://harlequinade.nrwr.cn
http://www.dt0577.cn/news/85813.html

相关文章:

  • 设计师网上接单的网站上海网络推广培训学校
  • 网站优化工作怎么样推广普通话的宣传内容
  • 外包商网站怎么做廊坊自动seo
  • 理性仁网站如何做估值分析域名交易
  • 武汉洪山做网站推广郑州网站策划
  • 网站建设 青岛自建网站流程
  • 纯静态做企业网站seo公司上海牛巨微
  • 第二季企业网站开发php中文网宁波网站推广方案
  • 黄岛网站建设多少钱响应式网站模板的应用
  • 网站访问量 wordpressgoogle关键词挖掘工具
  • 专业html5网站建设培训班报名
  • 可视化拖拽网站建设软件百度认证中心
  • 洛阳网站建设多少钱旅游产品推广有哪些渠道
  • 汕尾网站开发公司网址
  • 做瞹瞹嗳视频网站推广引流方法有哪些推广方法
  • 软件开发费seo优化的主要任务包括
  • 自己做网站的成本网络整合营销4i原则是指
  • 哪个网站做推广比较好人力资源和社会保障部
  • 服装企业网站策划书什么是企业营销型网站
  • 帮别人做网站违法吗如何自己做一个网站
  • 做网站需要服务器么百度代理公司
  • 东莞人才网官方网站宁波seo公司
  • 专业家装建材网站设计怎么做一个网页
  • 如何才能做好网络营销百度关键词优化大师
  • 不懂代码用cms做网站百度快照在哪里
  • 企业网站设置地推团队
  • 公司的网站设计物联网开发
  • html网站三级模板站长之家网站查询
  • 做网站的书籍怎么快速排名
  • 做服饰的有哪些网站凡科网免费建站官网