Chinaunix首页 | 论坛 | 博客
  • 博客访问: 29943796
  • 博文数量: 708
  • 博客积分: 12163
  • 博客等级: 上将
  • 技术积分: 8240
  • 用 户 组: 普通用户
  • 注册时间: 2007-12-04 20:59
文章分类

全部博文(708)

分类: 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

 
一、JavaBean 激活框架(JavaBeans Activation Framework)

javax.activation.*, 这个框架实际上是比较旧的,过去一直是对JavaMail库和email附件的处理进行了包装。现在Mustang加强了它的功能,使它不只用于JavaMail,而是成了标准的Java API

CommandMap类提供了getDefaultCommandMap()方法,获取缺省的Command Map。这样,你可以得到CommandMap 对应的MimeType,也可以调用getAllCommands()得到一个MimeType的所有Command。看下例:

import javax.activation.*;
public class Commands {
    public static void main(String args[]) {
        CommandMap map = CommandMap.getDefaultCommandMap();
        String types[] = map.getMimeTypes();
        for (String type: types) {
            System.out.println(type);
            CommandInfo infos[] = map.getAllCommands(type);
            for (CommandInfo info: infos) {
                System.out.println("\t" + info.getCommandName());
            }
        }
    }
}

在缺省目录下运行,得到下面的结果:

image/jpeg
     view
text/*
     view
     edit
image/gif
     view

你还可以使用JavaBeans 框架实现将文件映射到Mime类型,下面的例子说明了mime类型与特定的文件类型是否有关联。

import javax.activation.*;
import java.io.*;
public class FileTypes {
    public static void main(String args[]) {
        FileTypeMap map = FileTypeMap.getDefaultFileTypeMap();
        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(file.getName() + ": " +
                    map.getContentType(file));
        }
    }
}

运行:> java FileTypes /tmp
ack.jpg: image/jpeg
addr.html: text/html
alabama.gif: image/gif
alarm.wav: audio/x-wav
alex.txt: text/plain
alt.tif: image/tiff
 
二、jdk16增强的桌面功能

使用的常用代码片段如下:
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());
                    }
                }
            }
        }
    }
}

三、java.lang包的变化

同以前版本一样,不需要显示的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


四、java.util包的变化,主要包括:

日历显示名称
Deque(双端队列)
Map和Set的可导航(遍历)特性
Resource Bundle的控制
Array的复制 
Lazy atomics(原语)

1. 日历显示名称
当定义好了DateFormat, 像01/02/03这样的日期,对于欧洲国,它表示为:February 1, 2003,对于美国,表示为January 2, 2003,可以消除这样的混淆。
过去,不被推荐调用DateFormatSymbols的getWeekDays()方法,现在可以使用Calendar的getDisplayNames()来代替了。
  Map names = aCalendar.getDisplayNames(Calendar.DAY_OF_WEEK,   
               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 names = now.getDisplayNames(
                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类

阅读(2018) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~