其他对象和IO流
一、System类
System:类中的方法和属性都是静态的。
out:标准输出流。默认是控制台。
in:标准输入流。默认是键盘。
获取系统信息:
static Properties getProperties()
确定当前的系统属性。
static String getProperty(String key)
获取指定键指示的系统属性。
static String getProperty(String key, String def)
获取用指定键描述的系统属性。
例:
通过查阅Api文档发现,Properties是HashTable的子类,也就是Map集合的一个子类对象,那么可以通过Map的方法取出该集合中的元素。
import java.util.*;
class Demo
{
public static void main(String []args){
Properties pro = System.getProperties();
for(Map.Entry<Object,Object> me: pro.entrySet()){
String key = (String)me.getKey();
String value = (String)me.getValue();
System.out.println(key+"------"+value);
}
}
}
在系统中自定义一些特有信息:
Object setProperty(String key, String value)
调用 Hashtable 的方法 put。
例:
import java.util.*;
class Demo
{
public static void main(String []args){
Properties pro = System.getProperties();
pro.setProperty("mykey", "hahahahahahahahahahahaha");
for(Map.Entry<Object,Object> me: pro.entrySet()){
String key = (String)me.getKey();
String value = (String)me.getValue();
System.out.println(key+"------"+value);
}
}
}
获取指定属性信息:
例:
import java.util.*;
class Demo
{
public static void main(String []args){
Properties pro = System.getProperties();
pro.setProperty("mykey", "hahahahahahahahahahahaha");
String key = "mykey";
String value = pro.getProperty(key);
System.out.println(key+":::"+value);
}
}
手动设置value值:
例:
import java.util.*;
class Demo
{
public static void main(String []args){
Properties pro = System.getProperties();
String key = "mykey";
String value = pro.getProperty(key);
System.out.println(key+":::"+value);
}
}
二、Runtime类
Runtime类:该类中没有提供构造函数,说明不可以new对象。那么会直接想到该类中的方法都是静态的,查阅Api文档发现该类中还有非静态方法。说明该类肯定会提供了方法获取本类对象,而且该方法是静态的,并且返回值类型是本类类型。由这个特点可以看出该类使用了单例设计模式完成。
获取本类对象方法:
static Runtime getRuntime()
返回与当前 Java 应用程序相关的运行时对象。
例:
class Demo
{
public static void main(String []args){
Runtime r = Runtime.getRuntime();
r.exec("qq.exe");
}
}
打开了QQ登陆界面。
例:
class Demo
{
public static void main(String []args) throws Exception{
Runtime r = Runtime.getRuntime();
r.exec("notepad.exe Demo.java");
}
}
用记事本打开了.java文件。
三、Date类
import java.util.*;
class Demo
{
public static void main(String []args) throws Exception{
Date d = new Date();
System.out.println(d);
}
}
打印的时间看不懂,希望有些格式。
例:
import java.util.*;
import java.text.*;
class Demo
{
public static void main(String []args) throws Exception{
Date d = new Date();
//将模式封装到SimpleDateFormat对象中
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 E HH:mm:ss");
//调用format();方法,让模式格式化指定的Date对象
String date = sdf.format(d);
System.out.println(date);
}
}
四、Calendar类
Calendar类:是Date类升级,更为人性化。
查表法进行优化:
import java.util.*;
import java.text.*;
class Demo
{
public static void main(String []args) throws Exception{
Calendar c = Calendar.getInstance();
c.set(2013,6,30);
String []month = {"一","二","三","四","五","六","七","八","九","十","十一","十二"};
String []week = {"日","一","二","三","四","五","六"};
System.out.println(c.get(Calendar.YEAR)+"年");
System.out.println(month[c.get(Calendar.MONTH)-1]+"月");
System.out.println(c.get(Calendar.DAY_OF_MONTH)+"日");
System.out.println("星期"+week[c.get(Calendar.DAY_OF_WEEK)+1]);
}
}
练习:获取任意年的二月有多少天
思路:根据指定年设置一个时间,就是c.set(year,2,1);某一年的3月1日。c.add(Calendar.DAY_OF_MONTH,-1);3月1日向前推一天,就是二月的最后一天。
import java.util.*;
import java.text.*;
class Demo
{
public static void main(String []args) throws Exception{
int year = 2013;
Calendar c = Calendar.getInstance();
c.set(year,2,1);
c.add(Calendar.DAY_OF_MONTH,-1);
System.out.println(c.get(Calendar.DAY_OF_MONTH));
}
}
练习:获取昨天的现在这个时刻
import java.util.*;
import java.text.*;
class Demo
{
public static void main(String []args) throws Exception{
int year = 2013;
Calendar c = Calendar.getInstance();
c.add(Calendar.DAY_OF_MONTH,-1);
System.out.println(c.getTime());
}
}
五、Math类
abs():返回绝对值。
ceil():返回指定数据的最小整数。
static double ceil(double a)
返回最小的(最接近负无穷大)double 值,该值大于等于参数,并等于某个整数。
例:class Demo
{
public static void main(String []args) throws Exception{
double d = Math.ceil(12.34);
System.out.println(d);
}
}
floor():正好和ceil()相反。
round():四舍五入
例:
class Demo
{
public static void main(String []args) throws Exception{
double d1 = Math.round(12.34);
System.out.println(d1);
double d2 = Math.round(12.54);
System.out.println(d2);
}
}
pow():次幂运算
例:
class Demo
{
public static void main(String []args) throws Exception{
double d = Math.pow(2,3);
System.out.println(d);
}
}
random()(重点):随机
例:
class Demo
{
public static void main(String []args) throws Exception{
for(int x=0;x<10;x++){
int d = (int)(Math.random()*10+1);
System.out.println(d);
}
}
}
六、Random类
Random类:随机类
例:
import java.util.*;
class Demo
{
public static void main(String []args) throws Exception{
Random r = new Random();
for(int x=0;x<10;x++){
int i = r.nextInt(10)+1;
System.out.println(i);
}
}
}
练习:给定一个小数,保留该小数的最后两位。
例:
import java.util.*;
import java.text.*;
class Demo
{
public static void main(String []args) throws Exception{
double d = 12.3426;
NumberFormat nf = NumberFormat.getNumberInstance();
nf.setMaximumFractionDigits(2);
System.out.println(d);
d = Double.parseDouble(nf.format(d));
System.out.println(d);
}
}
七、IO流(Input/Output)

