1,System类:方法和属性都是静态的。out:标准输出,默认是控制台。in :标准输入,默认是键盘。2,获取系统属性信息:Properties getProperties()3,在系统中自定义一些属性信息:setProperty(key,value)4,获取指定属性信息:getProperty(key)5,在JVM启动时动态加载一些属性信息:import java.util.*;import java.lang.*;class SystemDemo{public static void main(String[] args){//动态加入属性信息:String v = System.getProperty("haha");System.out.println("v: "+v);//启动JVM的时候格式:java -Dhaha=qqqqq SystemDemo//在系统中自定义一些属性信息:System.setProperty("aaa","bbb");System.out.println(System.getProperty("aaa"));//获取系统属性信息:// Properties pro = System.getProperties();// for(Object obj: pro.keySet())// {// String pp = (String)obj;// String value = (String)pro.get(pp);// System.out.println(pp+": "+value);// }}}
2,Runtime:
没有构造函数。
说明不能直接创建该类的对象,可以想到该类的方法都是静态的,如果不都是静态的,可以想到该类应该提供了方法获取本类对象,而且该方法是静态的,并且返回值类型是本类类型。
getRuntime()
class RuntimeDemo
{
public static void main(String[] args)throws Exception
{
Runtime r = Runtime.getRuntime();
Process p = r.exec("notepad.exe RuntimeDemo.java");
Thread.sleep(4000);
p.destroy();
}
}
//java.text:SimpleDateFormat类
import java.util.*;
import java.text.*;
class DateDemo
{
public static void main(String[] args)
{
Date d = new Date();
System.out.println(d);//Sun Jun 16 19:55:03 CST 2013
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日E hh:mm:ss");
String time = sdf.format(d);
System.out.println(time);//2013年06月16日星期日 08:30:15
}
}
import java.util.*;
class CalendarDemo
{
public static void main(String[] args)
{
Calendar c = Calendar.getInstance();
// sop(c.get(Calendar.YEAR)+"年"+(c.get(Calendar.MONTH)+1)+"年"+c.get(Calendar.DAY_OF_MONTH)+"月"+"星期"+c.get(Calendar.DAY_OF_WEEK));
//查表法:
String[] month = {"一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"};//月份系统中是从0-11
int index = c.get(Calendar.MONTH);
sop(month[index]);
String[] week = {"","星期日","星期一","星期二","星期三","星期四","星期五","星期六",};//星期系统中是从1-7,加入一个空值,直接返回从角标1-7的值。
int index1= c.get(Calendar.DAY_OF_WEEK);
sop(week[index1]);
}
public static void sop(Object obj)
{
System.out.println(obj);
}
}
返回给定日历字段的值。
abstract void add(int field, int amount)
根据日历的规则,为给定的日历字段添加或减去指定的时间量。
void set(int field, int value)
将给定的日历字段设置为给定值。
void set(int year, int month, int date)
设置日历字段 YEAR、MONTH 和 DAY_OF_MONTH 的值。
void set(int year, int month, int date, int hourOfDay, int minute)
设置日历字段 YEAR、MONTH、DAY_OF_MONTH、HOUR_OF_DAY 和 MINUTE 的值。
void set(int year, int month, int date, int hourOfDay, int minute, int second)
设置字段 YEAR、MONTH、DAY_OF_MONTH、HOUR、MINUTE 和 SECOND 的值。
import java.util.*;
class CalendarDemo2
{
public static void main(String[] args)
{
Calendar c = Calendar.getInstance();
c.set(2013,7,23);
c.add(Calendar.DAY_OF_MONTH,-2);
printDate(c);
}
public static void sop(Object obj)
{
System.out.println(obj);
}
public static void printDate(Calendar c)
{
sop(c.get(Calendar.YEAR)+"年");
String[] month = {"一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"};//月份系统中是从0-11
int index = c.get(Calendar.MONTH);
sop(month[index]);
sop(c.get(Calendar.DAY_OF_MONTH)+"日");
String[] week = {"","星期日","星期一","星期二","星期三","星期四","星期五","星期六",};//星期系统中是从1-7,加入一个空值,直接返回从角标1-7的值。
int index1= c.get(Calendar.DAY_OF_WEEK);
sop(week[index1]);
}
}
ceil :返回大于指定数据的最小整数。
floor:返回小于指定数据的最大整数。
round:四舍五入
static double random():返回带正号的 double 值,该值大于等于 0.0 且小于 1.0。
java.util:Rondom类:
int nextInt(int n) :返回一个伪随机数,它是取自此随机数生成器序列的、在 0(包括)和指定值(不包括)之间均匀分布的 int 值。
class MathDemo
{
public static void main(String[] args)
{
double d1 = Math.ceil(12.45);
sop(d1);
double d2 = Math.floor(12.90);
sop(d2);
double d3 = Math.round(24.09);
sop(d3);
Random r = new Random();
for (int x=0;x<10;x++)//返回1-10的随机数
{
int dd = r.nextInt(10)+1;
// int dd = (int)(Math.random()*10+1);
sop(dd);
}
}
public static void sop(Object obj)
{
System.out.println(obj);
}
}
用途:IO流用于处理设备之间数据的传输。java对数据的操作是通过流的方式。java用于操作流的对象都在IO包中。流按操作数据分为两种:字节流与字符流。流按照流向分为:输入流,输出流。
常用基类:
字节流的抽象基类:InputStream OutputStream
字符流的抽象基类:Reader Writer
由这四个类派生出来的子类大都是由父类名称作为其子类名称的后缀。
//需求:在硬盘上创建一个文件并写入一些数据。
import java.io.*;
class ioDemo
{
public static void main(String[] args)throws IOException
{
//创建一个FileWriter对象。该对象一被初始化就必须要明确被操作的文件。
//而且该文件会被创建到指定目录下。如果该目录下已有同名文件,将被覆盖。
//其实该步就是在明确数据要存放的目的地。
FileWriter file = new FileWriter("demo.txt");
//调用write方法,将字符串写入到流中。
file.write("这是一个IO练习");
//刷新流对象缓冲中的数据,将数据刷到目的地中。
file.flush();
//关闭流资源,但是在关闭之前会先刷新一次缓冲中的数据。将数刷到目的地,关闭后流不能继续使用。
file.close();
file.write("haha");//java.io.IOException: Stream closed
file.flush();
}
}
class ioDemo2
{
public static void main(String[] args)throws IOException
{
FileWriter file = null;
//一起try的原因:这两句诗关联的,如果分开,第一句出问题了处理了后,第二句就没必要在执行了。
try
{
file = new FileWriter("k:\\Demo2.txt");//(系统找不到指定的路径。)
file.write("IO异常处理222");
}
catch (IOException e)
{
System.out.println(e.toString());
}
finally
{
try
{
if(file!=null)//防止异常:lang.NullPointerException,如果有多个流要当度进行关闭资源,当度进行判断流对象是否为null
file.close();
}
catch (IOException e)
{
System.out.println(e.toString());
}
}
}
}
import java.io.*;
class IODemo3
{
public static void main(String[] args)
{
FileWriter file = null;
try
{
file = new FileWriter("Demo2.txt",true);
file.write("hiehie");
file.write("\r\n1244");
}
catch (IOException e)
{
System.out.println(e.toString());
}
finally
{
try
{
file.close();
}
catch (IOException e)
{
System.out.println(e.toString());
}
}
}
}
class FileReaderDemo1
{
public static void main(String[] args)throws IOException
{
//创建一个文件读取流对象,和指定名称的文件相关联。
//要保证该文件是已经存在的,如果不存在,会发生异常FileNotFoundException
FileReader file = new FileReader("Demo2.txt");
// int ch=0;
// while((ch=file.read())!=-1)
// {
// System.out.print((char)ch);
// }
while(true)
{
int ch = file.read();
if(ch==-1)
break;
System.out.print((char)ch);
}
file.close();//Reader的close方法只关闭流对象资源,不刷新。
}
}
import java.io.*;
class FileReaderDemo2
{
public static void main(String[] args)throws IOException
{
FileReader file = new FileReader("FileReaderDemo1.java");
//定义一个数组用于储存读到的字符。
//该read(char[])返回的是读到字符
char[] buf = new char[1024];
int num = 0;
while((num=file.read(buf))!=-1)
{
System.out.println(new String(buf,0,num));
}
file.close();
}
}
// 在D盘创建一个文件,用于储存C盘文件中的数据。
// 定义读取流和C盘文件相关联。
// 通过不断的读写完成数据储存。
// 关闭资源。
import java.io.*;
class Copytxt
{
public static void main(String[] args)throws IOException
{
Copy_2();
}
public static void Copy_2()
{
try
{
FileReader fr = null;
FileWriter fw = null;
fw = new FileWriter("Demo0002.txt");
FileReader fr = new FileReader("c:\\Calendarhaha.java");
char[] buf = new char[1024];
int len = 0;
while((len=fr.read(buf))!=-1)
{
fw.write(buf,0,len);
}
}
catch (IOException e)
{
System.out.println("读写失败');
}
finally
{
if (fr!=null)
try
{
fr.close();
}
catch (IOException e)
{
}
if(fw!=null)
try
{
fr.close();
}
catch (IOException e)
{
}
}
}
public static void Copy_1()throws IOException
{
//创建目的地:
FileWriter fw = new FileWriter("Demo22.txt");
//与已有文件关联:
FileReader fr = new FileReader("c:\\Calendarhaha.java");
//从C盘读取一个字符就写入一个字符:
int ch=0;
while((ch=fr.read())!=-1)
{
fw.write(ch);
}
fr.close();
fw.close();
}
}