---------------------- ASP.Net+Android+IO开发S、.Net培训、期待与您交流! ----------------------
//18-1
import java.util.*;
public class SystemDemo {
public static void main(String[] args){
//Properties 是HashTable的子类,也就是Map的子类
Properties prop = System.getProperties();
//获取指定系统信息
String value = System.getProperty("os.name");
System.out.println("val="+value);
//在系统中设定一些指定系统信息
System.setProperty("mykey", "myvalue");
//获取所以属性信息
/*for(Object obj: prop.keySet()){
String value = (String)prop.get(obj);
System.out.println(obj+"::"+value);
}*/
//java 命令查看java 参数列表
//动态加载一些属性信息,在cmd中用-Dha=hahaha
//详细命令 java -Dha=hahaha SystemDemo
String v = System.getProperty("ha");
System.out.println("v="+v);
}
}
/*
总结:
System.getProperties();
System.getProperty("key");
System.setProperty("key","value");
prop.keySet(); // Property 类的非静态方法。
prop.get(obj);//get属于Property类,而getProperty属于System.
*/
/*18-2
Runtime:使应用程序与其运行环境相连接
没有构造函数,不可new,可想到该类的方法都是静态的。
然而该类中还有非静态方法,则该类提供了方法用于获取本类对象
该方法为getRuntime();
这个特点可以看出该类使用了单例设计模式
*/
class RuntimeDemo
{
public static void main(String[] args) throws Exception
{
Runtime r = Runtime.getRuntime();
Process p = r.exec("Notepad.exe SystemDemo.java");
Thread.sleep(4000);
p.destroy();
System.out.println("Hello World!");
}
}
/*
总结
Runtime.getRuntime(); //Runtime 类的静态方法
r.exec("Notepad.exe SystemDemo.java"); //Runtime 类的非静态方法
Thread.sleep(4000); //Thread 类的静态方法
p.destroy(); // Process 类的destroy
*/
//18-3
import java.util.*;
import java.text.*;
class DateDemo
{
public static void main(String[] args)
{
Date d = new Date();//打印的时间格式不合适
System.out.println(d);
SimpleDateFormat sdt = new SimpleDateFormat("yyyy年MM月dd日 E hh:mm:ss");
String time = sdt.format(d);
System.out.println(time);
long l = System.currentTimeMillis();
Date date = new Date(l);
System.out.println(date);
}
}
/*
总结
new Date(); //Date类的构造函数
new SimpleDateFormat("yyyy年MM月dd日 E hh:mm:ss"); //SimpleDateFormat 类构造函数
sdt.format(d); //SDF类的format方法
//提问:format有3个参数,为何只用一个参数也能正常的运行?
//因为用的是父类的方法。
*/
//18-4
import java.util.*;
import java.text.*;
class CalendarDemo
{
public static void main(String[] args)
{
String[]month = {"一","二","三","四","五","六","七","八","九","十","十一","十二"};
String[]week = {"","日","一","二","三","四","五","六"};
Calendar c = Calendar.getInstance();
sop(c.get(Calendar.YEAR)+"年"+c.get(Calendar.MONTH)+"月");
sop(c.get(Calendar.YEAR)+"年");
sop(month[c.get(Calendar.MONTH)]+"月");
sop(c.get(Calendar.DAY_OF_MONTH)+"日");
sop("星期"+week[c.get(Calendar.DAY_OF_WEEK)]);
}
public static void sop (Object obj){
System.out.println(obj);
}
}
/*
总结
Calendar.getInstance();
c.get(Calendar.YEAR);
String[]month = {""};//String 数字, 运用查表法。
*/
class CalendarDemo2
{
public static void main(String[] args)
{
Calendar c = Calendar.getInstance();
c.set(2015,5,5);
c.add(Calendar.YEAR, 5);//向前推就是加 负的值
show(c);
System.out.println("Hello World!");
}
public static void show(Calendar c)
{
String[]month = {"一","二","三","四","五","六","七","八","九","十","十一","十二"};
String[]week = {"","日","一","二","三","四","五","六"};
//Calendar c = Calendar.getInstance();
sop(c.get(Calendar.YEAR)+"年"+c.get(Calendar.MONTH)+"月");
sop(c.get(Calendar.YEAR)+"年");
sop(month[c.get(Calendar.MONTH)]+"月");
sop(c.get(Calendar.DAY_OF_MONTH)+"日");
sop("星期"+week[c.get(Calendar.DAY_OF_WEEK)]);
}
public static void sop (Object obj){
System.out.println(obj);
}
}
/*
总结
c.set(2015,5,5);
c.add(Calendar.DAY_OF_MONTH, -1);
*/
//18-5
class MathDemo
{
public static void main(String[] args)
{
System.out.println("Hello World!");
}
}
/*
总结
ceil();
floor();
random();
pow(2,3);//指数运算
new Random().nextint(10);
DecimalFormat df = new DecimalFormat(".00");
df.format();
*/
//18-7
import java.io.*;
class FileWriteDemo
{
public static void main(String[] args) throws IOException
{
//创建或覆盖已有文件于指定目录,该对象一被初始化就必须明确被操作的文件对象。
FileWriter fw = new FileWriter("demo.txt");
//把字符写入到流中
fw.write("adfaffa");
//刷新缓冲区
fw.flush();
//刷新并关闭缓冲区
fw.close();
System.out.println("Hello World!");
}
}
/*
sum:
new FileWriter("demo.txt");
fw.write("afadf");
fw.flush();
fw.close();
*/
//18-8
import java.io.*;
class FileWriterDemo2
{
public static void main(String[] args)
{
System.out.println("afaf");
FileWriter fw = null;
try{
fw = new FileWriter("k:\\demo.txt");
fw.write("abdaf");
}
catch(Exception e){
System.out.println(e.toString());
}
finally{
try{
if(fw!=null) //必须判断fw不是空指针。
fw.close();
}
catch(Exception e){
System.out.println(e.toString());
}
}
}
}
/*
sum:
必须判断fw不为空指针。
*/
//18-9
import java.io.*;
class FileWriterDemo3
{
public static void main(String[] args) throws IOException
{
FileWriter fw = new FileWriter("demo.txt", true);
//参数:
//file - 要写入数据的 File 对象
//append - 如果为 true,则将字节写入文件末尾处,而不是写入文件开始处
fw.write("\r\nwindows换行符,\nlinux换行符");
fw.close();
}
}
/*
sum:
new FileWriter("demo.txt",true);//续写文件
\r\n,与\n的区别。
*/
//18-10
import java.io.*;
class FileReaderDemo
{
public static void main(String[] args) throws IOException
{
//创建目标文件的流对象
FileReader fr = new FileReader("demo.txt");
//read一次读一个字符,且自动向下移动。当文件读完是返回-1。
//read();返回:
//作为整数读取的字符,范围在 0 到 65535 之间 (0x00-0xffff),
//如果已到达流的末尾,则返回 -1
int ch = 0;
while((ch = fr.read())!=-1){
System.out.println((char)ch);
}
/*while(true){
int ch = fr.read();
if(ch==-1)
break;
System.out.println(ch);
}*/
fr.close();
}
}
/*
sum:
fr.read();
*/
//18-11
import java.io.*;
class FileReaderDemo2
{
public static void main(String[] args) throws IOException
{
FileReader fr = new FileReader("demo.txt");
//read(char[]) 返回读到字符个数,当数据读完是返回-1。
char[] buff = new char[1024];
int num = 0;
while((num=fr.read(buff))!=-1)
System.out.print(new String(buff,0,num)); //用print而不用println
fr.close();
}
}
/*
sum:
read(char[]);
new String(buff,0,num);//分配一个新的 String,它包含取自字符数组参数一个子数组的字符。
*/
//18-13
import java.io.*;
class CopyDemo
{
public static void main(String[] args)
{
System.out.println("Hello World!");
}
public static void copy(){
FileReader fr = null;
FileWriter fw = null;
try{
fr = new FileReader("FileReaderDemo.java");
fw = new FileWriter("FileReaderDemo_copy.java");
char [] buff = new char[1024];
int len =0;
while((len = fr.read(buff))!=-1)
fw.write(new String(buff,0,len));
}
catch(IOException e){
throw new RuntimeException("读写失败");
}
finally{
try{
if(fr!=null)
fr.close();
}catch(IOException e){
System.out.println(e.toString());
}
try{
if (fw!=null)
{
fw.close();
}
}catch(IOException e){
System.out.println(e.toString());
}
}
}
}
/*
sum:
多个流要分开try。
*/
---------------------- ASP.Net+Android+IO开发S、.Net培训、期待与您交流! ----------------------