读书人

请问一个细节有关问题实在是不知其所

发布时间: 2013-12-21 20:16:01 作者: rapoo

请教一个细节问题,实在是不知其所以然,读过Java核心卷一的朋友看过来!!!!

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.logging.*;
import javax.swing.*;

/**
* A modification of the image viewer program that logs various events.
* @version 1.02 2007-05-31
* @author Cay Horstmann
*/
public class LoggingImageViewer
{
public static void main(String[] args)
{
if (System.getProperty("java.util.logging.config.class") == null
&& System.getProperty("java.util.logging.config.file") == null)
{
try
{
Logger.getLogger("com.horstmann.corejava").setLevel(Level.ALL);
final int LOG_ROTATION_COUNT = 10;
Handler handler = new FileHandler("%h/LoggingImageViewer.log", 0, LOG_ROTATION_COUNT);
Logger.getLogger("com.horstmann.corejava").addHandler(handler);
}
catch (IOException e)
{
Logger.getLogger("com.horstmann.corejava").log(Level.SEVERE,
"Can't create log file handler", e);
}
}

EventQueue.invokeLater(new Runnable()
{
public void run()
{
Handler windowHandler = new WindowHandler();
windowHandler.setLevel(Level.ALL);
Logger.getLogger("com.horstmann.corejava").addHandler(windowHandler);

JFrame frame = new ImageViewerFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Logger.getLogger("com.horstmann.corejava").fine("Showing frame");
frame.setVisible(true);
}
});
}
}



这段代码里前边有一个if判断
 if (System.getProperty("java.util.logging.config.class") == null
&& System.getProperty("java.util.logging.config.file") == null)



查看API
getProperty
public static String getProperty(String key)
Gets the system property indicated by the specified key.
First, if there is a security manager, its checkPropertyAccess method is called with the key as its argument. This may result in a SecurityException.

If there is no current set of system properties, a set of system properties is first created and initialized in the same manner as for the getProperties method.

Parameters:
key - the name of the system property.
Returns:
the string value of the system property, or null if there is no property with that key.
Throws:
SecurityException - if a security manager exists and its checkPropertyAccess method doesn't allow access to the specified system property.
NullPointerException - if key is null.
IllegalArgumentException - if key is empty.
See Also:
setProperty(java.lang.String, java.lang.String), SecurityException, SecurityManager.checkPropertyAccess(java.lang.String), getProperties()

我的理解是如果在系统属性里没有包含key这个属性,则创建一个并且返回 null

查看 getProperties()方法确实没有这两个属性
(System.getProperty("java.util.logging.config.class") == null


&& System.getProperty("java.util.logging.config.file") == null)

那么系统就会创建这两个属性

第一段代码是在java核心卷上的一段代码
http://bbs.csdn.net/topics/390475826

我的问题是为啥要加这个if判断,而且这两个属性可以随便吗?为啥要判断一下这两个属性?


谢谢!!!
[解决办法]
System 类还有

public static String setProperty(String key, String value)
方法,它取得应该不是系统自带的属性,而是自己新添加进去的。
参考 setProperty(String key, String value)
[解决办法]
看这个

读书人网 >J2SE开发

热点推荐