黑马程序员_学习笔记第18天——IO流、字符流

---------------------- ASP.Net+Android+IOS开发、 href="http://edu.csdn.net"target="blank">.Net培训、期待与您交流! ----------------------

1、System:描述系统一些信息,类中的方法和属性都是静态的。

获取系统属性信息:Properties  getProperties();

out:标准输出,默认控制台

in:标准输入,默认键盘

因为Properties是Hashtable的子类,也就是Map集合的一个子类对象,那么可以通过map的方法取出该集合中的元素,该集合中存储都是字符串,没有泛型定义。

在系统中自定义特有信息:System.setProperty("mykey","myvalue");

获取指定属性信息:System.getProperty("mykey");

public static void main(String[] args) {
		Properties pro = System.getProperties();
		//System.setProperty("aaaa","bbbb");
		for(Object obj : pro.keySet()){
			String value = (String)pro.get(obj);
			System.out.println(obj+"::"+value);
		}

		String value = System.getProperty("aaaa");
		System.out.println(value);
		
	}
jvm启动时动态加载一些属性信息:java -Dhaha=qqq  SystemDemo//运行SystemDemo.class文件,同时在属性中增加键值对haha=qqq

2、Runtime对象

该类并没有提供构造函数,说明不可以new对象,那么会直接想到该类中的方法都是静态的,发现该类中还有非静态方法,说明该类肯定会提供了方法获取本类对象,而且该方法是静态的,并返回值类型是本类类型。

该方法是static Runtime  getRuntime();

由这个特点可以看出该类使用了单例设计模式。

public static void main(String[] args) throws Exception{
		
		Runtime r = Runtime.getRuntime();
		Process p = r.exec("notepad.exe SystemDemo.java");//用记事本打开

//		Thread.sleep(4);
//		p.destroy();//杀掉子进程
	}
3、Date:java.text.*;

将模式封装到SimpleDateFormat对象中

	public static void main(String[] args) {

		Date d = new Date();
		System.out.println(d);
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
		String time = sdf.format(d);//调用format方法让模式格式化指定Date对象
		System.out.println(time);
		
		long l = System.currentTimeMillis();
		Date d1 = new Date(l);
		System.out.println(d1);

	}

4、Calendar

public static void main(String[] args) {
		// TODO Auto-generated method stub
		Calendar c = Calendar.getInstance();
		
		String[] mons = {"一月","二月","三月","四月",
						"五月","六月","七月","八月",
						"九月","十月","十一月","十二月"};
		int index = c.get(Calendar.MONTH);
		String[] weeks = {
							"","星期日","星期一","星期二","星期三","星期四","星期五","星期六"
							};
		int index2 = c.get(Calendar.DAY_OF_WEEK);
		System.out.println(c.get(Calendar.YEAR)+"年");
		System.out.println(mons[index]);
		System.out.println(c.get(Calendar.DAY_OF_MONTH)+"日");
		System.out.println(weeks[index2]);
		//System.out.println(c);

	}

//练习:1、求某一年的二月份有多少天?
//思路:c.set(year,2,1)//某一年的3月1日
//c.add(Calendar.DAY_OF_MONTH,-1);//3月1日,往前推一天,就是2月最后一天
//练习:2、获取昨天的现在这个时刻
//思路:c.add(Calendar.DAY_OF_MONTH,-1);
import java.util.Calendar;

public class UtilCalendarDemo2 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Calendar c = Calendar.getInstance();
		//c.set(2014,1,30);
		c.add(Calendar.YEAR, 1);
		printCalendar(c);

	}
	public static void printCalendar(Calendar c ) {
		String[] mons = {"一月","二月","三月","四月",
						"五月","六月","七月","八月",
						"九月","十月","十一月","十二月"};
		int index = c.get(Calendar.MONTH);
		String[] weeks = {
							"","星期日","星期一","星期二","星期三","星期四","星期五","星期六"
							};
		int index2 = c.get(Calendar.DAY_OF_WEEK);
		System.out.println(c.get(Calendar.YEAR)+"年");
		System.out.println(mons[index]);
		System.out.println(c.get(Calendar.DAY_OF_MONTH)+"日");
		System.out.println(weeks[index2]);
	}

}

