java学习--IO流(1)

本文详细介绍了 Java 中 System 类、Runtime 类、Date 类、字符流和字节流的基本使用,以及如何实现文件的创建、写入、读取、续写和复制操作。此外,还展示了如何通过异常处理确保 I/O 操作的稳定性和鲁棒性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

01)System

  1. /* 
  2.  * System:类中的方法和属性都是静态的。 
  3.  * out:标准输出,默认是控制台。 
  4.  * in:标准输入,默认是键盘。 
  5.  */  
  6. public class SystemDemo {  
  7.     public static void sop(Object obj){  
  8.         System.out.println(obj);  
  9.     }  
  10.     public static void main(String[] args) {  
  11.         method();  
  12.     }  
  13.     public static void method(){  
  14.         /* 
  15.          * 因为Properties是Hashtable的子类,也就是Map集合的一个子类对象。 
  16.          * 那么可以通过map的方法取出该集合中的元素。 
  17.          * 该集合中存储都是字符串。没有泛型定义。 
  18.          */  
  19.         Properties prop = System.getProperties();  
  20.           
  21.         /*在系统中自定义一些特有信息。 
  22.          * 通过System.setProperty(T, V)方法设置。 
  23.         //System.setProperty("hah", "hhahhah"); 
  24.         */  
  25.           
  26.         /* 
  27.          * 获取指定属性信息。 
  28.          * 通过System.getProperty(T)方法。 
  29.          */  
  30.         String value = System.getProperty("os.name");//获取本机操作系统。  
  31.         sop("value=" + value);  
  32.           
  33.         /* 
  34.          * 如何在jvm启动时,动态加载一些属性信息? 
  35.          * 通过命令行方式:java -Dxxx=qqq 类名      这个格式。xxx对应键,qqq对应值。 
  36.          */  
  37.           
  38.         /* 
  39.         //获取所有属性信息。 
  40.         for (Object obj : prop.keySet()){ 
  41.             String value = (String)prop.get(obj); 
  42.             sop(obj + " = " + value); 
  43.         } 
  44.         */  
  45.     }  
  46. }  
运行结果如下图所示:


02)Runtime

  1. /* 
  2.  * Runtime对象。 
  3.  * 该类并没有提供构造函数:说明不可以new对象。那么会想到该类中都是些静态方法。 
  4.  * 发现该类中还有非静态方法:说明该类肯定会提供了方法获取本类对象。而且该方法是静态的,并返回值类型是本类类型。 
  5.  * 该方法是static Runtime getRuntime(); 
  6.  */  
  7. public class RuntimeDemo {  
  8.     public static void main(String[] args) throws IOException, InterruptedException{  
  9.         method();  
  10.     }  
  11.     public static void method() throws IOException, InterruptedException{  
  12.         Runtime r = Runtime.getRuntime();//获取本类对象。   
  13.         Process p = r.exec("QQProtect.exe");//可执行程序放在:Windows系统文件System32文件之内。  
  14.         //如果可执行程序不存在,运行后就会显示“Cannot run program”的提示信息,“找不到程序”。  
  15.           
  16.         Thread.sleep(1000);//睡眠1000毫秒。  
  17.         p.destroy();//强制终止此 Process 对象表示的子进程。  
  18.     }  
  19. }  
运行结果如下图所示:


03)Date

  1. /* 
  2.  * 打印日期。 
  3.  */  
  4. public class DateDemo {  
  5.     public static void sop(Object obj){  
  6.         System.out.println(obj);  
  7.     }  
  8.     public static void main(String[] args) {  
  9.         method();  
  10.     }  
  11.     public static void method(){  
  12.         Date d = new Date();  
  13.         sop("本地日期格式:" + d);//打印时间。  
  14.         //将模式封装到SimpleDateformat对象中。  
  15.         SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日E  kk:mm:ss");  
  16.         //调用format方法让模式格式化指定Date对象。  
  17.         sop("格式化后格式:" + sdf.format(d));  
  18.     }  
  19. }  
运行结果如下图所示:


07)IO流(FileWriter)

  1. /* 
  2.  * 字符流和字节流: 
  3.  * 字节流两个基类:InputStream OutputStream 
  4.  *  
  5.  * 字符流两个基类:Reader Writer 
  6.  * 字符流的特点:IO流是用于操作数据的,数据的最常见体现形式是:文件。 
  7.  *  
  8.  * 以操作文件为主来演示。 
  9.  * 需求:在硬盘上,创建一个文件并写入一些文字数据。 
  10.  * 找到一个专门用于操作文件的Writer子类对象。FileWriter。  后缀名是父类名。 前缀名是该流对象的功能。 
  11.  *  
  12.  * void write(String str):写入字符串。 
  13.  * abstract void flush():刷新该流的缓冲。IOException - 如果发生 I/O 错误 
  14.  * abstract  void close():关闭此流,但要先刷新它。 
  15.  */  
  16. public class FileWriterDemo {  
  17.     public static void main(String[] args) throws IOException{  
  18.         method();  
  19.     }  
  20.     public static void method() throws IOException{  
  21.         /* 
  22.          * 1)创建一个FileWriter对象。该对象一被初始化就必须要明确被操作的文件。 
  23.          *    而且该文件会被创建到指定目录下。如果该目录下已有同名文件,将被覆盖。 
  24.          *    其实该步就是在明确数据要存放的目的地。 
  25.          */  
  26.         FileWriter fw = new FileWriter("WriterDemo.txt");  
  27.         //2)调用write方法,将字符串写入到流中。  
  28.         fw.write("aaaaa");  
  29.         //3)刷新流对象缓冲区的数据,刷新到目的地中。  
  30. //      fw.flush();  
  31.         fw.write("bbbbb");  
  32.         fw.write("ccccc");  
  33.         fw.write("ddddd");  
  34.         /* 
  35.          * 04)(close)关闭流资源,但是关闭之前会刷新一次内部的缓冲中的数据。 
  36.          *     将数据刷到目的地中。 
  37.          *     和flush区别:flush刷新后,流可以继续使用,close刷新后,会将流关闭。 
  38.          *     关闭此流,但要先刷新它。在关闭该流之后,再调用 write() 或 flush() 将导致抛出 IOException。 
  39.          *     关闭以前关闭的流无效。  
  40.          */  
  41.         fw.close();  
  42. //      fw.write("eeeee");//异常IOException。  
  43.     }  
  44. }  
运行结果如下图所示:


08)IO异常的处理方式。

  1. /* 
  2.  * try{ 
  3.  * }catch(){ 
  4.  * }finally{ 
  5.  * } 
  6.  */  
  7. public class FileWriterDemo2 {  
  8.     public static void main(String[] args) {  
  9.         method();  
  10.     }  
  11.     public static void method(){  
  12.         FileWriter fw = null;//在外部建立引用。  
  13.         try{  
  14.             fw = new FileWriter("WriterDemo2.txt");//在内部进行初始化。  
  15.             fw.write("WriterDemo2.txt");  
  16.         }catch(IOException e){  
  17.             System.out.println("catch:" + e.toString());  
  18.         }finally{  
  19.             try{  
  20.                 if (fw != null)//如果文件不等于与NULL。  
  21.                     fw.close();//执行关闭流操作。  
  22.             }catch(IOException e){  
  23.                 System.out.println(e.toString());//打印异常信息。  
  24.             }  
  25.         }  
  26.     }  
  27. }  
运行结果如下图所示:


09)文件的续写

  1. /* 
  2.  * FileWriter(String fileName, boolean append): 
  3.  *      根据给定的文件名以及指示是否附加写入数据的 boolean 值来构造 FileWriter 对象。 
  4.  *  
  5.  * 显示对已有文件的数据的续写。 
  6.  */  
  7. public class FileWriterDemo3 {  
  8.     public static void main(String[] args) {  
  9.         method();  
  10.     }  
  11.     public static void method(){  
  12.           
  13.         FileWriter fw = null;  
  14.         try{  
  15.             //传递一个(true)参数,代表不覆盖已有文件。并在已有文件的末尾处进行续写。  
  16.             fw = new FileWriter("WriterDemo.txt"true);  
  17.             fw.write("\r\n换行续写!");  
  18.         }catch(IOException e){  
  19.             System.out.println("catch:" + e.toString());  
  20.         }finally{  
  21.             try{  
  22.                 if (fw != null)  
  23.                     fw.close();  
  24.             }catch(IOException e){  
  25.                 System.out.println(e.toString());  
  26.             }  
  27.         }  
  28.     }  
  29. }  
运行结果如下图所示:


10)文本文件读取方式(一)

  1. /* 
  2.  * 第一种方式:调用读取流对象的read方法。 
  3.  * int read():读取单个字符。 
  4.  *          在字符可用、发生 I/O 错误或者已到达流的末尾前,此方法一直阻塞。 
  5.  *          作为整数读取的字符,范围在 0 到 65535 之间 (0x00-0xffff),如果已到达流的末尾,则返回 -1  
  6.  */  
  7. public class FileReaderDemo {  
  8.     public static void sop(Object obj){  
  9.         System.out.print(obj + " ");  
  10.     }  
  11.     public static void main(String[] args) {  
  12.         method();  
  13.     }  
  14.     public static void method(){  
  15.         FileReader fr = null;  
  16.         try{  
  17.             //创建一个文件读取流对象,和指定名称的文件相关联。  
  18.             //要保证文件是已经存在的,如果不存在,会发生异常:FileNotFoundException。  
  19.             fr = new FileReader("WriterDemo2.txt");  
  20.             int ch = 0;//设置读取下标从0开始。  
  21.             while((ch = fr.read()) != -1)//只要有字符,就读取。  
  22.                 sop((char)ch);//read():一次读一个字符,会自动往下读。  
  23.         }catch(IOException e){  
  24.             System.out.println("catch:" + e.toString());  
  25.         }finally{  
  26.             try{  
  27.                 if (fr != null)  
  28.                     fr.close();  
  29.             }catch(IOException e){  
  30.                 System.out.println("文件不存在异常:" + e.getMessage());  
  31.             }  
  32.         }  
  33.     }  
  34. }  

