Java学习系列(十一)Java面向对象之I/O流(下)

来源:互联网 发布:ubuntu和debian哪个强 编辑:程序博客网 时间:2024/05/16 09:07

今天接着昨天的IO流讲,内容可能会比较多。

DataInputStream与DataOutputStream
它们是建立在已有的IO的基础上的两个特殊的过滤流。规律:它们只是增加了一些特定的方法读取特定的数据。

举例说明1:

Java代码  收藏代码
  1. public class Test {  
  2.     public static void main(String[] args) {  
  3.         DataOutputStream dos = null;  
  4.         try {  
  5.             dos = new DataOutputStream(new FileOutputStream("F:/price.txt"));  
  6.             dos.writeDouble(243.21);  
  7.         } catch (IOException e) {  
  8.             e.printStackTrace();  
  9.         } finally {  
  10.             try {  
  11.                 dos.close();  
  12.             } catch (IOException e2) {  
  13.                 e2.printStackTrace();  
  14.             }  
  15.         }  
  16.     }  
  17. }  

举例说明2:

Java代码  收藏代码
  1. public class Test {  
  2.     public static void main(String[] args) {  
  3.         DataInputStream dis = null;  
  4.         try {  
  5.             dis = new DataInputStream(new FileInputStream("F:/price.txt"));  
  6.             System.out.println(dis.readDouble());  
  7.         } catch (IOException e) {  
  8.             e.printStackTrace();  
  9.         } finally {  
  10.             try {  
  11.                 dis.close();  
  12.             } catch (IOException e2) {  
  13.                 e2.printStackTrace();  
  14.             }  
  15.         }  
  16.     }  
  17. }  

 

 

节点流(System.in是读取键盘输入,可以换成new FileInputStream("f:/1.txt")读文件,也可以换成读网络(Socket)--以后会讲)包装成过滤流编程:

Java代码  收藏代码
  1. public class Test {  
  2.     public static void main(String[] args) {  
  3.         BufferedReader br = null;  
  4.         try {  
  5.             br = new BufferedReader(new InputStreamReader(System.in));  
  6.             String line = null;  
  7.             while ((line = br.readLine()) != null) {  
  8.                 System.out.println(line);  
  9.             }  
  10.   
  11.   
  12.         } catch (IOException e) {  
  13.             e.printStackTrace();  
  14.         }  
  15.     }  
  16. }  

 

 

Java虚拟机读取其他进程的数据
Java如何启动其他进程:Runtime实例.exec(String command)
举例说明:

 

 

Java代码  收藏代码
  1. public class Test {  
  2.     public static void main(String[] args) {  
  3.         Process process = null;  
  4.         BufferedReader br = null;  
  5.         try {  
  6.             process = Runtime.getRuntime().exec("javac.exe");  
  7.             br = new BufferedReader(new InputStreamReader(  
  8.                     process.getErrorStream()));  
  9.             String line = null;  
  10.             System.out.println("编译出错,错误信息如下~~~~~");  
  11.             while ((line = br.readLine()) != null) {  
  12.                 System.out.println(line);  
  13.             }  
  14.         } catch (IOException e) {  
  15.             e.printStackTrace();  
  16.         } finally {  
  17.             try {  
  18.                 br.close();  
  19.             } catch (IOException e2) {  
  20.                 e2.printStackTrace();  
  21.             }  
  22.         }  
  23.     }  
  24. }  

 

 

 

RandomAccessFile ---随机(任意)访问文件。 --创建时需要指定r/w模式。
Random --想访问文件的哪个点,就访问文件的哪个点(任意)。
特征:既可读、又可写、还可追加,不会覆盖原有文件内容。但它只能访问文件。
举例说明1:

 

Java代码  收藏代码
  1. public class Test {  
  2.     public static void main(String[] args) throws IOException {  
  3.         RandomAccessFile raf = new RandomAccessFile("f:/1.txt""rw");  
  4.         byte[] buff = new byte[1024];  
  5.         int hasRead = -1;  
  6.         while ((hasRead = raf.read(buff)) > 0) {  
  7.             System.out.println(new String(buff, 0, hasRead));  
  8.         }  
  9.         raf.close();  
  10.     }  
  11. }  


举例说明2:

 

 

Java代码  收藏代码
  1. /** 
  2.  * @author lhy 
  3.  * @description 向文件中的指定位置插入内容 
  4.  */  
  5. public class Test {  
  6.     public static void main(String[] args) {  
  7.         RandomAccessFile raf = null;  
  8.         try {  
  9.             raf = new RandomAccessFile("f:/1.txt""rw");  
  10.             insertContent(raf, 100"Java面向对象之I/O流之RandomAccessFile的使用");  
  11.         } catch (IOException e) {  
  12.             e.printStackTrace();  
  13.         } finally {  
  14.             try {  
  15.                 raf.close();  
  16.             } catch (IOException e) {  
  17.                 e.printStackTrace();  
  18.             }  
  19.         }  
  20.     }  
  21.   
  22.     public static void insertContent(RandomAccessFile raf, int pos,  
  23.             String content) {  
  24.         ByteArrayOutputStream bos = null;  
  25.         try {  
  26.             bos = new ByteArrayOutputStream();  
  27.             raf.seek(pos);// 把记录指针移到要插入的地方  
  28.             byte[] buff = new byte[1024];  
  29.             int hasRead = -1;  
  30.             // 将raf从pos位置开始、直到结尾所有的内容  
  31.             while ((hasRead = raf.read(buff)) > 0) {  
  32.                 bos.write(buff, 0, hasRead);// 将读取的数据(从pos位置开始)放入bos中  
  33.             }  
  34.             raf.seek(pos);// 再次将记录指针移到要插入的地方  
  35.             raf.write(content.getBytes()); // 写入要插入的内容  
  36.             raf.write(bos.toByteArray()); // 写入之前保存的内容  
  37.   
  38.         } catch (IOException e) {  
  39.             e.printStackTrace();  
  40.         } finally {  
  41.             try {  
  42.                 bos.close();  
  43.             } catch (IOException e) {  
  44.                 e.printStackTrace();  
  45.             }  
  46.         }  
  47.     }  
  48. }  

 

 

结束语

有关Java中的IO流类比较多,而且方法大同小异,大家有空可以多查查API文档。今天就讲到这,明天开始讲Java面向对象之序列化机制及版本。

0 0
原创粉丝点击