java文件读取和写入(文件读写java代码)

在这几天的学习过程中,有开发的朋友告知我,每个编程语言基本都有相应的配置文件支持类,像 Python 编程语言中支持的 ini 文件及其对应的配置文件读取类 ConfigParse,通过这个类,用户可以方便的修改 ini 配置文件。在 Java 中对应的配置文件为 properties 格式的文本文件,其对应的内容格式为 “键=值” ,文本注释信息可以用 “#” 注释。同时 Java 语言中对应的类为 Properties(java.util.Properties),即为读取 properties 文件的类,继承于 Hashtable (如下所示),用户使用这个类可以很方便的操作 properties 文件。

java文件读取和写入(文件读写java代码)

下面首先看一下其对应的结果主要方法,如下所示:

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

下面通过针对 Properties 配置文件的读取进行实例代码演示,以加深对 Properties 类的理解。

小二上码。。。

以下为原始的 properties 文件内容:

# oracle config db_type=oracle driver=oracle.jdbc.driver.OracleDriver host=127.0.0.1 port=1521 database=test user=aaron pass=ffp sql=select * from hr t where t.no = ‘1521’ desc=oracle\u6570\u636E\u5E93\u8FDE\u63A5\u914D\u7F6E

properties 配置文件方法的源码(返回 Properties):

/** * @function 文件读取: properties 文件 * @description PS:注意读取中文时的乱码处理 * * @author Aaron.ffp * @version V1.0.0: autoUISelenium main.java.aaron.java.tools FileUtils.java propertiesRead, 2014-11-20 16:29:09 Exp $ * * @param filename 文件路径 * @return Properties */public Properties propertiesRead(String filename){ Properties properties = new Properties(); /* 参数校验: 为null或空字符串时, 抛出参数非法异常 */ if (filename == null || “”.equals(filename) || !assertFileType(filename, “PROPERTIES”)) { throw new IllegalArgumentException(); } try { /* 获取文件数据流 */ InputStream is = new FileInputStream(filename); /* 加载文件数据流 */ properties.load(is); /* 关闭文件数据流 */ is.close(); } catch (IOException ioe) { this.message = “文件 {” filename “} 读取失败!”; this.logger.error(this.message, ioe); } return properties;}

测试方法源码:

/** * Test : properties read for txt file * * @author Aaron.ffp * @version V1.0.0: autoUISelenium test.java.aaron.java.tools FileUtilsTest.java test_propertiesRead, 2014-11-20 16:35:09 Exp $ * */public void test_propertiesRead(){ this.message = “\n\n\nTEST:FileUtils.propertiesRead(String filename)”; this.logger.debug(this.message); this.fu = new FileUtils(); String filename = this.constantslist.PROJECTHOME this.constantslist.FILESEPARATOR “testng-temp” this.constantslist.FILESEPARATOR “propertiesRead.properties”; Properties prop = this.fu.propertiesRead(filename); // get keys from properties Enumeration<?> enu = prop.propertyNames(); // print-1 prop.list(System.out); System.out.println(“\n\n”); // print-2 String key = “”; while (enu.hasMoreElements()) { key = (String)enu.nextElement(); System.out.println(key ” = ” prop.getProperty(key)); }}

程序运行结果如下所示:

PS:若需使用此文中的源码,需要将 properties 文件中的内容保存至本地,并将测试方法源码中的文件路径改为本地路径,同时对上述源码进行适当修改才可成功运行,请知悉!

至此,Java学习-019-Properties 文件读取实例源代码顺利完结,希望此文能够给初学 Java 的您一份参考。

最后,非常感谢亲的驻足,希望此文能对亲有所帮助。热烈欢迎亲一起探讨,共同进步。非常感谢!^_^

发表评论

登录后才能评论