5、Math
1)Math类中
ceil方法返回大于指定数据的最小整数  double d = Math.ceil(13.2);//返回14.0
floor方法返回小于指定数据的最大整数 double d1 = Math.floor(14.2);//返回14.0
round方法四舍五入  long l = Math.round(12.4);//返回12
pow方法指数幂   double d2 = Math.pow(2, 3);//返回8.0
2)随机数
两种方法:
第一种用Math类中的random()方法:int i = (int)(Math.random()*10+1);//1-10的随机数。
第二种用Random类中的nextInt(n)方法:Random r = new Random();int i = r.nextInt(10)+1;//不用强转
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		double d = Math.ceil(13.2);
		System.out.println(d);
		double d1 = Math.floor(14.2);
		System.out.println(d1);
		int l = Math.round(12.4f);
		System.out.println(l);
		double d2 = Math.pow(2, 3);
		System.out.println(d2);
		
		Random r = new Random();
		for(int x=0;x<10;x++) {
			//int i = (int)(Math.random()*10+1);
			int i = r.nextInt(10)+1;
			System.out.println(i);
		}
	}

6、IO流

IO流用来处理设备之间的数据传输,java对数据的操作是通过流的方式,java用于操作流的对象都在IO包中。

流按操作数据类型分为两种:字节流与字符流。

流按流向分为:输入流与输出流。

7、IO流常用基类:

字节流的抽象基类:InputStream,OutputStream

字符流的抽象基类:Reader,Writer

注:由这四个类派生出来的子类名称都是以父类名作为子类名的后缀。如:InputStream的子类FileInputStream,Reader的子类FileReader。

8、字符流特点:

既然IO流是用于操作数据的,那么数据的最常见体现形式是文件。

	public static void main(String[] args) throws IOException {
		//创建一个FileWriter对象,该对象一被初始化就必须要明确被操作的文件
		//而且给文件会被创建到指定目录下,如果该目录下已有同名文件,将被覆盖
		//其实该步就是在明确数据要存放的目的地
		FileWriter fw = new FileWriter("1.txt");
		//调用write方法,将字符串写入到流中
		fw.write("abcde");
		//刷新流对象中的缓冲中的数据
		//将数据刷到目的地中
		//fw.flush();
		
		//关闭流资源,但是关闭之前会刷新一次内部的缓冲中的数据
		//将数据刷到目的地中
		//和flush区别:flush刷新后,流可以继续使用,close刷新后,会关闭流资源
		fw.close();

	}

9、IO流异常处理方式:

	public static void main(String[] args) {
		FileWriter fw = null;
		try {
			fw = new FileWriter("demo.txt");
			fw.write("abcd");
		}catch(IOException e) {
			System.out.println(e.toString());
		}
		finally{
			try {
				if(fw!=null)
					fw.close();
			}catch(IOException e) {
				System.out.println(e.toString());
			}
		}

	}

10、IO流中文件的续写:

	public static void main(String[] args) throws IOException{
		//传递一个true参数,代表不覆盖已有文件,并在已有文件的末尾处进行数据续写。
		FileWriter fw = new FileWriter("demo.txt",true);
		fw.write("nihao\r\nxiexie");
		fw.write('2');
		fw.close();

	}

11、IO流文本文件读取方式(两种):

	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.print((char)ch);
		}
		*/
		//读取方法二:通过字符数组进行读取
		
		//定义一个字符数组,用于存储读到的字符
		//该read(char[])返回的是读到字符个数
		char[] buf = new char[1024];
		int num =0;
		while((num=fr.read(buf))!=-1) {
			System.out.print(new String(buf,0,num));
		}
		fr.close();

	}

12、IO流拷贝文本文件:

/*
 练习:从一个目录下c盘拷贝txt文件到另一个目录下d盘。
 1、在d盘创建一个文件,用于存储c盘文件中的数据
 2、定义读取流和c盘文件关联
 3、通过不断的读写完成数据存储
 4、关闭资源
 */
public class CopyTextDemo {

	public static void main(String[] args) throws IOException/**/{
		copy_2();

	}
//   方法一:从c盘读一个字符就往d盘中写入一个字符
	public static void copy_1() throws IOException {
		//创建目的地
		FileWriter fw = new FileWriter("d:\\1.txt",true);
		FileReader fr = new FileReader("c:\\1.txt");
		int ch = 0;
		while((ch =fr.read())!=-1) {
			fw.write(ch);
		}
		fw.close();
		fr.close();
		
	}
	public static void copy_2() {
		FileWriter fw = null;
		FileReader fr = null;
		try {
			fw = new FileWriter("d:\\2.txt");
			fr = new FileReader("c:\\1.txt");
			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(e.toString());
			throw new RuntimeException("读写失败");
		}
		finally {
			try {
				if(fw!=null)
					fw.close();
			}catch(IOException e) {
				System.out.println(e.toString());
			}
			try{
				if(fr!=null) 
					fr.close();
			}catch(IOException e) {
				System.out.println(e.toString());
			}
		}
	}
}


---------------------- ASP.Net+Android+IOS开发、 href="http://edu.csdn.net"target="blank">.Net培训、期待与您交流! ----------------------详细请查看: http://edu.csdn.net
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值