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

来源:互联网 发布:java classloader级别 编辑:程序博客网 时间:2024/05/01 03:57
---------------------- 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");

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. public static void main(String[] args) {  
  2.         Properties pro = System.getProperties();  
  3.         //System.setProperty("aaaa","bbbb");  
  4.         for(Object obj : pro.keySet()){  
  5.             String value = (String)pro.get(obj);  
  6.             System.out.println(obj+"::"+value);  
  7.         }  
  8.   
  9.         String value = System.getProperty("aaaa");  
  10.         System.out.println(value);  
  11.           
  12.     }  
jvm启动时动态加载一些属性信息:java -Dhaha=qqq  SystemDemo//运行SystemDemo.class文件,同时在属性中增加键值对haha=qqq

2、Runtime对象

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

该方法是static Runtime  getRuntime();

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

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. public static void main(String[] args) throws Exception{  
  2.           
  3.         Runtime r = Runtime.getRuntime();  
  4.         Process p = r.exec("notepad.exe SystemDemo.java");//用记事本打开  
  5.   
  6. //      Thread.sleep(4);  
  7. //      p.destroy();//杀掉子进程  
  8.     }  
3、Date:java.text.*;

将模式封装到SimpleDateFormat对象中

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. public static void main(String[] args) {  
  2.   
  3.     Date d = new Date();  
  4.     System.out.println(d);  
  5.       
  6.     SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");  
  7.     String time = sdf.format(d);//调用format方法让模式格式化指定Date对象  
  8.     System.out.println(time);  
  9.       
  10.     long l = System.currentTimeMillis();  
  11.     Date d1 = new Date(l);  
  12.     System.out.println(d1);  
  13.   
  14. }  

