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

阅读Java代码的的一些收获

2008-04-10 17:32 411 查看
1. StringTokenizer 怎么用 ?

String str = "127.0.0.1";
StringTokenizer st = new StringTokenizer(str, ".", false);
//如果最后一个参数为true,则返回值中包括"."
while(st.hasMoreTokens()){
int j;
String token = st.nextToken();
j = Integer.parseInt(token);
System.out.println(j);
}
}

st就是StringTokenizer,nextToken返回下一个token

2.System.getProperties() 得到什么。。。

import java.util.Enumeration;
import java.util.Properties;

public class SystemInfo {

public static void main(String[] args) {
Properties systemProperties=System.getProperties();
Enumeration enums=systemProperties.propertyNames();
while(enums.hasMoreElements()){
String key=(String)enums.nextElement();
System.out.println(key+"="+systemProperties.getProperty(key));
}
}
}

System.getProperties()可以得到Java运行环境的一些基本属性

你要添加的话,可以实例化一个Properties对象,然后set

-- listing properties --
java.runtime.name=Java(TM) 2 Runtime Environment, Stand...
sun.boot.library.path=D:/j2sdk1.4.1_03/jre/bin
java.vm.version=1.4.1_03-b02
java.vm.vendor=Sun Microsystems Inc.
java.vendor.url=http://java.sun.com/
path.separator=;
java.vm.name=Java HotSpot(TM) Client VM
file.encoding.pkg=sun.io
user.country=CN
sun.os.patch.level=Service Pack 3
java.vm.specification.name=Java Virtual Machine Specification
java.runtime.version=1.4.1_03-b02
java.awt.graphicsenv=sun.awt.Win32GraphicsEnvironment
java.endorsed.dirs=D:/j2sdk1.4.1_03/jre/lib/endorsed
os.arch=x86
line.separator=

java.vm.specification.vendor=Sun Microsystems Inc.
user.variant=
java.naming.factory.url.pkgs=org.apache.naming
os.name=Windows 2000
sun.java2d.fontpath=
java.library.path=D:/j2sdk1.4.1_03/bin;.;D:/WINNT/Syste...
java.specification.name=Java Platform API Specification
java.class.version=48.0
java.util.prefs.PreferencesFactory=java.util.prefs.WindowsPreferencesFac...
os.version=5.0
user.home=D:/Documents and Settings/administrator
user.timezone=Asia/Shanghai
catalina.useNaming=true
java.awt.printerjob=sun.awt.windows.WPrinterJob
file.encoding=GBK
java.specification.version=1.4
catalina.home=E:/Program Files/JBuilder7/jakarta-to...
user.name=chenyan
java.class.path=D:/Program Files/Apache Tomcat 4.0/bi...
java.naming.factory.initial=org.apache.naming.java.javaURLContext...
axis.enableListQuery=true
java.vm.specification.version=1.0
sun.arch.data.model=32
java.home=D:/j2sdk1.4.1_03/jre
java.specification.vendor=Sun Microsystems Inc.
user.language=zh
awt.toolkit=sun.awt.windows.WToolkit
java.vm.info=mixed mode
java.version=1.4.1_03
java.ext.dirs=D:/j2sdk1.4.1_03/jre/lib/ext
sun.boot.class.path=D:/j2sdk1.4.1_03/jre/lib/rt.jar;D:/j2...
java.vendor=Sun Microsystems Inc.

//往system properties 里set新的对象
Properties p=System.getProperties();
p.setProperty("just test","Ok");//

另:用什么类和函数可以得到jvm的版本呢?
直接用
String s = System.getProperty("java.vm.version");
s 就是jvm的版本.字符串格式.
如果输出结果可能类似如下格式(和jvm版本有关).
1.5.0_05-b05

3.in.read()返回的什么?

From JDK API Document:
public int read()
throws IOExceptionReads a byte of data from this input stream. This method blocks if no input is yet available.
Overrides:
read in class InputStream
Returns:
the next byte of data, or -1 if the end of the file is reached.
Throws:
IOException - if an I/O error occurs.
-----------------------------------------------------------------
read return a byte code read from stream. That's why using write(c) can write a correct character to the outputstream.
You can use if(c == '/n') to check if the character is a return carrier.

使用:
int ch = in.read ();
while (true) {
switch (ch) {
case -1:
return;

case '#':
if (srcFile == null) {
do {
ch = in.read ();
}
while ((ch >= 0) && (ch != '/n') && (ch != '/r'));
}
else {
bufindx = 0;
for (ch = in.read (); (ch >= 0) && (ch != '/n') && (ch != '/r'); ch = in.read ()) {
buf = appendToArray (buf, bufindx++, ch);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: