javaIO流

IO

文件--基本概念

文件是數據源(保存數據的地方)的一種,比如大word文檔、jpg文件、MP4文件...都是文件。文件最主要的作用就是保存數據,它既可以保存一張圖片,也可以保存視頻、聲音...等

 

文件流--基本概念

文件在程序中是以流的形式來操作的。



流:數據在數據源(文件)和程序(內存)之間經歷的路徑

輸入流:數據從數據源(文件)到程序(內存)的路徑

輸出流:數據從程序(內存)到數據源(文件)的路徑


如何判斷是輸入流、輸出流?

以內存爲參照,如果數據流向內存流動,則是輸入流;反之,則是輸出流。


文件流--分類



java流分爲兩種流

1、字節流:可以用於讀寫二進制文件及任何類型文件

2、字符流:可以用於讀寫文本文件,不能操作二進制文件


實例1.File類的使用

  1. /** 
  2.  * File類的基本用法 
  3.  */  
  4. package com.io;  
  5.   
  6. import java.io.*;  
  7.   
  8. public class IO1 {  
  9.     public static void main(String[] args) {  
  10.         //創建一個文件對象  
  11.         File f1 = new File("C:\\in.txt");  
  12.           
  13.         //得到文件路徑  
  14.         System.out.println("文件路徑:" + f1.getAbsolutePath());  
  15.           
  16.         //得到文件的大小,字節數  
  17.         System.out.println("文件大小:" + f1.length());  
  18.           
  19.         //是否可讀  
  20.         System.out.println("可讀" + f1.canRead());  
  21.           
  22.         //創建文件和創建文件夾  
  23.         File f2 = new File("C:\\in2.txt");  
  24.         //判斷文件是否存在  
  25.         if(!f2.exists()){  
  26.             //創建一個文件  
  27.             try {  
  28.                 f2.createNewFile();  
  29.             } catch (IOException e) {  
  30.                 e.printStackTrace();  
  31.             }  
  32.             System.out.println("文件創建成功");  
  33.         } else {  
  34.             System.out.println("文件已存在,無法創建");  
  35.         }  
  36.           
  37.         File f3 = new File("C:\\file1");  
  38.         //判斷文件是否爲文件夾  
  39.         if(f3.isDirectory()){  
  40.             System.out.println("文件夾已存在");  
  41.         } else {  
  42.             //創建文件夾  
  43.             f3.mkdir();  
  44.             System.out.println("文件夾已創建");  
  45.         }  
  46.           
  47.         //列出一個文件夾下面的所有文件  
  48.         File f4 = new File("C:\\");  
  49.           
  50.         if(f4.isDirectory()){  
  51.             //獲取文件數組  
  52.             File[] lists = f4.listFiles();  
  53.             for(int i = 0; i < lists.length; i++){  
  54.                 System.out.println("文件名:" + lists[i].getName());  
  55.             }  
  56.               
  57.         }  
  58.           
  59.           
  60.     }  
  61.   
  62. }  


實例2.文件字節流的使用

  1. /** 
  2.  * FileInputStream類的使用 
  3.  */  
  4. package com.io;  
  5. import java.io.*;  
  6. public class IO2 {  
  7.   
  8.       
  9.     public static void main(String[] args) {  
  10.         //創建一個文件對象  
  11.         File f = new File("C:\\in.txt");  
  12.         FileInputStream fis = null;  
  13.         //File無讀寫能力,所以需要使用InputStream進行讀入  
  14.         try {  
  15.             fis = new FileInputStream(f);  
  16.               
  17.             //定義一個字節數組,相當於緩存  
  18.             byte[] bytes = new byte[1024];  
  19.             //得到實際讀取到的字節數  
  20.             int n = 0;  
  21.             //循環讀取  
  22.             while((n = fis.read(bytes)) != -1){  
  23.                 //把字節轉換成String  
  24.                 String s = new String(bytes, 0, n);  
  25.                 System.out.println(s);  
  26.             }  
  27.               
  28.         } catch (Exception e) {  
  29.             e.printStackTrace();  
  30.         } finally {  
  31.             //關閉文件流--必須放這裏  
  32.             try {  
  33.                 fis.close();  
  34.             } catch (IOException e) {  
  35.                 e.printStackTrace();  
  36.             }  
  37.         }  
  38.     }  
  39.   
  40. }  


  1. /** 
  2.  * FileOutputStream類的使用 
  3.  */  
  4. package com.io;  
  5. import java.io.*;  
  6. public class IO3 {  
  7.     public static void main(String[] args) {  
  8.         //創建文件對象  
  9.         File f = new File("C:\\out2.txt");  
  10.           
  11.         //字節輸出流  
  12.         FileOutputStream fos = null;  
  13.           
  14.         try {  
  15.             fos = new FileOutputStream(f);  
  16.             String s = "hello,world\r\n";  
  17.             String s2 = "hello,java\r\n";  
  18.             //定義字節數組  
  19.             //byte[] bytes = new byte[1024];  
  20.               
  21.             fos.write(s.getBytes());  
  22.             fos.write(s2.getBytes());  
  23.         } catch (Exception e) {  
  24.             e.printStackTrace();  
  25.         } finally {  
  26.             try {  
  27.                 fos.close();  
  28.             } catch (IOException e) {  
  29.                 e.printStackTrace();  
  30.             }  
  31.         }  
  32.   
  33.     }  
  34.   
  35. }  

  1. /** 
  2.  * 圖片拷貝 
  3.  */  
  4. package com.io;  
  5. import java.io.*;  
  6. public class IO4 {  
  7.     public static void main(String[] args) {  
  8.         //先把圖片讀入到內存 -> 寫入到某個文件  
  9.         //因爲是二進制文件,因此只能用字節流完成  
  10.           
  11.         //輸入流  
  12.         FileInputStream fis = null;  
  13.         //輸出流  
  14.         FileOutputStream fos = null;  
  15.          try {  
  16.             fis = new FileInputStream(new File("C:\\image01.jpg"));  
  17.             fos = new FileOutputStream(new File("C:\\image01_copy.jpg"));  
  18.               
  19.             byte[] buf = new byte[1024];  
  20.             //記錄實際讀取到的字節  
  21.             int n = 0;  
  22.             //循環讀取  
  23.             while((n = fis.read(buf)) != -1){  
  24.                 //輸出到指定文件  
  25.                 fos.write(buf);  
  26.             }  
  27.         } catch (Exception e) {  
  28.             e.printStackTrace();  
  29.         } finally {  
  30.             //關閉打開的文件流  
  31.             try {  
  32.                 fis.close();  
  33.                 fos.close();  
  34.             } catch (IOException e) {  
  35.                 e.printStackTrace();  
  36.             }  
  37.         }  
  38.   
  39.     }  
  40.   
  41. }  


