-------
一、System (java.lang.System)
System 类中的方法和属性都是静态的。
out:标准输出,默认是控制台。
in:标准输入,默认是键盘。
需求: 获取系统属性信息(Properties getProperties())。
import java.util.*;
class SystemDemo {
public static void main(String[] args) {
Properties prop = System.getProperties();
//因为Properties是Hashtable的子类,也就是Map集合的一个子类对象,即系统属性是成对出现的。
//那么可以通过map的方法取出该集合中的元素。该集合中存储都是字符串,没有泛型定义。
//如何在系统中自定义一些特有信息呢?
System.setProperty("mykey","myvalue");
//获取指定属性信息
String mykey = System.getProperty("mykey");
System.out.println("mykey="+mykey);
//可不可以在jvm启动时,动态加载一些属性信息呢?
String value = System.getProperty("value");
System.out.println("value="+value);
//获取所有属性
for (Object obj : prop.keySet()){
String v = (String)prop.get(obj);
System.out.println(obj+"="+v);
}
}
动态设置一些属性信息,使得JVM启动时就能加载。在dos命令行下设置,设置方式:java -D<name>=<value>
如:运行前设置:java -Dname=localhost SystemDemo ;
编译时设置:javac -Dname=localhost SystemDemo.java;
二、Runtime (java.lang.Runtime)
该类并没有提供构造函数,说明不可以 new 对象,那么会直接想到该类中的方法都是静态的。发现该类中还有非静态方法。说明该类肯定会提供方法获取本类对象,而且该方法是静态的,并返回值类型是本类类型。
由这个特点可以看出该类使用了单例设计模式。
获取本类对象方式是 static Runtime getRuntime( )
Runtime run = Runtime.getRuntime();
// D:\Program\QQ\Bin\QQ.exe 这种写法错误,\ 是转义字符。\\ 表示 \
// run.exec("D:\\Program\\QQ\\Bin\\QQ.exe");
Process pro = run.exec("notepad.exe SystemDemo.java");
Thread.sleep(1000);
pro.destroy(); //终止进程pro。
三、Date (java.util.Date) 和 DateFormat(java.text.DateFormat)
Date日期时间类:表示特定的瞬间,精确到毫秒,不过很多方法都被日历类替代了 Calender 。在类 Date 所有可以接受或返回年、月、日期、小时、分钟和秒值的方法中,将使用下面的表示形式:
- 年份 y 由整数 y - 1900 表示。
- 月份由从 0 至 11 的整数表示;0 是一月、1 是二月等等;因此 11 是十二月。
- 日期(一月中的某天)按通常方式由整数 1 至 31 表示。
- 小时由从 0 至 23 的整数表示。因此,从午夜到 1 a.m. 的时间是 0 点,从中午到 1 p.m. 的时间是 12 点。
- 分钟按通常方式由 0 至 59 的整数表示。
- 秒由 0 至 61 的整数表示;值 60 和 61 只对闰秒发生,尽管那样,也只用在实际正确跟踪闰秒的 Java 实现中。于按当前引入闰秒的方式,两个闰秒在同一分钟内发生是极不可能的,但此规范遵循 ISO C 的日期和时间约定。
在所有情形中,针对这些目的赋予方法的参数不需要在指定的范围内。例如,可以把日期指定为 1 月 32 日,并把它解释为 2 月 1 日的相同含义。
DateFormat 是日期/时间格式化子类的抽象类,它以与语言无关的方式格式化并解析日期或时间。日期/时间格式化子类(如 SimpleDateFormat)允许进行格式化(也就是日期 -> 文本)、解析(文本-> 日期)和标准化。将日期表示为 Date 对象,或者表示为从 GMT(格林尼治标准时间)1970 年 1 月 1 日 00:00:00 这一刻开始的毫秒数。关于日期和时间模式请参照类 SimpleDateFormat 。
示例:
Date d = new Date();
System.out.println(d);//打印的时间看不懂,希望有些格式。
//将模式封装到SimpleDateFormat对象中。
DateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 E hh:mm:ss");
//用sdf的格式去格式化指定Date对象;
String time = sdf.format(d);
System.out.println(time);
long l = System.currentTimeMillis();
Date d1 = new Date(l);
System.out.println("d1="+d1);
四、Calender(java.util.Calendar)
Calendar 类是一个抽象类,它为特定瞬间与一组诸如 YEAR、MONTH、DAY_OF_MONTH、HOUR 等日历字段之间的转换提供了一些方法,并为操作日历字段(例如获得下星期的日期)提供了一些方法。瞬间可用毫秒值来表示,它是距历元(即格林威治标准时间 1970 年 1 月 1 日的 00:00:00.000,格里高利历)的偏移量。
其具体日历规范请参照Calendar 的 API 文档。
需求1,获取任意年的二月有多少天。
思路:根据指定年设置一个时间就是
c.set(year,2,1) // 某一年的3月1日。
c.add(Calenar.DAY_OF_MONTH,-1); // 3月1日,往前推一天,就是2月最后一天。
需求2,获取昨天的现在这个时刻。
c.add(Calenar.DAY_OF_MONTH,-1);
@Test
public static void main(String[] args) {
Calendar c = Calendar.getInstance();
// c.set(2012,2,23);
c.add(Calendar.DAY_OF_MONTH,-18);
printCalendar(c);
}
public static void printCalendar(Calendar c){
String[] mons = {"一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"};
String[] weeks = {"","星期日","星期一","星期二","星期三","星期四","星期五","星期六",};
int index = c.get(Calendar.MONTH);
int index1 = c.get(Calendar.DAY_OF_WEEK);
System.out.println(c.get(Calendar.YEAR)+"年");
//System.out.println((c.get(Calendar.MONTH)+1)+"月");
System.out.println(mons[index]);
System.out.println(c.get(Calendar.DAY_OF_MONTH)+"日");
//System.out.println("星期"+c.get(Calendar.DAY_OF_WEEK));
System.out.println(weeks[index1]);
}
五、Math(java.lang.Math) 和 Random(java.util.Random)
Math 类包含用于执行基本数学运算的方法,如初等指数、对数、平方根和三角函数。
Random 类的实例用于生成伪随机数流。此类使用 48 位的种子,使用线性同余公式 (linear congruential form) 对其进行了修改。
其常用方法介绍:
@Test
public static void show()
{
double d = Math.ceil(-16.34); //ceil返回大于指定数据的最小整数。
double d1 = Math.floor(16.34); //floor返回小于指定数据的最大整数。
double d2 = Math.pow(2,3); //指数运算。
long l = Math.round(-12.54); //四舍五入。
System.out.println("d="+d);
System.out.println("d1="+d1);
System.out.println("d2="+d2);
System.out.println("l="+l);
}
需求1:给定一个小数,保留该小数的后两位。选作:可以考虑,保留时进行四舍五入。
//scale:保留小数位数;is_round:需要四舍五入么?
@Test
public static void saveTwo(double d, int scale, boolean is_round)
{
double base = Math.pow(10,scale);
double num = is_round ? Math.round(d*base)/base : ((int)(d*base))/base;
System.out.println("num="+num);
/*
double d1 = d * base;
System.out.println("d1="+d1);
double d2 = (int)(d1 + 0.5);
System.out.println("d2="+d2);
double d3 = d2 / base;
System.out.println("d3="+d3);
*/
}
需求2:编写一个掷骰子游戏,连两个骰子点数和为7 表示投掷陈功。
import java.util.*;
//掷骰子游戏
class DieGame{
private Die die1,die2;
@Test
public void play(){
die1 = new Die();
die2 = new Die();
die1.roll();
die2.roll();
int x = die1.getValue();
int y = die2.getValue();
if((x+y)==7)
System.out.println("中了!");
else
System.out.println("Fuck...");
}
}
class Die{
private int facevalue;
public void roll(){
//random:返回一个 0.0<= r <1.0 的double数,是一个随机值
Random r = new Random();
//返回的是一个整数,且为1——6间的随机值,不加1返回的是:0——5间的随机值。
facevalue = r.nextInt(6)+1;
}
public int getValue(){
System.out.println(facevalue);
return facevalue;
}
}
注: 很多应用程序会发现 Math.random() 方法更易于使用。
六、Properties (java.util.Properties)
Properties 是 HashTable 的子类,也就是说它具备 Map 集合的特点,而且它里面存储的键值对都是字符串。是集合中和 IO 技术相结合的集合容器。
特点:可以用于键值对形式的配置文件(是一些软件基本配置信息存放的地方)。那么在加载数据时,需要数据有固定格式:键=值。
需求1:对 Properties 集合的基本设置和获取操作
public static void setAndGet(){
Properties prop = new Properties();
prop.setProperty("zhangsan","30");
prop.setProperty("lisi","33");
String value = prop.getProperty("lisi");
System.out.println("value: "+value);
prop.setProperty("lisi","50");
Set<String> names = prop.stringPropertyNames();
for(String s : names){
System.out.println("name: "+s+", age: "+prop.getProperty(s));
}
}
需求2:将info.txt中键值数据存到集合中进行操作。
方式一:
1、用一个流和info.txt文件关联。
2、读取一行数据,将该行数据用"="进行切割。
3、等号左边作为键,右边作为值,存入到Properties集合中即可。
public static void loadDemo_1()throws IOException{
BufferedReader bufr = new BufferedReader(new FileReader("info.txt"));
Properties prop = new Properties();
String line = null;
while ((line=bufr.readLine())!=null){
String[] arr = line.split("=");
prop.setProperty(arr[0],arr[1]);
}
bufr.close();
System.out.println(prop);
}
方式二:直接使用 Properties 的 load 加载配置文件 info.txt。并对集合中的数据进行修改在通过 PrintWrite 输出流将数据写入到配置文件中。
public static void loadDemo_2()throws IOException{
Properties prop = new Properties();
FileInputStream fis = new FileInputStream("info.txt");
//将流中数据加载进集合
prop.load(fis);
//修改键所对用的值
prop.setProperty("zhaoqi","20");
PrintWriter pw = new PrintWriter("info.txt");
//将修改后的信息存储进文件中,且写入注释:修改成功
prop.store(pw,"修改成功");
//列出Properties中的键值对
prop.list(System.out);
fis.close();
pw.close();
}
配置文件 info.txt 修改后信息:
#修改成功
#Fri Mar 15 02:59:47 CST 2013
zhangsan=23
lisi=55
zhaoqi=20
需求3:限制程序运行次数。当运行次数到达5次时,给出请您注册的提示,并不再让该程序执行。
分析:很容易想到的是:计数器。可是该计数器定义在程序中,随着程序的运行而在内存中存在,并进行自增。但随着该应用程序的退出,该计数器也在内存中消失了。而下一次在启动该程序,又重新开始从0计数。 这不是我们想要的。我们需要的是程序即使结束,计数器的值也存在,下次程序启动会先加载计数器的值并加1后再重新存储起来。所以要建立配置文件,用于记录该软件的使用次数。配置文件使用键值对的形式,这样便于阅读数据,并操作数据。
Map 集合存放的是键值对数据,而数据是以文本的形式存储,要是使用 IO 技术。所以练习 Map 和 IO 的特殊对象数 Properties 集合。
配置文件可以实现应用程序数据的共享。在java开发中,配置文件有两种标记:.xml 或 .properties
@Test
public static void count()throws IOException{
File file = new File("count.properties");
//若文件不存在就在当前目录下创建一个。
if (!file.exists())
file.createNewFile();
FileReader ins = new FileReader(file);
Properties prop = new Properties();
//将文件加载到集合prop中
prop.load(ins);
int count = 0;
//获取键time所对应的值
String value = prop.getProperty("time");
if (value!=null){
count = Integer.parseInt(value);
if (count>=5)
System.out.println("您好,使用次数已到,拿钱!");
}
count++;
prop.setProperty("time",count+"");
PrintWriter out = new PrintWriter(file);
//将更改后的次数重新存储到文件中,并附上注释
prop.store(out,"The count had used");
ins.close();
out.close();
}