分类: Java
2008-10-13 11:34:42
Mustang的release包含了下列内容:
• JSR 105: XML Digital Signature
• JSR 173: Streaming API for XML
• JSR 181: Web Services Metadata
• JSR 199: Java Compiler API
• JSR 202: Java Class File Specification Update
• JSR 221: JDBC 4.0
• JSR 222: JAXB 2.0
• JSR 223: Scripting for the Java Platform
• JSR 224: Java API for XML-Based Web Services (JAX-WS) 2.0
• JSR 250: Common Annotations
• JSR 269: Pluggable Annotation Processing API
除了上述这些,它还有下面几个目标:
----兼容性和稳定性
----可诊断性、监控和管理
----易于开发
----企业级的桌面
----XML以及Web服务
----透明性(?何为透明性?)
新的package:
Package Description
java.text.spi Service provider classes for java.text package
java.util.spi Service provider classes for java.util package
javax.activation Activation Framework
javax.annotation Annotation processing support
javax.jws Web services support classes
javax.jws.soap SOAP support classes
javax.lang.model.* For modeling a programming language and processing its elements
and types
javax.script Java Scripting Engine support framework
javax.tools Provides access to tools, such as the compiler
javax.xml.bind.* JAXB-related support
javax.xml.crypto.* XML cryptography–related support
javax.xml.soap For creating and building SOAP messages
javax.xml.stream.* Streaming API for XML support
javax.xml.ws.* JAX-WS support
使用的常用代码片段如下:
Desktop desktop = Desktop.getDesktop();
if (desktop.isSupported(Desktop.Action.OPEN)) {
...
}
这里并不问你是否支持打开某一种Mime类型,而是询问本地系统是否支持打开操作。
示例:使用本地程序打开文件.
import java.awt.*;
import java.io.*;
public class DesktopTest {
public static void main(String args[]) {
if (!Desktop.isDesktopSupported()) {
System.err.println("Desktop not supported!");
System.exit(-1);
}
Desktop desktop = Desktop.getDesktop();
String path;
if (args.length == 0) {
path = ".";
} else {
path = args[0];
}
File dir = new File(path);
File files[] = dir.listFiles();
for (File file: files) {
System.out.println("Open " + file.getName() + "? [YES/NO] :");
if (desktop.isSupported(Desktop.Action.OPEN)) {
String line = System.console().readLine();
if ("YES".equals(line)) {
System.out.println("Opening... " + file.getName());
try {
desktop.open(file);
} catch (IOException ioe) {
System.err.println("Unable to open: " + file.getName());
}
}
}
}
}
}
同以前版本一样,不需要显示的import这个包。但是在Java 6里,有两个显示的变化:
--- Console的输入和输出
--- 空字符串的检查
先看看Console的输入、输出:
public class Output {
public static void main(String args[]) {
String string = "Español";
System.out.println(string);
System.console().printf("%s%n", string);
}
}
结果为:
> java Output
Espa±ol
Español
Outputstream与console().printf()效果好像不太一样:)
再看看Console的输入,这里的例子是读取用户输入的密码
import java.io.Console;
public class Input {
public static void main(String args[]) {
Console console = System.console();
console.printf("Enter name: ");
String name = console.readLine();
char password[] = console.readPassword("Enter password: ");
console.printf("Name:%s:\tPassword:%s:%n",
name, new String(password));
}
}
效果非常好哦。
空字符串的检查:
if (myString.length() == 0) {
...
}
现在可以直接写成:
if (myString.isEmpty()) {
...
}
看下例:
public class EmptyString {
public static void main(String args[]) {
String one = null;
String two = "";
String three = "non empty";
try {
System.out.println("Is null empty? : " + one.isEmpty());
} catch (NullPointerException e) {
System.out.println("null is null, not empty");
}
System.out.println("Is empty string empty? : " + two.isEmpty());
System.out.println("Is non empty string empty? : " + three.isEmpty());
}
}
结果如下:
> java EmptyString
null is null, not empty
Is empty string empty? : true
Is non empty string empty? : false
日历显示名称
Deque(双端队列)
Map和Set的可导航(遍历)特性
Resource Bundle的控制
Array的复制
Lazy atomics(原语)
1. 日历显示名称
当定义好了DateFormat, 像01/02/03这样的日期,对于欧洲国,它表示为:February 1, 2003,对于美国,表示为January 2, 2003,可以消除这样的混淆。
过去,不被推荐调用DateFormatSymbols的getWeekDays()方法,现在可以使用Calendar的getDisplayNames()来代替了。
Map
Calendar.LONG, Locale.getDefault());
看看下边的例子:
import java.util.*;
public class DisplayNames {
public static void main(String args[]) {
Calendar now = Calendar.getInstance();
Locale locale = Locale.getDefault();
// Locale locale = Locale.ITALIAN;
Map
Calendar.DAY_OF_WEEK, Calendar.LONG, locale);
// System.out.println(names);
System.console().printf("%s%n", names.toString());
String name = now.getDisplayName(Calendar.DAY_OF_WEEK,
Calendar.LONG, locale);
System.console().printf("Today is a %s.%n", name);
}
}
> java DisplayNames
{Saturday=7, Monday=2, Wednesday=4, Sunday=1, Friday=6, Tuesday=3, Thursday=5}
Today is a Saturday.
java.util包的变化,
Deque(双端队列)
:)
双端队列Deque(Double-ended queue,念作Deck, :) ),普通的队列支持从一端加入元素,从另一端移除元素,而Deque则同时支持在两端添加或删除元素,从功能上来说,是栈和队列的组合。
Deque接口扩展了queue接口,这是在Java 5里引入的,并且是对集合框架的最新补充。该接口的实现包括:LinkedList, ArrayQueue, 以及并发的LinkedBlockingQueue.
添加元素:
addFirst(E e)
addLast(E e)
add(E e) 等同于addLast(), add系列方法总是返回true
另一套添加元素的方法:
offer(), offerFist(), offerLast(), 真实的返回是否添加成功的标志。
删除元素:
remove(), removeFirst(), removeLast(),队列为空时,会抛出NoSuchElementException,甚至可以指定删除特定的元素:remove(Object o), removeFirstOccurrence(o), removeLastOccurrence()
poll(), pollFirst(), pollLast(),队列为空时,返回null值
Deque还有6个用于检验元素的方法:
element(), getFirst(), getLast(), peek(), peekFirst(), peekLast(),但是没有get()方法
element方法是从queue接口里继承得到。get*类似于remove*会抛异常,而peek则会返回null。
Deque的遍历,iterator()从前到后,而descendingIterator()则从后到前。
可导航的Map和Set类