您的位置:首页 > 编程语言 > Java开发

Something about Java

2014-11-17 21:23 274 查看
<span style="font-family: Helvetica, Arial, sans-serif; background-color: rgb(255, 255, 255);">实时更新</span>


很多常用知识,防止自己忘记,记录下来,同时与大家分享,如有错误,劳烦指出,谢谢。

1. java.lang.String.endsWith() 方法返回的测试该字符串是否以指定后缀sffix结束

2. java.io.File.list() 返回的文件和目录在此抽象路径名指定的目录中的数组。如果抽象路径名不表示一个目录,该方法返回null。

3. ClassLoader
catalinaLoader

<span style="font-size:14px;">ClassstartupClass=
catalinaLoader.loadClass
("org.apache.catalina.startup.Catalina");
Object startupInstance = startupClass.newInstance();
String methodName = "setParentClassLoader";
Class paramTypes[] = new Class[1];
paramTypes[0] = Class.forName("java.lang.ClassLoader");
Object paramValues[] = new Object[1];
paramValues[0] = sharedLoader;</span>


//先获取相应的method对象


//getMethod第一个参数是方法名,第二个参数是该方法的参数类型,


//因为存在同方法名不同参数这种情况,所以只有同时指定方法名和参数类型才能唯一确定一个方法

Method method =

startupInstance.getClass().getMethod(methodName, paramTypes);

//第一个参数是具体调用该方法的对象


//第二个参数是执行该方法的具体参数

method.invoke(startupInstance, paramValues);

实现将class加载入内存,取得方法并调用方法
4.
程序默认控制命令为开始,若有其他命令输入,替换
ex:

<span style="font-size:14px;">String command = "start";
if (args.length > 0) {
command = args[args.length - 1];
}</span>


5.
Class.getPrimitiveClass(String
name)

Return the Virtual Machine's Class object for the named primitive type.

6.
boolean java.io.File.isAbsolute()

Tests whether this abstract pathname is absolute. The definition of absolute pathname is system dependent. On UNIX systems, a pathname is absolute if its prefix is
"/"
. On Microsoft Windows systems, a pathname is
absolute if its prefix is a drive specifier followed by
"\\"
, or if its prefix is
"\\\\"
.

Returns:
true
if this abstract pathname is absolute,
false
otherwise
7.
String java.io.File.getCanonicalPath() throws IOException

取得标准路径
String java.io.File.getAbsolutePath()

取得绝对路径
System.getProperty("java.io.tmpdir")

取得默认临时文件路径
8.
JMX : Java Management Extensions
JNDI : Java Naming and Directory Interface

9.
ClassLoader java.lang.Class.getClassLoader()

Returns the class loader for the class. Some implementations may use null to represent the bootstrap class loader. This method will return null in such implementations if this class was loaded by the bootstrap class loader.

If a security manager is present, and the caller's class loader is not null and the caller's class loader is not the same as or an ancestor of the class loader for the class whose class loader is requested, then this method calls
the security manager's
checkPermission
method with a
RuntimePermission("getClassLoader")
permission to ensure it's ok to access the class loader for the class.

If this object represents a primitive type or void, null is returned.

Returns:the class loader that loaded the class or interface represented by this object.Throws:SecurityException - if a security manager exists and its
checkPermission
method denies access to the class loader for the class.
10.
Apache Commons

the Digester component was designed to provide a common implementation that can be used in many different projects.

Basically, the Digester package lets you configure an XML -> Java object mapping module, which triggers certain actions called rules whenever a particular pattern of nested XML elements is recognized. A rich set
of predefined rules is available for your use, or you can also create your own.
url:http://commons.apache.org/proper/commons-digester/

Digester是apache commons下的一个子项目,包含下面三个包:

(1)org.apache.commons.digester:该包可用于处理xml文件;

(2)org.apache.commons.digester.rss:可用于解析rss;

(3)org.apache.commons.digester.xmlrules:该包提供了基于xml的定义。

11.File.separator 系统分隔符:windows是\,unix是/
12.
autoflush is true, println() will flush, but print() will not.
13. facade classes外观模式
14. singleton class
The only constructor it has is private so that you cannot use the new keyword to instantiate it from
outside the class.
ex:

<span style="font-size:14px;">private static Hashtable managers = new Hashtable(); public synchronized static StringManager getManager(String packageName) { StringManager mgr = (StringManager)managers.get(packageName);
if (mgr == null) { mgr = new StringManager(packageName); managers.put(packageName, mgr); } return mgr; }</span>


15. jsessionid is used to carry a session identifier in Tomcat
16. instanceof
java 中的instanceof 运算符是用来在运行时指出对象是否是特定类的一个实例。instanceof通过返回一个布尔值来指出,这个对象是否是这个特定类或者是它的子类的一个实例。
17. notifyAll() 多线程唤醒所有等待线程,具体可看博文中Java分类的多线程讲解。
18. Using character arraysto compare
to avoid expensive string operations
ex:
static final char[] AUTHORIZATION_NAME = "authorization".toCharArray();


19. request.getRemoteAddr()获取的值为0:0:0:0:0:0:0:1
解决办法:修改C:\Windows\system32\drivers\etc\hosts文件
20. java.lang.System.arraycopy(Object src, int srcPos, Object dest, int destPos, int length)
Copies an array from the specified source array, beginning at the specified position, to the specified position of the
destination array.
Parameters:

src the source array.

srcPos starting position in the source array.

dest the destination array.

destPos starting position in the destination data.

length the number of array elements to be copied.
21. java.io.File.mkdirs()
Creates the directory named by this abstract pathname, including any necessary but nonexistent parent directories. Note
that if this operation fails it may have succeeded in creating some of the necessary parent directories.

Returns:

true if and only if the directory was created, along with all necessary parent directories; false otherwise
22. writer = new PrintWriter(new FileWriter(pathname, true), true);
java.io.FileWriter.FileWriter(String fileName, boolean append)
Constructs a FileWriter object given a file name with a boolean indicating whether or not to append the data written.

Parameters:

fileName String The system-dependent filename.

append boolean if true, then data will be written to the end of the file rather than the beginning.

java.io.PrintWriter.PrintWriter(Writer out, boolean autoFlush)

Creates a new PrintWriter.

Parameters:

out A character-output stream

autoFlush A boolean; if true, the println, printf, or format methods will flush the output buffer

23. 使用java.beans.PropertyChangeSupport监控变量
24. Class<?> java.lang.Class.forName(String className) throws ClassNotFoundException
Returns the Class object associated with the class or interface with the given string name. Invoking this method is equivalent
to:

Class.forName(className, true, currentLoader)

where currentLoader denotes the defining class loader of the current class.

25. java.lang.Thread.Thread(Runnable target, String name)
Allocates a new Thread object. This constructor has the same effect as Thread (null, target, name).

Parameters:

target: the object whose run method is invoked when this thread is started. If null, this thread's run method is invoked.

name: the name of the new thread
26. Note that thetransient keyword makes a variable non-serializable.
27. Web项目获取prperties文件:

Properties prop = new Properties();
InputStream in = this.getClass().getResourceAsStream("/name.properties");
if(in != null){
prop.load(in);
if(!prop.isEmpty()){
configEntity.setBrokerHost(prop.getProperty("name"));
}
} else {
logger.info("未找到资源文件");
}


28. All JDK 7 versions ranging from 7u21 to 7u55 are buggy with regards to invokedynamic. If you plan to use invokedynamic support, make sure you either use 7u60 or JDK 8.

29.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: