IO輸入/輸出流的簡單總結

 

  1.  
  2. /**  
  3.  * 輸入/輸出流  
  4.  * InputSream:  
  5.  *      ObjectInputStream       //對象流  
  6.  *      PipedInputStream        //管道流  
  7.  *      FileInputStream         //文件流  
  8.  *      ByteArrayInputStream    //字節數組流  
  9.  *      FilterInputStream       //過濾流  
  10.  *          DataInputStream     //數據輸入流  
  11.  *          BufferedInputSteam  //將數據寫入緩衝區  
  12.  *   
  13.  *   
  14.  * 字符流的層次結構  
  15.  * Reader:  
  16.  *      BufferedReader          //用字符流寫入緩衝區  
  17.  *      InputStreamReader       //用於將字節碼 與 字符碼 轉換  
  18.  *      FileReader              //用於字符文件的輸入  
  19.  *   
  20.  * 文件處理:  
  21.  *  創建文件對象:  
  22.  *      1、File(String path)  
  23.  *      2、File(String path, String name)  
  24.  *      3、File(File dir, String name)  
  25.  *    
  26.  *  創建文件 和 目錄  
  27.  *      1、public boolean exists()       //文件、目錄 是否存在  
  28.  *        
  29.  *      2、public boolean isFile()  
  30.  *         public boolean isDirectory() //判斷是文件還是目錄  
  31.  *        
  32.  *      3、public String getName()  
  33.  *         public String getPaht()      //獲取文件名 或 目錄  
  34.  *   
  35.  *      4、public long length()          //長度  
  36.  *    
  37.  *      5、public boolean canRead()      //文件是否 可讀  
  38.  *         public boolean canWrite()    //文件是否可寫  
  39.  *    
  40.  *      6、public String[] list()        //列出目錄中文件  
  41.  *    
  42.  *      7、public boolean equals()       //比較文件 或 目錄  
  43.  *    
  44.  *  文件目錄操作  
  45.  *      1、public boolean renameTo(File newFile())       //重命名  
  46.  *    
  47.  *      2、public void delete()                          //刪除  
  48.  *    
  49.  *      3、public boolean mkdir()                        //創建目錄    
  50.  *    
  51.  *    
  52.  *8.2.2文件流  
  53.  *  
  54.  *  1、字節文件流讀取文件  
  55.  *  
  56.  *      FileIputStream  
  57.  *          (1)FileInputStream(String filename)         //指定的文件名,包括路徑  
  58.  *      (2)FileInputStream(File file)                   //指定文件對象  
  59.  *      (3) FileDescriptor( FileDescritor fdObj)        //指定文件描述符  
  60.  *  
  61.  *      從文件輸入流中讀取字節  
  62.  *          int read()  
  63.  *          int rean(byte[] b)                          //讀到的數據直接寫入字節數組b中  
  64.  *          int read(byte[] b, int off, int len)  
  65.  *  
  66.  *示例:  
  67.  *  public static void main(String[] args){  
  68.  *      byte[] buffer = new byte[2056];  
  69.  *      String str;  
  70.  *      try{  
  71.  *  
  72.  *      }  
  73.  *  }  
  74.  *  
  75.  */ 
  76.  
  77.  
  78. /***字節文件輸入流讀取文件***/ 
  79. import java.io.FileInputStream;  
  80. import java.io.FileOutputStream;  
  81.  
  82. import javax.swing.JOptionPane;  
  83. class Example{  
  84.     public static void main(String[] args){  
  85.         byte[] buffer = new byte[2056];  
  86.         String str;  
  87.         try{  
  88.             File file = new File("d:/jtest/test.dat");  
  89.             FileInputStream fileInput = new FileInputStream(file);  
  90.             //將數據讀到 字節數組buffer中  
  91.             int bytes = fileInput.read(buffer, 02056);  
  92.             //用字節數組buffer中的數據構建字符串  
  93.             str = new String(buffer, 0, bytes);  
  94.         }  
  95.         /***省略程序**/ 
  96.           
  97.           
  98.         /***省略程序**/ 
  99.     }  
  100. }  
  101.  
  102.  
  103. /***字節文件輸出流寫入文件***/ 
  104.  
  105. /**  
  106.  * 把字節發送到文件輸出流  
  107.  *      write(int b)        //將指定字節寫入此文件輸出流  
  108.  *      write(byte[] b)     //將b.length個字節從指定字節數組寫入此文件輸出流中  
  109.  *      write(byte[] b, int off, int len)   //將指定字節數組從偏移量off開始的len個字節寫入  
  110.  * */ 
  111.  
  112. 示例:  
  113. import javax.swing.JOptionPane;  
  114. import java.io.*;  
  115. class FileRW{  
  116.     int bytes;  
  117.     byte buffer[] = new byte[65560];  
  118.     FileInputStream fileInput;  
  119.     FileOutputStream fileOutput;  
  120.     /**  
  121.      * 省略部分程序  
  122.      * */ 
  123.     //將圖像文件讀取到字節數組中  
  124.     try{  
  125.         File file = new File("a.jpg");  
  126.         fileInput = new FileInputStream(file);  
  127.         //將文件a.jpg的數據讀取到字節數組buffer中,  
  128.         bytes = fileInput.read(buffer, 0 ,65560);  
  129.           
  130.     }  
  131.     //將字節數組的數據寫入到文件b.jpg中  
  132.     try{  
  133.         fileOutput = new FileOutputStream("b.jpg");  
  134.         fileOutput.write(buffer, 0, bytes);  
  135.     }  
  136. }  
  137.  
  138.  
  139. /***字符文件流讀寫文本文件***/ 
  140.  
  141. (1)FileReader和FileWriter  
  142.     FileReader(String filename)  
  143.     FileReader(File file)  
  144.     FileReader(FileDescriptor fdObj)  
  145.       
  146.     將字符輸入或輸出到緩衝區  
  147.     BufferedReader(Reader in)  
  148.     BufferedWriter(Reader out)  
  149. (2)用字符流進行讀寫操作方法  
  150.       
  151.     //從輸入流中按行讀取字符的方法  
  152.     String readLine()  
  153.     //向輸出流寫入多個字符的方法  
  154.     write(String s, int off, int len)  
  155.     //刷新緩衝區  
  156.     flush()  
  157.     //關閉流  
  158.     close()  
  159.  
  160. /****************/ 
  161. 示例:  
  162.  
  163. FileReader r_file;  
  164. FileWriter w_file;  
  165. BufferedReader buf_reader;  
  166. BufferedWriter buf_writer;  
  167. /**  
  168.  * 省略程序  
  169.  * */ 
  170. //讀取文件  
  171. try{  
  172.     File f = new File("D:/jtext/""a.txt");  
  173.     r_file = new FileReader(f);  
  174.     buf_reader = new BufferedReader(r_file);  
  175. }  
  176. catch(IOException ef){System.out.println(ef);}  
  177. try{  
  178.     while((s=buf_reader.readLine()) != null){  
  179.         txt.append(s + '\n');  
  180.     }  
  181. }  
  182. catch(IOException ef){Syte........}  
  183. //寫入文件  
  184. try{  
  185.     w_file = new FileWriter("b.txt");  
  186.     buf_writer = new BufferedWriter(w_file);  
  187.     String str = txt.getText();  
  188.       
  189.     buf_writer.write(str, 0, str.length());  
  190.     buf_writer.flush();  
  191. }  
  192.  
  193.  
  194. /****隨即存取文件流***/ 
  195. *前面學的都是順序訪問的  
  196. *隨機存取可以訪問任意位置的的數據  
  197. (1)RandomAccessFile(String filename, String mode)  
  198. (2)RandomAccessFile(File file, String mode)  
  199.  
  200. 示例:  
  201. try{  
  202.     RandomAccessFile f = new RandomAccessFile("a.txt""rw");  
  203.     f.writeBytes("zhang si ming");  
  204.     f.close();  
  205. }  
  206.  
  207. /***本地可執行文件**/ 
  208.  
  209.  
  210. Runtime 類對象有幾個常用方法:  
  211.  
  212. (1)exit(int status)  
  213. //通過啓動虛擬機的關閉序列,終止當前正在運行的Java虛擬機  
  214.  
  215. (2)getRuntime()  
  216. //返回與當前Java應用程序相關的運行時對象,使用該方法創建Runtime類對象  
  217.     Runtime rt = Runtime.getRuntime()  
  218. (3)exec(String command)  
  219. //調用該方法可以再單獨的進程中運行由字符串命令指定的本地機上的可執行文件  
  220. (4)gc()  
  221. //運行垃圾回收器  
  222.  
  223. 示例:  
  224. /***調用windows系統自帶的計算器***/ 
  225. try{  
  226.     Runtime rt = Runtime.getRuntime();  
  227.     rt.exec("C:/windows/system32/calc.exe");  
  228. }  
  229.  
  230.  
  231. /***數據流與對象流****/ 
  232.  
  233. 構造方法:  
  234. public DataInputStream(InputStream in);  
  235. public DataOutputStream(OutputStream out);  
  236.  
  237. 示例:  
  238. int bytes, f_length;  
  239. byte[] buffer;  
  240.  
  241. FileInputStream fileInput;  
  242. FileOutputStream fileOutput;  
  243.  
  244. DataInputStream dataInput;  
  245. DataOutputStream dataOutput;  
  246.  
  247. File file = new File("ABC.mp3");  
  248. //獲取文件長度,設置數組容量  
  249. f_length = (int)file.length();  
  250. buffer = new byte[f_length];  
  251.  
  252. /***讀取聲音文件***/ 
  253. try{  
  254.     fileInput = new FileInputStream("ABC.mp3");  
  255.     dataInput = new DataInputStream(fileInput);  
  256.     bytes = dataInput.read(buffer);  
  257. }  
  258.  
  259. /******寫入到新文件*******/ 
  260. try{  
  261.     fileOutput = new FileOutputStream("新文件.mp3");  
  262.     dataOutput = new DataOutputStream(fileOutput);  
  263.     dataOutput.write(buffer, 0, bytes);  
  264. }  
  265.  
  266.  
  267. /***********對象流****************/ 
  268. 構造方法:  
  269. ObjectInputStream(InputStream in);  
  270. ObjectOutputStream(OutputStream out);  
  271. //創建數據輸入流  
  272.     FileInputStream file_in = new FileInputStream("sample.dat");  
  273.     ObjectInputStream object_in = new ObjectInputStream(file_in);  
  274.  
  275.  
  276. 常用方法:  
  277. readObject()  
  278. writeObject()  
  279.  
  280. 對象序列化  
  281. Serializable接口  
  282.  
  283.  
  284.  
  285.  
  286.  
  287.  
  288.  
  289.  
  290.  
  291.  
  292.  
  293.  
  294.  
  295.  
  296.  
  297.  
  298.  
  299.  
  300.  

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章