4、Calendar
[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. public static void main(String[] args) {  
  2.         // TODO Auto-generated method stub  
  3.         Calendar c = Calendar.getInstance();  
  4.           
  5.         String[] mons = {"一月","二月","三月","四月",  
  6.                         "五月","六月","七月","八月",  
  7.                         "九月","十月","十一月","十二月"};  
  8.         int index = c.get(Calendar.MONTH);  
  9.         String[] weeks = {  
  10.                             "","星期日","星期一","星期二","星期三","星期四","星期五","星期六"  
  11.                             };  
  12.         int index2 = c.get(Calendar.DAY_OF_WEEK);  
  13.         System.out.println(c.get(Calendar.YEAR)+"年");  
  14.         System.out.println(mons[index]);  
  15.         System.out.println(c.get(Calendar.DAY_OF_MONTH)+"日");  
  16.         System.out.println(weeks[index2]);  
  17.         //System.out.println(c);  
  18.   
  19.     }  

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. //练习:1、求某一年的二月份有多少天?  
  2. //思路:c.set(year,2,1)//某一年的3月1日  
  3. //c.add(Calendar.DAY_OF_MONTH,-1);//3月1日,往前推一天,就是2月最后一天  
  4. //练习:2、获取昨天的现在这个时刻  
  5. //思路:c.add(Calendar.DAY_OF_MONTH,-1);  
  6. import java.util.Calendar;  
  7.   
  8. public class UtilCalendarDemo2 {  
  9.   
  10.     public static void main(String[] args) {  
  11.         // TODO Auto-generated method stub  
  12.         Calendar c = Calendar.getInstance();  
  13.         //c.set(2014,1,30);  
  14.         c.add(Calendar.YEAR, 1);  
  15.         printCalendar(c);  
  16.   
  17.     }  
  18.     public static void printCalendar(Calendar c ) {  
  19.         String[] mons = {"一月","二月","三月","四月",  
  20.                         "五月","六月","七月","八月",  
  21.                         "九月","十月","十一月","十二月"};  
  22.         int index = c.get(Calendar.MONTH);  
  23.         String[] weeks = {  
  24.                             "","星期日","星期一","星期二","星期三","星期四","星期五","星期六"  
  25.                             };  
  26.         int index2 = c.get(Calendar.DAY_OF_WEEK);  
  27.         System.out.println(c.get(Calendar.YEAR)+"年");  
  28.         System.out.println(mons[index]);  
  29.         System.out.println(c.get(Calendar.DAY_OF_MONTH)+"日");  
  30.         System.out.println(weeks[index2]);  
  31.     }  
  32.   
  33. }  

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;//不用强转
[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. public static void main(String[] args) {  
  2.     // TODO Auto-generated method stub  
  3.     double d = Math.ceil(13.2);  
  4.     System.out.println(d);  
  5.     double d1 = Math.floor(14.2);  
  6.     System.out.println(d1);  
  7.     int l = Math.round(12.4f);  
  8.     System.out.println(l);  
  9.     double d2 = Math.pow(23);  
  10.     System.out.println(d2);  
  11.       
  12.     Random r = new Random();  
  13.     for(int x=0;x<10;x++) {  
  14.         //int i = (int)(Math.random()*10+1);  
  15.         int i = r.nextInt(10)+1;  
  16.         System.out.println(i);  
  17.     }  
  18. }  

6、IO流

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

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

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

7、IO流常用基类:

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

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

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

8、字符流特点:

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

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. public static void main(String[] args) throws IOException {  
  2.     //创建一个FileWriter对象,该对象一被初始化就必须要明确被操作的文件  
  3.     //而且给文件会被创建到指定目录下,如果该目录下已有同名文件,将被覆盖  
  4.     //其实该步就是在明确数据要存放的目的地  
  5.     FileWriter fw = new FileWriter("1.txt");  
  6.     //调用write方法,将字符串写入到流中  
  7.     fw.write("abcde");  
  8.     //刷新流对象中的缓冲中的数据  
  9.     //将数据刷到目的地中  
  10.     //fw.flush();  
  11.       
  12.     //关闭流资源,但是关闭之前会刷新一次内部的缓冲中的数据  
  13.     //将数据刷到目的地中  
  14.     //和flush区别:flush刷新后,流可以继续使用,close刷新后,会关闭流资源  
  15.     fw.close();  
  16.   
  17. }  

9、IO流异常处理方式:
[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. public static void main(String[] args) {  
  2.     FileWriter fw = null;  
  3.     try {  
  4.         fw = new FileWriter("demo.txt");  
  5.         fw.write("abcd");  
  6.     }catch(IOException e) {  
  7.         System.out.println(e.toString());  
  8.     }  
  9.     finally{  
  10.         try {  
  11.             if(fw!=null)  
  12.                 fw.close();  
  13.         }catch(IOException e) {  
  14.             System.out.println(e.toString());  
  15.         }  
  16.     }  
  17.   
  18. }  

10、IO流中文件的续写:
[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. public static void main(String[] args) throws IOException{  
  2.     //传递一个true参数,代表不覆盖已有文件,并在已有文件的末尾处进行数据续写。  
  3.     FileWriter fw = new FileWriter("demo.txt",true);  
  4.     fw.write("nihao\r\nxiexie");  
  5.     fw.write('2');  
  6.     fw.close();  
  7.   
  8. }  

11、IO流文本文件读取方式(两种):
[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. public static void main(String[] args) throws IOException {  
  2.     //创建一个文件读取流兑现,和指定名称的文件想关联  
  3.     //要保证该文件是已经存在的,如果不存在,会发生异常FileNotFoundException  
  4.     FileReader fr = new FileReader("demo.txt");  
  5.     //调用读取流对对象的read方法  
  6.     //read();一次读一个字符,而且会自动往下读  
  7.     /*int ch = 0; 
  8.     while((ch=fr.read())!=-1) { 
  9.         System.out.print((char)ch); 
  10.     } 
  11.     */  
  12.     //读取方法二:通过字符数组进行读取  
  13.       
  14.     //定义一个字符数组,用于存储读到的字符  
  15.     //该read(char[])返回的是读到字符个数  
  16.     char[] buf = new char[1024];  
  17.     int num =0;  
  18.     while((num=fr.read(buf))!=-1) {  
  19.         System.out.print(new String(buf,0,num));  
  20.     }  
  21.     fr.close();  
  22.   
  23. }  

12、IO流拷贝文本文件:
[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. /* 
  2.  练习:从一个目录下c盘拷贝txt文件到另一个目录下d盘。 
  3.  1、在d盘创建一个文件,用于存储c盘文件中的数据 
  4.  2、定义读取流和c盘文件关联 
  5.  3、通过不断的读写完成数据存储 
  6.  4、关闭资源 
  7.  */  
  8. public class CopyTextDemo {  
  9.   
  10.     public static void main(String[] args) throws IOException/**/{  
  11.         copy_2();  
  12.   
  13.     }  
  14. //   方法一:从c盘读一个字符就往d盘中写入一个字符  
  15.     public static void copy_1() throws IOException {  
  16.         //创建目的地  
  17.         FileWriter fw = new FileWriter("d:\\1.txt",true);  
  18.         FileReader fr = new FileReader("c:\\1.txt");  
  19.         int ch = 0;  
  20.         while((ch =fr.read())!=-1) {  
  21.             fw.write(ch);  
  22.         }  
  23.         fw.close();  
  24.         fr.close();  
  25.           
  26.     }  
  27.     public static void copy_2() {  
  28.         FileWriter fw = null;  
  29.         FileReader fr = null;  
  30.         try {  
  31.             fw = new FileWriter("d:\\2.txt");  
  32.             fr = new FileReader("c:\\1.txt");  
  33.             char[] buf= new char[1024];  
  34.             int len = 0;  
  35.             while((len=fr.read(buf))!=-1) {  
  36.                 fw.write(buf,0,len);  
  37.             }  
  38.         }catch(IOException e) {  
  39.             //System.out.println(e.toString());  
  40.             throw new RuntimeException("读写失败");  
  41.         }  
  42.         finally {  
  43.             try {  
  44.                 if(fw!=null)  
  45.                     fw.close();  
  46.             }catch(IOException e) {  
  47.                 System.out.println(e.toString());  
  48.             }  
  49.             try{  
  50.                 if(fr!=null)   
  51.                     fr.close();  
  52.             }catch(IOException e) {  
  53.                 System.out.println(e.toString());  
  54.             }  
  55.         }  
  56.     }  
  57. }  


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