11)文本文件读取方式(二)

  1. /* 
  2.  * 第二种方式:通过字符数组进行读取。 
  3.  */  
  4. public class FileReaderDemo2 {  
  5.     public static void sop(Object obj){  
  6.         System.out.println(obj);  
  7.     }  
  8.     public static void main(String[] args)  throws IOException{  
  9.         method();  
  10.     }  
  11.     public static void method(){  
  12.         FileReader fr = null;  
  13.         try{  
  14.             //创建一个文件读取流对象,和指定名称的文件相关联。  
  15.             //要保证文件是已经存在的,如果不存在,会发生异常:FileNotFoundException。  
  16.             fr = new FileReader("WriterDemo2.txt");  
  17.             char[] buf = new char[1024];//常见定义长度。 
  18.            //定义一个字符数组。用于存储读到字符。
               //该read(char[])返回的是读到字符个数。
  19.             int len = 0;  
  20.             while((len = fr.read(buf)) != -1)  
  21.                 sop("第二种方式:通过字符数组进行读取。\n" + new String(buf, 0, len));  
  22.         }catch(IOException e){  
  23.             System.out.println("catch:" + e.toString());  
  24.         }finally{  
  25.             try{  
  26.                 if (fr != null)  
  27.                     fr.close();  
  28.             }catch(IOException e){  
  29.                 System.out.println("文件不存在异常:" + e.getMessage());  
  30.             }  
  31.         }  
  32.     }  
  33. }  
运行结果如下图所示:


12)练习

  1. /* 
  2.  * 读取一个.java文件,并打印在控制台上。 
  3.  */  
  4. public class FileReaderTest {  
  5.     public static void sop(Object obj){  
  6.         System.out.print(obj);  
  7.     }  
  8.     public static void main(String[] args) {  
  9.         method();  
  10.     }  
  11.     public static void method(){  
  12.         FileReader fr = null;  
  13.         try{  
  14.             fr = new FileReader("RuntimeDemo.java");  
  15.             char[] buf = new char[1024];  
  16.             int len = 0;  
  17.             while((len = fr.read(buf)) != -1)  
  18.                 sop(new String(buf, 0, len));  
  19.         }catch(IOException e){  
  20.             throw new RuntimeException("读取失败");  
  21.         }finally{  
  22.             if (fr != null){  
  23.                 try{  
  24.                         fr.close();  
  25.                 }catch(IOException e){  
  26.                     System.out.println("文件不存在异常:" + e.getMessage());  
  27.                 }  
  28.             }  
  29.         }  
  30.     }  
  31. }  
运行结果如下图所示(这是我RuntimeDemo.java文件中的内容):


13)拷贝文本文件

  1. /* 
  2.  *  
  3.  * 复制的原理: 
  4.  * 其实就是将C盘下的文件数据存储到E盘的一个文件中。 
  5.  *  
  6.  * 步骤: 
  7.  * 1:在E盘(目标)创建一个文件。用于存储C盘(源文件)文件中的数据。 
  8.  * 2:定义读(FileReader)取(FileWriter)流和C盘(源文件)文件关联。 
  9.  * 3:通过不断的读(read)写(write)完成数据存储。 
  10.  * 4:关闭(close)资源。 
  11.  */  
  12. public class CopyTestDemo {  
  13.     public static void main(String[] args) {  
  14.         method();  
  15.     }  
  16.       
  17.     public static void method(){  
  18.         FileWriter fw = null;  
  19.         FileReader fr = null;  
  20.         try{  
  21.             fw = new FileWriter("E:\\RuntimeDemo_copy.txt");  
  22.             fr = new FileReader("RuntimeDemo.java");  
  23.             char[] ch = new char[1024];  
  24.             for (int len = 0;(len = fr.read(ch)) != -1;)  
  25.                 fw.write(ch, 0, len);  
  26.         }catch(IOException e){  
  27.             throw new RuntimeException("读写失败");  
  28.         }finally{  
  29.             if (fr != null){  
  30.                 try{  
  31.                         fr.close();  
  32.                 }catch(IOException e){  
  33.                 }  
  34.             }  
  35.             if (fw != null){  
  36.                 try{  
  37.                     fw.close();  
  38.                 }catch(IOException e){  
  39.                 }  
  40.             }  
  41.         }  
  42.     }  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值