首页 > 编程知识 正文

be动词的用法总结,匿名类有没有构造方法

时间:2023-05-03 05:32:19 阅读:119260 作者:4146

一、properties文件

属性文件是java中常用的配置文件之一,文件后缀为“. Properties”,是文本文件。 文件的内容形式为“键=值”的形式,可以将“#”用作注释。 java编程中使用的地方很多,通过活用轮廓,java在深层次的解除连接变得容易。 例如,如果Java APP应用程序通过JDBC连接到数据库,则通常需要在代码中写入数据库连接字符串。 以下代码显示了java通过JDBC连接到数据库的代码。 例如,mysql。

string driver=' com.MySQL.JDBC.driver '; //mysql提供的驱动程序接口的实现类stringjdbcurl=' JDBC : MySQL :///user '; //这里是' JDBC : MySQL ://localhost :3306/user '的简化形式,user是数据库名称String user='root '; String password='451535 '; class.forname (驱动程序; //反射导致mysql数据库驱动的连接连接=驱动程序管理器. getconnection (JDBC URL、user、password ); 上述代码连接到mysql数据库没有任何问题,但如果尝试更改为Oracle数据库,则会出现问题。 不是不能更改,而是必须从java源代码中修改代码。 这种硬代码绑定在java中很少见(可能是私人程序员)。 因此,要解除绑定,可以使用配置文件保存数据库的连接字符串。 粘贴要存储数据库连接字符串的properties配置文件jdbc.properties。

driver=com.mysql.jdbc.Driver

JDBC URL=JDBC : MySQL ://localhost :3306/user

用户根

password=451535

这样可以加载properties配置文件并连接到数据库,以实现深度解耦的目的。 更换为oracle或DB2时,只需更改配置文件即可,无需更改代码即可更换数据库。二、Properties类

java包含配置文件的操作类Properties类(java.util.Properties )。

publicclassPropertiesextendsHashtable .在Properties对象上上传,因为properties类继承Hashtable,hashtable实现了Map接口但是,这两种方法都不推荐,因为调用方可以插入键或值不是字符串的项目。 相反,必须使用setProperty方法。 如果在“不安全”Properties对象(即包含非String键或值的Properties对象)上调用store或save方法,则调用将失败。

属性的常用方法:

1.set property (字符串密钥,字符串值)。

调用Hashtable的方法put。

2.

获取属性(字符串密钥) )。

用指定的键在此属性列表中搜索属性

3.

获取属性(字符串密钥,字符串默认值)。

使用指定的键在属性列表中搜索属性。

4.

输入流(load ) )。

从输入流中读取属性列表(键-元素对)。

5.

load (读取器读取器)

以简单的面向行格式从输入字符流中读取属性列表(键-元素对)。

6.

loadfromxml(inputstreamin ) )。

将指定输入流中由XML文档表示的所有属性加载到此属性工作表中。

7.store (输出流输出,字符串注释) )。

以适合使用load(inputstream )方法加载到Properties表中的格式,将此Properties表的属性列表(键-元素对)写入输出流。

8.store (写入器写入器,字符串注释)。

将此Properties表的属性列表(键-元素对)以适合使用load(reader )方法的格式写入输出字符中。

9.storetoxml (输出流操作系统,字符串注释)。

发布表示此表中包含的所有属性的XML文档。

10.storetoxml (输出流操作系统、字符串注释、字符串编码) )。

使用指定的编码发布表示此表中包含的所有属性的XML文档。

以代码的形式了解Properties的具体用法(以JUnit单元测试的形式运行代码)1.为properties对象添加属性和获取值

@ test公共

