第六章 其他对象和IO流(一)
一、其他对象(System):类中的方法和属性都是静态的。
1、out:标准输出,默认是控制台
2、in:标准输入,默认是键盘
描述系统一些信息。
获取系统属性信息:Properties getProperties();
import java.util.*;
class Demo
{
public static void main(String args[]) throws Exception
{
Properties prop = System.getProperties();
//因为Properties是Hashtable的子类,也就是Map集合的一个类的对象。
//那么可以通过Map的方法获取出该集合中的元素。
//该集合中存储都是字符串,没有泛型定义
//如何在系统中自定义一些特有信息呢?
System.setProperty("mykey","myvalue");
//可不可以在jvm启动时,动态加载一些属性信息呢
//命令行 java -Dhaha=qqqq 类
//获取指定属性信息
String value2 = System.getProperty("os.name");
System.out.println("value="+value2);
//获取所有属性信息。
for(Object obj : prop.keySet()){
String value = (String)prop.get(obj);
System.out.println(obj+"::"+value);
}
}
}
二、其他对象(Runtime)
该类并没有提供构造函数,
说明不可以new对象。那么会直接想发到该类中的方法都是静态的
但是,该类中还有非静态方法。
说明该类肯定会提供了方法获取本类的对象。而且该方法是静态的,并且返回类型是本类类型。
该方式是 static Runtime getRuntime();
Process p = r.exec("winmine.exe");
p.destroy();
r.exec("notepad.exe **.txt");
三、其他对象(Date)
java.lang.Object
java.text.Format
java.text.DateFormat
java.text.SimpleDateFormat
import java.util.*;
import java.text.*;
class Demo{
public static void main(String args[]) throws Exception{
Date d = new Date();
System.out.println(d);//打印时间看不懂,希望有些格式
//将模式封装到 SimpleDateFormate对象中
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 hh:mm:ss");
//调用 format方法让模式格式化指定的date对象
String time = sdf.format(d);
System.out.println(time);
}
}
四、其他对象(Calendar)
import java.util.*;
import java.text.*;
class Demo{
public static void main(String args[]) throws Exception{
Calendar c =Calendar.getInstance();
c.set(2012,2,23); //指定时间
c.add(Calendar.MONTH,4);//增加时间
c.add(Calendar.MONTH,-4);//往前推时间,不仅仅是减一年
String[] mons = {"一","二","三","四","五","六","七","八","九","十","十一","十二"};
String[] weeks = {"日","一","二","三","四","五","六"};
sop(c.get(Calendar.YEAR)+"年"+mons[c.get(Calendar.MONTH)]+"月"+c.get(Calendar.DAY_OF_MONTH)+"日 星期"+weeks[c.get(Calendar.DAY_OF_WEEK)-1]);
}
public static void sop(Object obj){
System.out.println(obj.toString());
}
}
五、其他对象(Math-Random)
class Demo{
public static void main(String args[]) throws Exception{
Random r = new Random();
while(true){
sop((int)(Math.random()*10+1));
r.nextInt(10)+1;
}
}
public static void sop(Object obj){
System.out.println(obj.toString());
}
}
六、IO流(概述)
Input Output
1、IO流用来处理设备之间的数据传输
2、Java对数据的操作是通过流的方式
3、Java用于操作流的对象都在IO包中
4、流按操作数据分为两种:字节流与字符流
5、流按流向分为:输入流,输出流
6、字节流的抽象基类
InputStrea,OutputStream
7、字符流的抽象基类
Reader,Writer
8、注:由这四个类派生出来的子类名称都是以其父类名作为子类明的后缀
(1)、InputStream的子类FileInputStream
(2)、Readerde 子类 FileReader
———————————————————————————————————————————————————
七、IO流(FileWriter)
1、用于操作文件的Writer子类对象,FileWriter。后缀名是父类名,前缀名是该流对象的功能
import java.io.*;
class Demo{
public static void main(String args[]) throws Exception{
//创建一个FileWriter对象,该对象一被初始化,就必须要明确被操作的文件
//而且该文件会被创建到指定目录下,如果该目录下已有同名的文件,将被覆盖。
//其实该步就是明确数据要存放的目的地
FileWriter fw = new FileWriter("Demo.txt");
//调用writer方法,将字符串写入到流中,会产生异常
fw.write("abc");
//刷新该流的缓冲。将数据刷新到文件中
fw.write("def");
fw.flush();
//关闭刘子源,但是关闭之前会刷新一次内部的缓冲的数据。,将数据刷到目的地中,和flush区别,flush(),刷新后,流可以继续使用,close刷新后,会将流关闭。
//操作完,必须关闭
fw.close();
}
public static void sop(Object obj){
System.out.println(obj.toString());
}
}
八、IO流(IO异常处理方式)
import java.io.*;
class Demo{
public static void main(String args[]){
FileWriter fw = null;
try{
fw = new FileWriter("rDemo.txt");
fw.write("abcdefg");
}catch(IOException e){
sop(e);
}finally{
try{
if(fw!=null)
fw.close();
}catch(IOException e){
sop(e);
}
}
}
public static void sop(Object obj){
System.out.println(obj.toString());
}
}
九、文件的续写
FileWriter(String fileName)
根据给定的文件名构造一个 FileWriter 对象。
FileWriter(String fileName, boolean append)
根据给定的文件名以及指示是否附加写入数据的 boolean 值来构造 FileWriter 对象。
windows中,换行是 \r\n
linux 是\n
import java.io.*;
class Demo{
public static void main(String args[]){
FileWriter fw = null;
try{
//为true表示不覆盖已有文件,并且续写
fw = new FileWriter("Demo.txt",true);
fw.write("\r\n换行");
}catch(IOException e){
sop(e);
}finally{
try{
if(fw!=null)
fw.close();
}catch(IOException e){
sop(e);
}
}
}
public static void sop(Object obj){
System.out.println(obj.toString());
}
}
十、IO流(文本文件读取方式一)
int read()
读取单个字符。
import java.io.*;
class Demo{
public static void main(String args[]) throws Exception{
//创建一个文件读取流对象,和指定名称的文件相关联。
//要保证该文件是已经存在的,如果不存在,会发生异常 FileNotFoundException
FileReader fr = new FileReader("Demo.txt");
//read()方法,读取一个字符,每次读一个,并且会接着上一个往下读,若达到末尾,则返回-1
int ch = 0;
while( (ch=fr.read())!=-1){
System.out.print((char)ch);
}
}
}
十一、IO流(文本文件读取方式二)
int read(char[] cbuf)
将字符读入数组。
import java.io.*;
class Demo{
public static void main(String args[]) throws Exception{
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(num+"..."+new String(buf,0,num));
}
}
}
十二、IO流(文本文件读取练习)
//读取一个java文件,并打印
import java.io.*;
class Demo{
public static void main(String args[]) throws Exception{
FileReader fr = new FileReader("Demo.java");
//定义一个字符串数组,用于存储到的字符
//该 read(char[])返回的是读到的字符个数
char[] buf = new char[50];
int num = 0;
while((num = fr.read(buf))!=-1){
//不可以换行
System.out.print(num+"..."+new String(buf,0,num));
}
}
}
十三、IO流(拷贝文本文件)
复制的原理,其实就是将一个文件的数据存储到另一个文件中
//复制文件
/*
1、在D盘创建一个文件,用于存储C盘文件中的数据
2、定义读取流和C盘文件关联
3、通过不断的读写完成数据存储
4、关闭资源
*/
import java.io.*;
class Demo{
public static void main(String args[]) throws Exception{
copy_2();
}
public static void copy_1() throws Exception{
//创建目的地
FileWriter fw = new FileWriter("Demo_copy.java");
//与已有文件关联
FileReader fr = new FileReader("Demo.java");
int ch = 0;
while((ch = fr.read())!=-1){
fw.write(ch);
}
fw.close();
fr.close();
}
public static void copy_2() throws IOException{
//创建目的地
FileWriter fw = null;
//与已有文件关联
FileReader fr = null;
try{
fw = new FileWriter("Demo_copy.java");
fr = new FileReader("Demo.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){
}
}
}
}