IO


  1.    
  2.     把字符串轉換爲數值  
  3.     常用的形式如下:  
  4.     s=br.readLine(); int i=Double.parseDouble(s);  
  5.     import java.io.*;  
  6.     class javadir{  
  7.         public static void main(String args[]) throws Exception {  
  8.             BufferedReader br=new BufferedReader(new InputStreamReader(System.in));  
  9.             PrintWriter pw=new PrintWriter(System.out);  
  10.             String s;  
  11.             int n;  
  12.               
  13.             double t;  
  14.             pw.println("enter");  
  15.             s=br.readLine();  
  16.             try{  
  17.                 n=Integer.parseInt(s);  
  18.             }catch(NumberFormatException exc){  
  19.                 pw.println("Invalid format");  
  20.                 n=0;  
  21.             }  
  22.             pw.println("Enter your number");  
  23.             for(int i=0;i<n;i++){  
  24.                 s=br.readLine();  
  25.                 try{  
  26.                     t= Double.parseDouble(s);  
  27.                 }catch(NumberFormatException exc){  
  28.                     pw.println("Invalid format");  
  29.                     t=0.0;  
  30.                 }  
  31.             }  
  32.         }  
  33.     }  
  34.       
  35.   
  36.   
  37.   
  38.   
  39.   
  40. java的標準數據流  
  41.     java通過系統類System實現標準輸入/輸出功能。System類在java.lang包中聲明一個final類:  
  42.     public final class System extends Object   
  43.     System類可以直接使用,但不能創建子類  
  44.     System.in提供了read()方法來接受數據,格式如下:  
  45.     public in read() 從輸入流中讀取數據的下一個字節  
  46.     public in read(byte[] b) 從輸入流中讀取一定量的字節並存儲在緩衝區數組b中  
  47.     例如:  
  48.     try{  
  49.         int a=0;  
  50.         System.out.println("輸入一個字節:");  
  51.         a=System.in.read();  
  52.         System.out.println("a="+a);  
  53.     }catch(IOException e){  
  54.         e.printStackTrace();  
  55.     }  
  56.     從輸出可以看出 輸出的是ASCII碼  
  57.       
  58. File類  
  59.     File類用來描述文件的一個類,利用其可以進行文件/文件夾的操作    
  60.       
  61.       
  62.       
  63.       
  64. 創建文件  
  65.     File類提供了幾種構造方法用於在不同的情況下創建File對象,其構造函數如下:  
  66.     public File(String path);  指定路徑  
  67.     public File(String path,String name) ; 指定文件名和路徑  
  68.     public File(File dir,String name); name指示File 對象的文件名,dir指示File對象名  
  69.     例子:  
  70.     File f=new File("d:/java/t.txt");   
  71.   
  72. 刪除文件  
  73.     File 類的delete()方法用於刪除當前的對象,聲明格式如下:  
  74.     public boolean delete();  
  75.     指的注意的是,File對象既可以是文件也可以是文件夾,而當文件夾內不爲空時是不允許刪除的,所以要成功刪除一個文件夾,需要先把文件夾置空  
  76.       
  77. 獲取文件的屬性  
  78.     例子:  
  79.     File a=new File("d:/java/t.txt");  
  80.     a.exists() //判斷a是否存在  
  81.     a.getAbsolutePath() //返回a的絕對路徑  
  82.     a.getName() //返回a的文件名  
  83.     a.length() //返回a的字符數  (a中內容的字符數)  
  84.     a.lastModified() //返回a最後一次修改的時間信息  
  85.       
  86.       
  87. 目錄操作  
  88.     1.mkdir()  
  89.         此方法用來創建子目錄,不過調用一次只會創建一個子目錄,其聲明格式如下:  
  90.         public boolean mkdir()      例子 : File a=new File("d:/java/ch09"); a.mkdir();   如果java這個目錄不存在 則ch09不會被創建  
  91.     2.mkdirs()  
  92.         此方法也是用來創建子目錄的,不過一次可以創建多級目錄,其聲明格式:  
  93.         public boolean mkdirs()    例子 :File f=new File("d://java/ch09"); f.mkdirs(); 該方法首先創建java目錄 然後創建ch09 目錄  
  94.     3.list()  
  95.         此方法以字符串數組的形式返回當前對象中全部文件名稱   
  96.         例子:    String ff[]=null; File f=new File("d://java/ch09"); ff=f.list();  
  97.           
  98. 輸入輸出流  
  99.     字節流  
  100.         如果用戶要讀取的文件比較簡單,可以採用以字節爲單位的方式輸入 輸出  
  101.         1.FileInputStream 類  
  102.             此類事InputStream 派生出來的輸入類,是所有面向字節輸入流的超類,也是一個抽象類,它的構造方法如下:  
  103.             1.FileInputStream(String name) name 表示文件名     例如:   FileInputStream ml=new FileInputStream("t.txt");  
  104.             2.FileInputStream(File file) file表示File對象  例如: File f1=new File("t.txt"); FileInputStream fs=new FileInputStream(f1);  
  105.               
  106.             此類主要的方法  
  107.             read() 方法能讓程序從輸入流中讀取數據,該方法有三種形態:  
  108.             int read()  從輸入流中讀取單個字符  
  109.             int read(byte[] b) 讀取的字符放在 b 數組裏  
  110.             int read(byte[] b,int off,int len)   off 指定把數據放在數組b的什麼地方,len指定該方法將讀取的最大字節數  
  111.             當到達文件尾部時,返回-1  
  112.   
  113.         2.FileOutputStream類  
  114.             此類用於將數據已字節爲單位寫入文件,其構造方法如下:  
  115.             FileOutputStream (String name)   
  116.             FileOutputStream(File file)    例子: FileOutputStream cl=new FileOutputStream("t.doc");  
  117.               
  118.             此類主要使用write() 方法將數據字節寫入文件中,此方法的形態有:  
  119.             public void write(int b)  
  120.             public void write(byte[] b)  
  121.             public void write(byte[] b,int off,int end) off參數指定把數據存放在字節數組b的什麼地方,len參數指定該方法將讀取的最大字節數  
  122.               
  123.   
  124.     字符流  
  125.         1.FileReader類  
  126.             FileReader類是Reader的子類,採用字符流讀取文件,其構造方法如下:  
  127.             1.FileReader(File file); 從文件file 中讀取數據並創建一個新的FileReader 對象  
  128.             2.FileReader(FileDescriptor fd) 構造與某個文件描述符fd相關聯的FileReader對象  
  129.             3.FileReader(String fileName)   
  130.         2.FileWrite類  
  131.             此類用來將數據字字符爲單位寫入文件,它是OutputStreamWriter 的一個子類,其構造方法如下:  
  132.             1.FileWrite(File file);  
  133.             2.FileWrite(File file,boolean append)  
  134.             3.FileWrite(FileDescriptor fd)  
  135.             4.FileWrite(String fileName)   
  136.             5.FileWrite(String fileName,boolean append)  
  137.             其提供write() 方法  
  138.             1.write(int b) 寫入一個字符  
  139.             2.write(char[] b,int off,int len)  
  140.             3.write(String s,int off,int len)  
  141.           
  142.           
  143.     緩存輸入輸出流  
  144.         如果希望以此讀取一行文本,可以使用FileReader類和BufferedReader類結合起來使用  
  145.         1.BufferedReader  
  146.         此類採用緩存方式從字符輸入流中讀取數據,一般不單獨使用,常和其他流一起使用  
  147.         其構造方法如下:  
  148.         public BufferedReader(Reader in)   讀入數據源  
  149.         public BufferedReader(Reader in,int sz) 讀入指定緩衝大小的數據源  
  150.           
  151.         當FileReader對象創建後,我們可以在創建一個BufferedReader對象,就可以按行讀取文本了  
  152.         例如:  
  153.         try{  
  154.             FileReader ml=new FileReader("t.doc");  
  155.             BufferedReader br=new BufferedReader(ml);  
  156.             String s=br.readLine();  
  157.         }catch(IOException e)  
  158.           
  159.           
  160.         2.BufferedWriter  
  161.         當需要一行行寫數據的時候,可以將BufferedWriter對象和FileWriter對象綁定在一起,然後通過BufferedWriter對象將數據寫到  
  162.         目的地,除了write()方法外 BufferedWriter還有一個newLine() 方法可以用來發送行結束符  
  163.         例如:  
  164.         try{  
  165.             FileWriter fw=new FileWriter("t.doc");  
  166.             BufferedWriter bw=new BufferedWriter(fw);  
  167.             String word="abc";  
  168.             bw.write(word);  
  169.             bw.newLine();  //用來發送行結束符  
  170.             bw.close();  
  171.         }catch(IOException e){}  
  172.           
  173.           
  174.     數據輸入輸出流  
  175.         數據輸入流允許應用程序以與機器無關的方式從底層輸入流中讀取java基本數據類型, 而數據輸出流允許應用程序以適當的發送將java基本數據類型寫入輸出流中  
  176.           
  177.             1.DataInputStream  
  178.                 此類可以從輸入流中讀取各種基本類型的數據,如 int boolean float   
  179.                 其構造方法如下:  
  180.                 public DataInputStream(InputStream in)    
  181.             2.DataOutputStream  
  182.                 此類可以將基本數據類型寫入輸出流  
  183.                 其構造方法如下:  
  184.                 public DataOutputStream(OutputStream out)  
  185.                   
  186.     壓縮輸入輸出流  
  187.         一個zip壓縮文件通常由一個或多個entry組成,每個entry有唯一的名稱,entry存儲壓縮文件的數據  
  188.         要創建壓縮文件,需要用的到zip包中的ZipInputStream ZipOutputStream ZipEntry 類  
  189.           
  190.         1.ZipInputStream  
  191.         此類是讀取壓縮文件是輸入流過濾器,其構造函數如下:  
  192.         ZipInputStream(InputStream in)     
  193.         其定義的方法如下:  
  194.         int available() 如果到達文件尾  返回0 否則返回1  
  195.         Void close()  關閉zip輸入流  
  196.         Void closeEntry() 關閉當前的zip記錄  
  197.         ZipEntry getNextEntry() 讀取下一個zip文件記錄數據  
  198.         int read(byte[] b,int off,int len)  將數據讀入字節數組  
  199.         long skip(long n)  跳過指定的字節數  
  200.           
  201.         FileInputStream in=new FileInputStream("t.txt");  
  202.         ZipInputStream zin=new ZipInputStream(in);  
  203.                   
  204.           
  205.         2.ZipOutputStream  
  206.             此類能夠將輸出流中的數據以Zip格式寫入文件,它支持壓縮和非壓縮兩種  主要用於創建zip文件 ,其構造方法如下:  
  207.             ZipOutputStream(OutputStream out)    
  208.             其主要方法如下:  
  209.             void close() 關閉zip輸出流和正在過濾的流  
  210.             Void closeEntry() 關閉當前zip  
  211.             Void finish() 完成寫入zip輸入流的內容  
  212.             void putNextEntry(ZipEntry e)  定位到下一個zip記錄數據的開始處  
  213.             void setComment(String comment)   設置zip註釋  
  214.             viod setLevel(int level) 設置壓縮級別  
  215.             void setMethod(int method) 設置默認的壓縮方法  
  216.             void write(byte[] b,int off,int len)  將字節數組寫入zip  
  217.           
  218.     3.ZipEntry  
  219.         此類用來描述zip文件 中文件條目,其構造方法如下:  
  220.         ZipEntry(String name)  
  221.           
  222.           
  223.     創建壓縮文件  
  224.         一般情況下,一個Zip文件有多個文件或文件夾,對於每個文件我們都需要創建一個ZipEntry對象並  
  225.         依次放進zip文件中,我們需要創建一個 ZipOutputStream 對象表示這個文件  
  226.         ZipOutputStream zout=new ZipOutputStream(new FileOutputStream("t.zip"));  
  227.         例如:  
  228.         public class ZIP{  
  229.             public static void main(String args[]){  
  230.                 File file=new File("p_w_picpath");  //壓縮源  
  231.                 createZip(file+".zip",file);  
  232.             }  
  233.         }  
  234.         private static void createZip(String zip,File source){  
  235.             try{  
  236.                 ZipOutputStream out=new ZipOutputStream(new FileOutputStream(zip));  
  237.                 System.out.println("開始壓縮...");  
  238.                 compressFile(out,"",source);  
  239.                 System.out.println("壓縮完成");  
  240.                 out.close();  
  241.             }catch(FileNotFoundException e){  
  242.                 e.printStackTrace();  
  243.             }catch(IOException ee){  
  244.                 e.printStackTrace();  
  245.             }  
  246.         }  
  247.         private static void compressFile(ZipOutputStream out,String dir,File source) throws IOException{  
  248.             System.out.println("正在壓縮"+source);  
  249.             if(source.isDirectory()){  
  250.                 File []files=source.listFile();  
  251.                 dir=dir+(dir.length()==0?"":"/")+source.getName();  
  252.                 for(File e:files){  
  253.                     compressFile(out,dir,e);  
  254.                 }  
  255.             }else{  
  256.                 dir=dir.length()==0?"":dir+"/"+.source.getName();  
  257.                 ZipEntry entry=new ZipEntry(dir);  
  258.                 out.putNextEntry(entry);  
  259.                 FileInputStream fis=new FileInputStream(source);  
  260.                 int size=0;  
  261.                 byte[] buffer=new byte[10240];  
  262.                 while((size=fis.read(buffer,0,10240))!=-1)  
  263.                 out.write(buffer,0,size);  
  264.                 fis.close();  
  265.             }  
  266.         }  
  267.           
  268.           
  269.           
  270.           
  271.           
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章