void setAndGetProperty() { Properties pro=new Properties(); //设置值 pro.setProperty("driver", "com.mysql.jdbc.Driver"); pro.setProperty("url", "jdbc:mysql///user"); pro.setProperty("user", "root"); pro.setProperty("password", "451535"); //获取值: //1、getProperty(String key)方法 通过键获取值 String str= pro.getProperty("driver"); System.out.println(str); //2、getProperty(String key, String defaultValue)重载方法 //当properties对象中没有所指定的键值时,显示给定的默认值 String str2=pro.getProperty("driver", "没有该值"); String str3=pro.getProperty("haha", "没有该值"); System.out.println(str2); System.out.println(str3); }

运行结果:
com.mysql.jdbc.Driver
com.mysql.jdbc.Driver
没有该值
2.以properties配置文件格式写入到硬盘中的某个文件夹(本例写入到D盘的others文件夹中):

@Testpublic void storePropertiesToHardFile() throws FileNotFoundException, IOException{ Properties pro=new Properties(); pro.setProperty("driver", "com.mysql.jdbc.Driver"); pro.setProperty("url", "jdbc:mysql///user"); pro.setProperty("user", "root"); pro.setProperty("password", "451535"); //1.通过字节流的形式 //store(OutputStream out, String comments) //outputStream:字节输出流 comments:配置文件说明 pro.store(new FileOutputStream(new File("d:/others/jdbc.properties")), "数据库配置文件"); //2.通过字符流的形式 //store(Writer writer, String comments) //writer:字符输出流 comments:配置文件说明 pro.store(new FileWriter("d:/others/jdbc.properties"), "数据库配置文件"); }

运行后效果:

3.以XML配置文件格式写入到硬盘中的某个文件夹(本例写入到D盘的others文件夹中):

@Testpublic void storeXMLToHardFile() throws FileNotFoundException, IOException{ Properties pro=new Properties(); pro.setProperty("driver", "com.mysql.jdbc.Driver"); pro.setProperty("url", "jdbc:mysql///user"); pro.setProperty("user", "root"); pro.setProperty("password", "451535"); //1.不指定编码 默认为:UTF-8 //storeToXML(OutputStream os, String comment) //因为XML不是文本文件,所以只能用字节流,为不能用字符流 pro.storeToXML(new FileOutputStream("d:/others/jdbc.xml"), "数据库配置文件"); //1.不指定编码 //storeToXML(OutputStream os, String comment) //因为XML不是文本文件,所以只能用字节流,为不能用字符流 pro.storeToXML(new FileOutputStream("d:/others/jdbc2.xml"), "数据库配置文件", "GBK"); }

运行后效果:

4.以properties和XML配置文件格式写入到应用程序的某个文件夹(本例写入应用程序的classPath类路径下):

public void storeToClassPsth() throws FileNotFoundException, IOException{ Properties pro=new Properties(); pro.setProperty("driver", "com.mysql.jdbc.Driver"); pro.setProperty("url", "jdbc:mysql///user"); pro.setProperty("user", "root"); pro.setProperty("password", "451535"); pro.store(new FileOutputStream("src/jdbc.properties"), "数据库配置文件"); pro.storeToXML(new FileOutputStream("src/jdbc.xml") , "数据库配置文件"); }

运行后效果:

5.加载和读取配置文件(以properties文件为例)

public void loadAndReadFile() throws FileNotFoundException, IOException{ Properties pro=new Properties(); //通过字节输入流 //load(InputStream inStream) pro.load(new FileInputStream("src/sql.properties")); //通过类加载器 获取当前类路径 //类路径是指 / bin路径 pro.load(this.getClass().getResourceAsStream("/sql.properties")); pro.load(this.getClass().getClassLoader().getResourceAsStream("sql.properties")); //也可以使用当前上下文的类加载器,不用“/” pro.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("sql.properties")); //通过字符输入流 //load(Reader reader) pro.load(new FileReader("src/jdbc.properties")); System.out.println(pro.get("driver"));//以上几种加载配置文件的方法都可以使用,此处都列举出来。 }

运行结果:
com.mysql.jdbc.Driver
6.附上通过读取配置文件JDBC连接数据库的代码干货:

public Connection getConnection() throws Exception{ Properties info=new Properties(); info.load(this.getClass().getClassLoader().getResourceAsStream("jdbc.properties")); String driver=info.getProperty("driver"); String jdbcUrl=info.getProperty("jdbcUrl"); String user=info.getProperty("user"); String password=info .getProperty("password"); Class.forName(driver); Connection connection=DriverManager.getConnection(jdbcUrl,user,password); return connection; }

总结:通过读取配置文件的形式可以实现代码的深层次解耦,在java编程中,配置文件的重要性更是不言而喻。java编程有一条不成文的规定就是:“约定大于配置,配置大于代码”意思就是能用约定的就不去配置,能用配置文件搞定的就不去写代码,真正牛逼的攻城狮(工程师)不是写代码,而是写配置。java的一些开源框架,如:Struts、Struts2、Hibernate、Spring、MyBatis等都大量的使用配置文件,我们在学习和工作中,都应该将“约定大于配置,配置大于代码”的思想运用到项目中,实现代码的解耦,体现出你比别人的高明之处,才能比别人优秀。

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。