------- android培训、java培训、期待与您交流! ----------
主要内容:一、System;二、Runtime;三、Date;四、Calendar;五、Math;六、IO(Input Output)流;七、文件的读取
一、System类:包含一些有用的类字段和方法。它不能被实例化。方法都是静态的。
System:类中的方法和属性都是静态的。
out:标准输出,默认是控制台。
in:标准输入,默认是键盘。
描述系统一些信息。
获取系统属性信息:Properties getProperties();
例:
public static void main(String[] args)
{
Properties prop = System.getProperties();
//因为Properties是Hashtable的子类,也就是Map集合的一个子类对象。
//那么可以通过map的方法取出该集合中的运算。
//该集合中存储的都是字符串,没有泛型定义。
//如何在系统中自定义一些特有信息呢?
System.setProperty("mykey","myvalue");
//获取指定属性信息。
String value = System.getProperty("os.name");
System.out.println("value="+value);
//可不可以在jvm启动时,动态加载一些属性信息呢?
String v = System.getProperty("haha");
System.out.println("v="+v);
/*
//获取所有属性信息。
for(Object obj : prop.keySet())
{
String value = (String)prop.get(obj);
System.out.println(obj+"----"+value);
}
*/
}
}
二、Runtime:每个 Java 应用程序都有一个 Runtime 类实例,使应用程序能够与其运行的环境相连接。
可以通过 getRuntime 方法获取当前运行时。
应用程序不能创建自己的 Runtime 类实例。
Runtime对象
该类并没有提供构造函数。
说明不可以new对象,那么会直接想到该类中的方法都是静态的。
发现该类中还有非静态方法。
说明该类肯定会提供一个方法获取本类对象。而且该方法是静态的,并且返回值类型是本类类型。
由这个特点可以看出该类使用了单例设计模式完成。
该方法是staic Runtime getRuntime();
exec();在单独的进程中执行指定的字符串命令
例:
class RuntimeDemo
{
public static void main(String[] args) throws Exception
{
Runtime r = Runtime.getRuntime();
Process p = r.exec("notepad.exe SystemDemo.java");//用记事本打开指定文件SystemDemo.java;
Thread.sleep(4000);
p.destroy();//杀死一个子进程
}
}
三、Date:类 Date 表示特定的瞬间,精确到毫秒。
1,Date();分配 Date 对象并初始化此对象,以表示分配它的时间(精确到毫秒)。
2,SimpleDateFormat(String pattern);用给定的模式和默认语言环境的日期格式符号构造 SimpleDateFormat。
3,format(Date date);将一个 Date 格式化为日期/时间字符串。
例:
import java.util.*;
import java.text.*;
class DateDemo
{
public static void main(String[] args)
{
Date d = new Date();
System.out.println(d);//打印的时间看不懂,希望有些格式。
//将模式封装到SimpleDateFormat对象中。
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日E a:kk:mm:ss");
//调用format方法让模式格式化指定Date对象。
String time =sdf.format(d);
System.out.println("time="+time);
}
}
四、Calendar:类是一个抽象类,它为特定瞬间与一组诸如 YEAR、MONTH、DAY_OF_MONTH、HOUR 等
日历字段之间的转换提供了一些方法,并为操作日历字段(例如获得下星期的日期)提供了一些方法。
1,Calendar.getInstance()
使用默认时区和语言环境获得一个日历。
查表法显示年月日
例:
import java.util.*;
class CalendarDemo
{
public static void main(String[] args)
{
Calendar c = Calendar.getInstance();
String[] mons = {"一月","二月","三月","四月",
"五月","六月","七月","八月",
"九月","十月","十一月","十二月"};
String[] weeks = {"","星期日","星期一","星期二",
"星期三","星期四","星期五","星期六"};
int index = c.get(Calendar.MONTH);
int index1= c.get(Calendar.DAY_OF_WEEK);
sop(c.get(Calendar.YEAR)+"年");
//sop((c.get(Calendar.MONTH)+1)+"月");
sop(mons[index]);
sop(c.get(Calendar.DAY_OF_MONTH)+"日");
//sop("星期"+c.get(Calendar.DAY_OF_WEEK));
sop(weeks[index1]);
}
public static void sop(Object obj)
{
System.out.println(obj);
}
}
2,set(int field, int value)
将给定的日历字段设置为给定值。
3,add(int field, int amount)
根据日历的规则,为给定的日历字段添加或减去指定的时间量。
例:
import java.util.*;
class CalendarDemo2
{
public static void main(String[] args)
{
Calendar c = Calendar.getInstance();
c.set(2013,2,23);//设置日期
c.add(Calendar.DAY_OF_MONTH,-10);//日期减10天。
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);
sop(c.get(Calendar.YEAR)+"年");
//sop((c.get(Calendar.MONTH)+1)+"月");
sop(mons[index]);
sop(c.get(Calendar.DAY_OF_MONTH)+"日");
//sop("星期"+c.get(Calendar.DAY_OF_WEEK));
sop(weeks[index1]);
}
public static void sop(Object obj)
{
System.out.println(obj);
}
}
五、Math 类包含用于执行基本数学运算的方法,如初等指数、对数、平方根和三角函数。
例:
import java.util.*;
class MathDemo
{
public static void main(String[] args)
{
Random r = new Random();
for (int x=0;x<10 ;x++ )
{
//int d = (int)(Math.random()*10+1);//随机数。
int d = r.nextInt(10)+1;
sop(d);
}
}
public static void show()
{
double d = Math.ceil(-16.34);//ceil返回大于指定数据的最小整数。
double d1 = Math.floor(12.34);//floor返回小于指定数据的最大整数。
long l = Math.round(12.54);//四舍五入
sop("d="+d);
sop("d1="+d1);
sop("l="+l);
double d2 = Math.pow(2,4);//2的4次方
sop("d2="+d2);
}
public static void sop(Object obj)
{
System.out.println(obj);
}
}
六、IO(Input Output)流
IO流用来处理设备之间的数据传输
Java对数据的操作是通过流的方式
Java用于操作流的对象都是IO包中
流按操作数据分为两种:字节流与字符流。
流按流向分为:输入流,输入流。
字节流的抽象基类:
InputStream OutputStream
字符流的抽象基类:
Reader Writer
注:由这四个类派生出来的子类名称都是
以其父类名作为子类名的后缀。
如:InputStream的子类FileInputStream
如:Reader的子类FileReader
1,FileWriter:
需求:在硬盘上,创建一个文件并写入一些文字数据。
找到一个专门用于操作文件的Writer子类对象。
FileWriter。后缀名是父类名。前缀名是该流对象的功能。
1.1 FileWriter fw = new FileWriter("demo.txt");
1.2 fw.write("abcde");
1.3 fw.flush();
1.4 fw.close();
例:
import java.io.*;
class FileWriterDemo
{
public static void main(String[] args) throws IOException
{
//创建一个FileWriter对象,该对象一被初始化就必须要明确被操作的文件。
//而且该文件会被创建到指定目录下。如果该目录下已有同名文件,将被覆盖。
//其实该步就是在明确数据要存放的目的地
FileWriter fw = new FileWriter("demo.txt");
//调用write方法,将字符串写人到流中。
fw.write("abcde");
//刷新流对象中的缓冲中的数据。
//将数据刷到目的地中。
//fw.flush();
//关闭流资源,但是关闭之前会刷新一次内部的缓冲中的数据。
//将数据刷到目的地中。
//和flush区别:flush刷新后,流可以继续使用,close刷新后,会将流关闭。
fw.close();
fw.write("haha");
}
}
2,IO异常的处理方式。
例:
import java.io.*;
class FileWriterDemo2
{
public static void main(String[] args)
{
FileWriter fw = null;
try
{
fw = new FileWriter("demo.txt");
fw.write("abcdefg");
}
catch (IOException e)
{
System.out.println("catch:"+e.toString());
}
finally
{
try
{
if(fw!=null)
fw.close();
}
catch (IOException e)
{
System.out.println(e.toString());
}
}
}
}
3,演示对已有文件的数据续写
传递一个true参数,代表不覆盖已有的文件。并在已有文件的末尾处进行数据续写。
FileWriter fw = new FileWriter("demo.txt",true);
例:
import java.io.*;
class FileWriterDemo3
{
public static void main(String[] args)throws IOException
{
//传递一个true参数,代表不覆盖已有的文件。并在已有文件的末尾处进行数据续写。
FileWriter fw = new FileWriter("demo.txt",true);
fw.write("nihao\r\nxiexie");
fw.close();
}
}
七、文件的读取
1,文本文件读取方式一:read():一次读一个字符。而且会自动往下读。
例:
import java.io.*;
class FileReaderDemo
{
public static void main(String[] args) throws IOException
{
//创建一个文件读取流对象,和指定名称的文件相关联。
//要保证该文件是已经存在的,如果不存在,会发生异常FileNotFoundException
FileReader fr = new FileReader("demo.txt");
//调用读取流对象的read方法。
//read():一次读一个字符。而且会自动往下读。
int ch=0;
while((ch=fr.read())!=-1)
{
System.out.println("ch="+(char)ch);
}
/*
while(true)
{
int ch = fr.read();
if(ch==-1)
break;
System.out.println("ch="+(char)ch);
}
*/
fr.close();
}
}
2,文本文件读取方式二:通过字符数组进行读取。
例:
import java.io.*;
class FileReaderDemo2
{
public static void main(String[] args) throws IOException
{
FileReader fr = new FileReader("demo.txt");
//定义一个字符数组,用于存储读到的字符。
//该read(char[])返回的是读到字符个数。
char[] buf = new char[1024];
int num = 0;
while((num=fr.read(buf))!=-1)
{
System.out.println(new String(buf,0,num));
}
fr.close();
}
}
3,练习:读取一.java文件,并打印在控制台上。
例:
import java.io.*;
class FileReaderTest
{
public static void main(String[] args) throws IOException
{
FileReader fr = new FileReader("DateDemo.java");
char[] buf = new char[1024];
int num = 0;
while((num=fr.read(buf))!=-1)
{
System.out.print(new String(buf,0,num));
}
fr.close();
}
}
4,练习:将C盘一个文本文件复制到D盘:(重点掌握)
复制的原理:
其实就是将C盘下的文件数据存储到D盘的一个文件中。
步骤:
1,在D盘创建一个文件,用于存储C盘文件中的数据。
2,定义读取流和C盘文件关联。
3,通过不断的读写完成数据存储。
4,关闭资源。
例:
import java.io.*;
class CopyText
{
public static void main(String[] args) throws IOException
{
copy_2();
}
public static void copy_2()
{
FileWriter fw = null;
FileReader fr = null;
try
{
fw = new FileWriter("SystemDemo_copy.txt");
fr = new FileReader("SystemDemo.java");
char[] buf = new char[1024];
int len=0;
while((len=fr.read(buf))!=-1)
{
fw.write(buf,0,len);
}
}
catch (IOException e)
{
throw new RuntimeException("读写失败");
}
finally
{
if(fr!=null)
try
{
fr.close();
}
catch (IOException e)
{
}
if(fw!=null)
try
{
fw.close();
}
catch (IOException e)
{
}
}
}
//从C盘读一个字符,就往D盘写一个字符。
public static void copy_1()throws IOException
{
//创建目的地
FileWriter fw = new FileWriter("RuntimeDemo_copy.txt");
//与已有文件关联
FileReader fr = new FileReader("RuntimeDemo.java");
int ch = 0;
while((ch=fr.read())!=-1)
{
fw.write(ch);
}
fw.close();
fr.close();
}
}
424

被折叠的 条评论
为什么被折叠?



