我也来驯服 Triger !
来源:互联网 发布:get it beauty 编辑:程序博客网 时间:2024/06/02 14:54
传统的操作Properties的方法如下,它将属性存在一个类ini的文本文件中,下面是读取方法:
import java.util.*;
import java.io.*;
public class LoadSample {
public static void main(String args[]) throws Exception {
Properties prop = new Properties();
FileInputStream fis =
new FileInputStream("sample.properties");
prop.load(fis);
prop.list(System.out);
System.out.println("/nThe foo property: " +
prop.getProperty("foo"));
}
}
在tiger版本中增加了用xml 文件保存属性的方法,操作方法与原来一样,只是加上了xml的字样:
import java.util.*;
import java.io.*;
public class LoadSampleXML {
public static void main(String args[]) throws Exception {
Properties prop = new Properties();
FileInputStream fis =
new FileInputStream("sampleprops.xml");
prop.loadFromXML(fis);
prop.list(System.out);
System.out.println("/nThe foo property: " +
prop.getProperty("foo"));
}
}
import java.util.*;
import java.io.*;
public class StoreXML {
public static void main(String args[]) throws Exception {
Properties prop = new Properties();
prop.setProperty("one-two", "buckle my shoe");
prop.setProperty("three-four", "shut the door");
prop.setProperty("five-six", "pick up sticks");
prop.setProperty("seven-eight", "lay them straight");
prop.setProperty("nine-ten", "a big, fat hen");
FileOutputStream fos =
new FileOutputStream("rhyme.xml");
prop.storeToXML(fos, "Rhyme");
fos.close();
}
}
这是新的属性文件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>Rhyme</comment>
<entry key="seven-eight">lay them straight</entry>
<entry key="five-six">pick up sticks</entry>
<entry key="nine-ten">a big, fat hen</entry>
<entry key="three-four">shut the door</entry>
<entry key="one-two">buckle my shoe</entry>
</properties>
使用 XML 文件还是使用老式的 a=b 类型的文件完全取决于您自己。老式文件从内存的角度看肯定是轻量级的。不过,由于 XML 的普遍使用,人们会期望 XML 格式流行起来,因为它已经被广泛使用了,只不过没有用到 Properties 对象。
2. 驯服 Tiger:新的StringBuilder类
StringBuffer被设计为线程安全的,但是有时我们想在单线程中使用StringBuffer的提供的功能,StringBuffer的线程安全性会带来性能损失,StringBuilder正是为此而设计的。
3. 驯服 Tiger:格式化输出
格式化字符串:
不是用加号将字符串连接在一起(如 firstName + " " + lastName),而是先提供一个字符串描述输出形式,然后提供参数替换字符串中的占位符。
Formatter 类(java.util.Formatter):
您可能不会经常直接使用这个类,但是它提供了所要进行的格式化的内部机制。在这个类的 Javadoc 中,会看到一个描述所支持的格式化选项的表。用 Formatter 格式化输出分为两步:创建一个 Appendable 对象以存储输出,用 format() 方法将带格式的内容放到这个对象中。下面列出了 Appendable 接口的实现器:
· BufferedWriter
· CharArrayWriter
· CharBuffer
· FileWriter
· FilterWriter
· LogStream
· OutputStreamWriter
· PipedWriter
· PrintStream
· PrintWriter
· StringBuffer
· StringBuilder
· StringWriter
· Writer
在使用 Formatter 类时,将实现了Appendable接口的对象传递给构造函数,把它作为扩充目标。清单 1 显示了通常如何开始使用 Formatter:
StringBuilder sb = new StringBuilder(); Formatter formatter = new Formatter(sb, ocale.US); |
创建了Formatter 类后,用格式化字符串和参数调用其 format() 方法。如果需要使用与传递给出构造函数的不同的 Locale 作为格式化输出的一部分,还可以向 format() 方法传递一个 Locale 对象。
清单 2. 演示得到精度为 10 位数字的 Pi 值
public static void main(String args[]) { StringBuilder sb = new StringBuilder(); Formatter formatter = new Formatter(sb, Locale.US); formatter.format("PI = % System.out.println(formatter); } |
PrintStream 支持:
用于写入标准输出和标准错误的 System.out 和 System.err 对象都是PrintStream的实例。新的PrintStream引入了两个新的构造函数(用于直接写入文件)和六个方法(三对)以提供对格式化的支持。六个方法中的第一对(append)实现了新的 java.lang.Appendable 接口定义的方法,一般直接调用的是 format() 和 printf(),其中 printf() 版本只是 format() 版本的方便的包装器,如清单 3 所示:
清单 3. 使用 PrintStream.format 的例子
public class Now { public static void main(String args[]) { System.out.format( "Today is %1$tB %1$te, %1$tY.", Calendar.getInstance() ); } } |
运行这个程序的输出是 Today is April 2, 2004.,当然实际的输出取决于运行这个程序的日期。上面代码中的格式化字符串 %1$tB 告诉程序使用第一个参数并打印 date 对象的完整月名。格式化字符串 %1$te 意味着显示月中的日期,而格式化字符串 %1$tY 是四位数字的年。在 Formatter 对象的 Javadoc 中列出了打印日期的其他选项。
String 支持:
String 类有两个新的 static format() 方法,它们的作用与相应的 printf() 类似。发送一个格式化字符串和参数(还可能有 Locale)、并使用在格式化字符串中指定的格式转换参数。这些方法不是很显眼,但是有了它们就可以避免直接使用 Formatter 对象并创建中间的 StringBuilder。
格式化任意对象:Formattable 接口
前面描述如何使用新的格式化能力对已有对象和基本数据进行格式化。如果希望用 Formatter 提供对自己的对象的支持,那么就要用到 Formattable 接口。通过在自己的类中实现如清单 4 所示的一个 formatTo() 方法,可以对自已的类使用格式化字符串:
清单4. Formattable接口方法formatTo
void formatTo(Formatter formatter, int flags, Integer width, Integer precision) |
清单 5 演示了通过提供一个具有 name 属性的简单类来使用 Formattable 接口。这个 name 显示在输出中,它支持控制输出的宽度和对齐。
public class MyObject implements Formattable { String name; public MyObject(String name) { this.name = name; } public void formatTo( Formatter fmt, int f, Integer width, Integer precision) { StringBuilder sb = new StringBuilder(); if (precision == null) { // no max width sb.append(name); } else if (name.length() < precision) { sb.append(name); } else { sb.append( name.substring(0, precision - 1)). append('*'); } //apply width and justification if ((width != null) && (sb.length() < width)) { for (int i = 0, n=sb.length(); i < width - n; i++){ if ((f & Formattable.LEFT_JUSTIFY) == Formattable.LEFT_JUSTIFY) { sb.append(' '); } else { sb.insert(0, ' '); } } } fmt.format(sb.toString()); } public static void main(String args[]) { MyObject my1 = new MyObject("John"); MyObject my2 = new MyObject("Really Long Name"); //Using toString() System.out.println("First Object : " + my1); // Second / Using Formatter System.out.format("First Object : '%s'//n", my1); // Second / Using Formatter System.out.format("Second Object: '%s'//n", my2); // Second / Using Formatter with width System.out.format( "Second Object: '%10.5s'//n", my2); //Using Formatter with width and left justification System.out.format( "Second Object: '%-10.5s'//n", my2); } } |
运行这个程序会生成如清单 6 所示的输出。前两行展示了使用 toString 和 Formatter 的不同结果。后三行显示宽度和对齐控制的选项。
First Object : MyObject@10b First Object : 'John' Second Object: 'Really Long Name' Second Object: ' Real*' Second Object: 'Real* ' |
结束语
除非通过使用 C 对它们已经很熟悉了,否则掌握 Formatter 中提供的所有格式化选项要花一些时间。有一些小的区别,但是大部分的行为是非常类似的。Java 平台上的一个重要的不同是当格式化字符串无效时会抛出异常。
一定要仔细查看 Formatter 类的 Javadoc 中所列出的可用的格式化字符串。在创建自己的自定义类时,不仅提供一个 toString() 实现,而且实现 Formattable 接口通常会有帮助。
- 我也来驯服 Triger !
- 我来也
- VB我来也
- 成都,我来也!
- 我来也
- 我来也
- 我来也
- Blog 我也来
- 我也来发表
- 我也来了
- 我也来写写...
- 我来也
- 我来也
- 我来也
- 我来也
- 勇浩,我来也!
- 我也来CSDN
- 我来也
- 像Booch一样思考
- 2k3风格菜单
- 教你怎么撬走别人的女友--官方教程
- 公司急聘“软件开发人才”请有意者E-mail简历到如下地址:web@box-hill.com.cn
- DOM中对象的方法
- 我也来驯服 Triger !
- 0831【总结】有关在eclipse、lomboz和jboss下的J2EE-EJB
- 小白的情感日志!!—— 第2章
- XSL函数
- 第一期:CSDN论坛秀-Delphi版-本期作秀:myling(阿德)
- [ER/Studio]进行不同版本数据库结构的合并
- 第二期:CSDN论坛秀-Delphi版-本期作秀:aiirii(爱的眼睛)
- 第三期:CSDN论坛秀-Delphi版-本期作秀:wxjh(农民)
- 创建IE快捷方式