實例3.文件字節流

  1. /** 
  2.  * 字符流 
  3.  */  
  4. package com.io;  
  5. import java.io.*;  
  6. public class IO5 {  
  7.     public static void main(String[] args) {  
  8.         //文件讀入字符流  
  9.         FileReader fr = null;  
  10.         //文件寫出字符流  
  11.         FileWriter fw = null;  
  12.           
  13.         try {  
  14.             //創建文件讀入字符流對象  
  15.             fr = new FileReader(new File("C:\\test.txt"));  
  16.             //創建文件寫出字符流對象  
  17.             fw = new FileWriter(new File("C:\\test_copy.txt"));  
  18.               
  19.             //讀入到內存  
  20.             //緩存char數組  
  21.             char[] c = new char[1024];  
  22.             //讀入實際大小  
  23.             int n = 0;  
  24.             while((n = fr.read(c)) != -1){  
  25.                 fw.write(c, 0, n);  
  26.             }  
  27.               
  28.         } catch (Exception e) {  
  29.             e.printStackTrace();  
  30.         } finally {  
  31.             //關閉文件流  
  32.             try {  
  33.                 fr.close();  
  34.                 fw.close();  
  35.             } catch (IOException e) {  
  36.                 e.printStackTrace();  
  37.             }  
  38.         }  
  39.     }  
  40.   
  41. }  


實例4.緩存字節流

  1. /** 
  2.  * 緩衝字符流 
  3.  *  
  4.  */  
  5. package com.io;  
  6. import java.io.*;  
  7. public class IO6 {  
  8.   
  9.       
  10.     public static void main(String[] args) {  
  11.         //緩衝字符流定義  
  12.         BufferedReader br = null;  
  13.         BufferedWriter bw = null;  
  14.           
  15.         try {  
  16.             //創建FileReader對象  
  17.             FileReader fr = new FileReader(new File("C:\\test.txt"));  
  18.             //創建FileWriter對象  
  19.             FileWriter fw = new FileWriter(new File("C:\\test_copy2.txt"));  
  20.               
  21.             //創建緩衝字符流  
  22.             br = new BufferedReader(fr);  
  23.             bw = new BufferedWriter(fw);  
  24.               
  25.               
  26.             //循環讀文件  
  27.             //臨時字符串  
  28.             String s = "";  
  29.             while((s = br.readLine()) != null){  
  30.                 //輸出到文件  
  31.                 bw.write(s + "\r\n");  
  32.             }  
  33.         } catch (Exception e) {  
  34.             e.printStackTrace();  
  35.         } finally {  
  36.               
  37.             //關閉緩衝字符流  
  38.             try {  
  39.                 br.close();  
  40.                 bw.close();  
  41.             } catch (IOException e) {  
  42.                 e.printStackTrace();  
  43.             }  
  44.         }  
  45.           
  46.           
  47.   
  48.     }  
  49.   
  50. }  


實例5.記事本

  1. /** 
  2.  * 記事本(界面+功能) 
  3.  */  
  4. package com.notepad;  
  5.   
  6. import java.io.*;  
  7. import java.awt.*;  
  8. import java.awt.event.*;  
  9.   
  10. import javax.swing.*;  
  11.   
  12. public class NotePad extends JFrame implements ActionListener{  
  13.     //定義組件  
  14.     //文本域  
  15.     JTextArea jta = null;  
  16.     //滾動條  
  17.     JScrollPane jsp = null;  
  18.     //菜單條  
  19.     JMenuBar jmb =null;  
  20.     //菜單欄目  
  21.     JMenu jm = null;  
  22.     //菜單項  
  23.     JMenuItem jmi1 = null;  
  24.     JMenuItem jmi2 = null;  
  25.       
  26.     //構造方法  
  27.     public NotePad(){  
  28.         //創建組件  
  29.         jta = new JTextArea();  
  30.         jsp = new JScrollPane(jta);  
  31.         jmb = new JMenuBar();  
  32.         jm = new JMenu("文件(F)");  
  33.         jmi1 = new JMenuItem("打開(O)");  
  34.         jmi2 = new JMenuItem("保存(S)");  
  35.           
  36.         //設置助記符  
  37.         jm.setMnemonic('F');  
  38.         jmi1.setMnemonic('O');  
  39.         jmi2.setMnemonic('S');  
  40.           
  41.         //設置監聽器  
  42.         jmi1.addActionListener(this);  
  43.         jmi2.addActionListener(this);  
  44.           
  45.         //設置動作監聽器反應命令  
  46.         jmi1.setActionCommand("open");  
  47.         jmi2.setActionCommand("save");  
  48.           
  49.         //設置菜單條  
  50.         setJMenuBar(jmb);  
  51.           
  52.         //把菜單欄目放入菜單條  
  53.         jmb.add(jm);  
  54.           
  55.         //菜單項放入菜單欄  
  56.         jm.add(jmi1);  
  57.         jm.add(jmi2);  
  58.           
  59.           
  60.         //加入到JFrame  
  61.         add(jsp);  
  62.           
  63.           
  64.         //設置窗體  
  65.         setTitle("我的記事本");  
  66.         setSize(400,300);  
  67.         setLocationRelativeTo(null);  
  68.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  69.         setVisible(true);  
  70.     }  
  71.       
  72.       
  73.     public static void main(String[] args) {  
  74.         NotePad np = new NotePad();  
  75.   
  76.     }  
  77.   
  78.     //動作監聽器實現  
  79.     @Override  
  80.     public void actionPerformed(ActionEvent e) {  
  81.           
  82.         if(e.getActionCommand().equals("open")){  
  83.             //文件選擇框  
  84.             JFileChooser jfc = new JFileChooser();  
  85.             jfc.setDialogTitle("打開文件");  
  86.             jfc.showOpenDialog(null);  
  87.             jfc.setVisible(true);  
  88.             String file = jfc.getSelectedFile().getAbsolutePath();  
  89.             //設置緩衝讀入流  
  90.             BufferedReader br = null;  
  91.             try {  
  92.                 br = new BufferedReader(new FileReader(new File(file)));  
  93.                 //臨時字符串  
  94.                 String s = "";  
  95.                 String all = "";  
  96.                 while((s = br.readLine()) != null){  
  97.                     //因爲readLine方法會去掉回車換行  
  98.                     all += s + "\r\n";  
  99.                 }  
  100.                 jta.setText(all);  
  101.             } catch (Exception e2) {  
  102.                 e2.printStackTrace();  
  103.             } finally {  
  104.                 try {  
  105.                     br.close();  
  106.                 } catch (IOException e1) {  
  107.                     e1.printStackTrace();  
  108.                 }  
  109.             }  
  110.               
  111.         } else if(e.getActionCommand().equals("save")){  
  112.             //文件選擇框  
  113.             JFileChooser jfc = new JFileChooser();  
  114.             jfc.setDialogTitle("保存文件");  
  115.             jfc.showSaveDialog(null);  
  116.             jfc.setVisible(true);  
  117.               
  118.             String file = jfc.getSelectedFile().getAbsolutePath();  
  119.             //設置緩衝寫出流  
  120.             BufferedWriter bw = null;  
  121.             try {  
  122.                 bw = new BufferedWriter(new FileWriter(new File(file)));  
  123.                 //臨時存放JTextArea中的字符串  
  124.                 String s = jta.getText();  
  125.                 //將字符串按一行分割成字符串數組  
  126.                 String[] ss = s.split("\r\n");  
  127.                 //循環寫入寫出流  
  128.                 for(int i = 0; i < ss.length; i++){  
  129.                     bw.write(ss[i] + "\r\n");  
  130.                 }  
  131.             } catch (Exception e2) {  
  132.                 e2.printStackTrace();  
  133.             } finally {  
  134.                 try {  
  135.                     bw.close();  
  136.                 } catch (IOException e1) {  
  137.                     e1.printStackTrace();  
  138.                 }  
  139.             }  
  140.               
  141.         } else {  
  142.             System.out.println("無效動作");  
  143.         }  
  144.           
  145.     }  
  146.   
  147. }  


----------參考《韓順平.循序漸進學.java.從入門到精通》

----------參考《JDK_API_1_6_zh_CN

Java學習筆記--導航http://blog.csdn.net/q547550831/article/details/49819641

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