答:IO流用来处理设备之间的数据传输。Java对数据的操作是通过流的方式,Java用于操作留的对象都在IO包中。
流按操作数据分为两种:字符流与字节流。
流按流向分为:输入流和输出流。
字节流的常用基类:InputStream,OutputStream。
字符流的常用基类:Reader,Writer。
注:由这四个类派生出来的子类名称都是以其父类名作为子类名的后缀。如InputStream的子类FileInputStream,Reader的子类FileReader。
既然IO流是用于操作数据的,那么数据的最常见体现形式是:文件。
需求:在硬盘上创建一个文件,并写入一些文字数据。
import java.io.*;
class Demo
{
public static void main(String []args) throws Exception{
//创建一个FileWriter对象,该对象一被初始化就必须要明确被操作的文件
//其实就是在明确数据要存放的目的地
//而且该文件会被创建到指定目录下,如果该文件目录下有同名文件,将被覆盖
FileWriter fw = new FileWriter("Test.txt");
//调用write方法,将字符串写入到流中
fw.write("abcde");
//刷新流对象中的缓冲中的数据,将流对象刷新到目的地中
fw.flush();
//关闭流资源,但是关闭之前会刷新一次内部的缓冲中的数据,将数据刷到目的地中
fw.close();
}
}
close()和flush()的区别:flush刷新后,流可以继续使用,close刷新后,会将流关闭。
IO异常的处理方式:
例:
import java.io.*;
class Demo
{
public static void main(String []args){
FileWriter fw = null;
try{
fw = new FileWriter("Test.txt");
fw.write("abcde");
fw.flush();
}
catch(IOException e){
System.out.println(e.toString());
}
finally{
if(fw!=null){
try{
fw.close();
}
catch(IOException e){
System.out.println(e.toString());
}
}
}
}
}
练习:对已有文件的数据续写
查阅Api文档:
FileWriter(String fileName, boolean append)
根据给定的文件名以及指示是否附加写入数据的 boolean 值来构造 FileWriter 对象。
import java.io.*;
class Demo
{
public static void main(String []args){
FileWriter fw = null;
try{
fw = new FileWriter("Test.txt",true);
fw.write("kkkkkk");
fw.flush();
}
catch(IOException e){
System.out.println(e.toString());
}
finally{
if(fw!=null){
try{
fw.close();
}
catch(IOException e){
System.out.println(e.toString());
}
}
}
}
}
Windows下换行:"\r\n"。
例:
import java.io.*;
class Demo
{
public static void main(String []args){
FileWriter fw = null;
try{
fw = new FileWriter("Test.txt",true);
fw.write("\r\n"+"kkkkkk");
fw.flush();
}
catch(IOException e){
System.out.println(e.toString());
}
finally{
if(fw!=null){
try{
fw.close();
}
catch(IOException e){
System.out.println(e.toString());
}
}
}
}
}
Read读取流:使用的是默认编码。
例:
第一种方式:一个字符一个字符读取。
import java.io.*;
class Demo
{
public static void main(String []args) throws Exception{
//创建一个读取流对象,和指定名称的文件相关联,要保证该文件已经存在。
//如果不存在,会发生FileNotFoundException文件找不到异常
FileReader fr = new FileReader("Test.txt");
int ch = 0;
//调用读取流对象的read方法,返回-1代表已经读到末尾,可作为循环条件。
//read方法一次读一个字符,而且会自动往下读。
while((ch = fr.read())!=-1){
System.out.print((char)ch);
}
}
}
第二种方式:通过字符数组进行读取。
import java.io.*;
class Demo
{
public static void main(String []args) throws Exception{
FileReader fr = new FileReader("Test.txt");
//定义一个字符数组。用于存储读到的字符,该read(char[])返回的是读到的字符个数。
char []chs = new char[1024];
int len = 0;
while((len = fr.read(chs))!=-1){
System.out.println(new String(chs,0,len));
}
}
}
练习:读取一个.Java文件,并打印在控制台上。
import java.io.*;
class Demo
{
public static void main(String []args){
FileReader fr = null;
try{
fr = new FileReader("Demo.java");
char []chs = new char[1024];
int len = 0;
while((len = fr.read(chs))!=-1){
System.out.println(new String(chs,0,len));
}
}
catch(IOException e){
System.out.println(e.toString());
}
finally{
if(fr!=null){
try{
fr.close();
}
catch(IOException e){
System.out.println(e.toString());
}
}
}
}
}
步骤:
1.在D盘创建一个文件,用于存储C盘文件中的数据。
2.定义读取流和C盘文件关联。
3.通过不断的读写完成数据的存储。
4.关闭资源。
第一种方式:
import java.io.*;
class Demo
{
public static void main(String []args) throws Exception{
FileReader fr = new FileReader("C://Test.txt");
FileWriter fw = new FileWriter("D://Test_copy.txt");
int ch = 0;
while((ch = fr.read())!=-1){
fw.write((char)ch);
fw.flush();
}
fr.close();
fw.close();
}
}
第二种方式:
import java.io.*;
class Demo
{
public static void main(String []args){
FileReader fr = null;
FileWriter fw = null;
try{
fr = new FileReader("C://Test.txt");
fw = new FileWriter("D://Test_copy.txt");
char []chs = new char[1024];
int len = 0;
while((len = fr.read(chs))!=-1){
fw.write(new String(chs,0,len));
fw.flush();
}
}
catch(IOException e){
throw new RuntimeException("读写失败");
}
finally{
if(fr != null){
try{
fr.close();
}
catch(IOException e){
System.out.println(e.toString());
}
}
if(fw != null){
try{
fw.close();
}
catch(IOException e){
System.out.println(e.toString());
}
}
}
}
}
解析:图示
读写完成后,在finally中,关闭两个流资源fr.close(),fw.close()。