阅读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);
}
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);
}
相关文章推荐
- 【java】【实践】阅读代码,一些较好的实践
- 一段适合新手的Java代码,阅读一定有收获。
- Mysql Java驱动代码阅读笔记及JDBC规范笔记
- Java 基础一些代码练习笔记( Interator)
- 一些防止java代码被反编译的方法
- Java 基础一些代码练习笔记( TreeSet 排序)
- Java 基础一些代码练习笔记(策略模式)
- 一些比较好的java代码规范
- Java优化集合代码的一些方法
- Java 基础一些代码练习笔记(多态1)
- 一些你需要知道的Java编程代码规范
- 编写Java代码一些注意点
- 我刚翻译的一些用Swig将c/c++程序转为java代码的英文文档
- 阅读项目代码,一些总结
- Java 基础一些代码练习笔记(Array数组)
- java代码的一些规范
- java 语法没有错,但是无法加载进去,一开始没有错的时候就要查看自己的一些String或者有引号“”的代码
- 一些java小代码
- java 编写代码的一些规则
- java与c/c++进行socket通信的一些问题(1)-JSP教程,Java技巧及代码