文件

 

  1. 1.  File 類用來代表一個文件,一個目錄名或一個目錄名和文件名組合。 
  2.     File類共提供了3中構造方法 
  3.     1. File(String path) Path可是帶絕對路徑的文件名,帶相對路徑的文件名,或者某個目錄 
  4.     2. File(String path,String name) path表示所對應的文件或目錄的絕對或相對路徑,name表示文件或目錄名 
  5.     3. File(File dir,String name) 使用一個已有的File對象作爲參數,表示文件或目錄的路徑,第二個字符表示文件名或目錄名 
  6.  
  7.  
  8. 2.  建立與刪除文件 
  9.      創建文件 
  10.      public boolean createNewFile();創建一個空文件 
  11.       
  12.      重命名文件 
  13.      public boolean renameTo(File newFile);將文件重命名成newFile對應的文件名 
  14.       
  15.      刪除文件 
  16.      public void delete();刪除文件 
  17.       
  18.      創建目錄 
  19.      public boolean mkdir();創建當前目錄的子目錄 
  20.       
  21.      下面一組句子將文件改名並且設置成只讀 
  22.       File f=new File("c:\t.txt"); 
  23.       f.renameTo(new File("c:\tt.txt")); 
  24.       f.setReadOnly(); 
  25.        
  26.     下面的語句建立目錄 
  27.       File f=new File("c:\demo");  其中的參數是要建立文件名 
  28.       f.mkdir(); 
  29.        
  30.     下面的語句建立文件後將其刪除 
  31.       File f=new File("demo.txt"); 
  32.       f.delete(); 
  33.     
  34. 3.  獲取文件或目錄的屬性 
  35.     判斷文件或目錄是否存在 
  36.       public boolean exists(); 
  37.        
  38.     判斷是文件還是目錄 
  39.       public boolean isFile(); 
  40.       public boolean isDirectory(); 
  41.       
  42.     獲取文件或目錄名稱與路徑 
  43.       public String getName();  
  44.       public String getPath(); 
  45.        
  46.     獲取文件的長度 
  47.       public long length(); 
  48.        
  49.     獲取文件的讀寫屬性 
  50.       public boolean canRead(); 
  51.       public boolean canWrite(); 
  52.        
  53.     比較兩個文件或目錄 
  54.       public boolean equals(File f); 
  55.        
  56.    4.  獲取目錄成員 
  57.       File類有個String list[] 方法,可以將目錄中所有文件名保存在一個字符串數組中,然後逐一的將這些文件顯示出來 
  58.       代碼如下: 
  59.       import java.io.File; 
  60.       public class tt{ 
  61.           public void static print(String s){ 
  62.               System.out.print(s); 
  63.           } 
  64.           public static void main(String args[]){ 
  65.               String path="."
  66.               File f=new File(path); 
  67.               if(f.isDirectory()){ 
  68.                   print("文件路徑"+path+"\n"); 
  69.                   String s[]=f.list(); 
  70.                   for(int i=0;i<f.length;i++){ 
  71.                       File ff=new File(path,s[i]); 
  72.                       if(ff.isDrectory()){ 
  73.                           print(s[i]+"是個目錄\n"); 
  74.                       }elseif(ff.isFile()){ 
  75.                           print(s[i]+"是個文件\n"); 
  76.                           print(ff.canRead()?"可以讀取\n":"不可以讀取\n"); 
  77.                       } 
  78.                   } 
  79.               } 
  80.           } 
  81.       } 
  82.  
  83. 5.  文件名過濾 
  84.        可以利用文件名過濾器接口(FilenameFilter)將其他類型的文件過濾掉,只保留所需要的文件 
  85.        該接口包含唯一的抽象方法 boolean accept (File dir,String name); 
  86.        建立一個文件名過濾器的方法如下: 
  87.          1. 定義一個實現FilenameFilter    接口的類,如 FileFilterDemo 
  88.       2. 定義要保留文件名和後綴名的成員變量 name 和 ext 
  89.       3. 定義該類的構造方法,並設置參數能夠初始化成員變量 
  90.       4. 定義accept方法,利用字符串的startWith(name)方法和endWith(ext)方法,判斷是否符合文件名和後綴名 
  91.     例子如下: 
  92.        class FilenameFilterDemo implements FilenameFilter{ 
  93.         private name; 
  94.         private ext; 
  95.         public FilenameFilterDemo(String name,String ext){ 
  96.             this.name=name; 
  97.             this.ext=ext; 
  98.         } 
  99.         public boolean accept(File dir,String filename){ 
  100.             boolean fString=true
  101.             if(name!=null){ 
  102.                 fString &= filename.startWith(name); 
  103.             } 
  104.             if(ext!=null){ 
  105.                 fString &= filename.endWith(ext); 
  106.             } 
  107.             return (fString); 
  108.         } 
  109.     }        
  110.        
  111.     定義好文件名過濾器後,將工作目錄作爲參數,建立File 對象 
  112.     創建文件名過濾對象,並將要提取的文件名和後綴名作爲創建時的構造方法參數 
  113.     使用File對象調用list() 對象,並將過濾類的對象作爲list()的參數 
  114.     例子如下: 
  115.     import java.io.*;  
  116.        class FilenameFilterDemo implements FilenameFilter{ 
  117.         private name; 
  118.         private ext; 
  119.         public FilenameFilterDemo(String name,String ext){ 
  120.             this.name=name; 
  121.             this.ext=ext; 
  122.         } 
  123.         public boolean accept(File dir,String filename){ 
  124.             boolean fString=true
  125.             if(name!=null){ 
  126.                 fString &= filename.startWith(name); 
  127.             } 
  128.             if(ext!=null){ 
  129.                 fString &= filename.endWith(ext); 
  130.             } 
  131.             return (fString); 
  132.         } 
  133.         public class javadir{ 
  134.             static void print(String s){ 
  135.                 System.out.print(s); 
  136.             } 
  137.             public static void main(String args[]){ 
  138.                 File path=new File("."); 
  139.                 print("文件路徑"+path); 
  140.                 FilenameFilter x=new FilenameFilterDemo(args[0],args[1]); 
  141.                 String s[]=path.list(x); 
  142.                 for(int i=0;i<s.length;i++){ 
  143.                     print(s[i]); 
  144.                 } 
  145.             } 
  146.         } 
  147.     } 
  148.      
  149. 6.  文件輸入流(FileInputString) 
  150.        FileInputString類是從InputStream類派生出來的簡單輸入流類,它可以處理簡單的文件傳輸操作 
  151.        它的構造方法有三種形式 
  152.        FileInputStream(String filename) FileInputStream(File file) FileInputStream(FileDescriptor fd) 
  153.        FileInputStream的構造方法用於建立一個從文件中讀取底層數據的字節流 
  154.        此類的主要方法 
  155.        int read() 從流中讀入一個字節,並將該字節作爲一個整數返回,若沒有數據則返回-1 
  156.        int read(byte b[]) 該方法以一個字節型數組作爲參數,可以用於一次讀取多個字節,讀入的字節直接放入數組b[]中,並返回讀取的字節數 
  157.        long skip(long n)throws IOException 跳過指定的字節數 
  158.        int available()throws IOException 返回當前流中可用的字節數 
  159.        void close() 關閉當前流對象 
  160.        例子如下: 
  161.        import java.io.*; 
  162.     public class javadir{ 
  163.         public static void print(String s){ 
  164.             System.out.print(s); 
  165.         } 
  166.         public static void main(String args[]) throws Exception{ 
  167.             int size=f.available(); 
  168.             FileInputStream f=new FileInputStream("t.txt"); 
  169.             print("該文件共有"+size+"字節"); 
  170.             byte data[]=new byte[size]; 
  171.             if(f.read(data)!=size) print("不能讀取\n"); 
  172.             else print(new String(data,0,size)); 
  173.             f.close(); 
  174.         } 
  175.     }        
  176.      
  177. 7.  文件輸出流(FileOutputStream) 
  178.        它可以簡單的向文件寫入數據 
  179.        它的構造方法有三種: 
  180.        FileOutputStream(String filename) FileOutputStream(File file) FileOutputStream(FileDescript fd)   
  181.        需要注意的是,如果用一個已經存在的文件名創建一個FileOutputStream對象,則這個文件將在無警告的情況下被一個空文件覆蓋 
  182.        常用的方法有如下幾種: 
  183.        write(int b) 向流中寫入一個字節 
  184.        write(byte[] b)向流中寫入一個字節數組 
  185.        write(byte[] b,int off,int len) 在從數組的第off個位置,寫入len個數據 
  186.        void close() 關閉流對象 
  187.     import java.io.*; 
  188.     public class javadir{ 
  189.         public static void print(String s){ 
  190.             System.out.print(s); 
  191.         } 
  192.         public static void main(String args[]) throws Exception{ 
  193.             byte []data="you will never win,if you do not begin".getBytes();//“”部分是一個字符串對象,因此可以調用getBytes()方法,該方法將字符串對象根據所在平臺默認的字符集編碼成字節數據序列,然後存儲到data字節中 
  194.             FileOutputStream f=new FileOutputStream("t.txt"); 
  195.             for(int i=0;i<data.length;i++){ f.write(data[i]); } 
  196.             FileOutputStream ff=new FileOutputStream("t.txt",true); 
  197.             ff.write(data); 
  198.             FileOutputStream fff=new FileOutputStream("t.txt",true); 
  199.             fff.write(data,0,18); 
  200.             f.close(); 
  201.             ff.close(); 
  202.             fff.close(); 
  203.         } 
  204.     } 
  205.      
  206.    8.  字節數組輸入流(ByteArrayInputStream) 
  207.     它可以用於讀取字節數組,並存入字節數組輸入流對象中 
  208.     它的構造方法如下: 
  209.     ByteArrayInputStream(byte[] bytedata) ByteArrayInputStream(byte[] bytedata,int start,int len) 
  210.     它的主要方法有: 
  211.     int read()  int read(byte b[])  long skip(long n)  int available()   void close() 
  212.     Synchronized void mark(int readlimit) 在流中標記一個位置 
  213.     Synchronized void reset() throws IOException 重新設置到流中上一次mark方法所標記的位置 
  214.     boolean markSupport() 返回一個表示流是否支持mark,reset操作的布爾值 
  215.     例子如下: 
  216.     import java.io.*; 
  217.     public class javadir{ 
  218.         public static void print(char s){ 
  219.             System.out.print(s); 
  220.         } 
  221.         public static void main(String args[]) throws Exception{ 
  222.             byte[] data="success is never ending ".getBytes(); 
  223.             ByteArrayInputStream f=new ByteArrayInputStream(data); 
  224.             int ch; 
  225.             while((ch=f.read())!=-1) print((char)ch); 
  226.         } 
  227.     } 
  228.      
  229.        字節數組輸出流(ByteArrayOutputStream) 
  230.        它可以將字節數組中的數據輸出到ByteArrayOutputStream中 
  231.        它的構造方法如下: 
  232.        ByteArrayOutputStream()     ByteArrayOutputStream(int len) 
  233.        int len 爲輸出的字節數,如果建立該對象是未設置輸出長度,則預設32個字節 
  234.        此類除了具有一般OutputStream所具有的方法外如 void write(int b)  void write(byte b[])  write(byte b[],int off,int len)  void close() 
  235.        還有兩個常用的方法 byte[] toByteArray  配置一個新的字節組  void writeto(OutputStream out) 將ByteArrayOutputStream對象的內容寫入OUTputStrean對象中 
  236.     此類的第一個構造方法直接讀取字節數組bytedata的全部元素到字節數組輸出流對象中,第二個構造方法從字節數組bytedata中一Start爲起點讀取len個字節到字節數組輸出流對象中 
  237.      
  238. 9.  數據輸出流(DataOutputStream) 
  239.        向輸出流中寫Java的基本數據類型,寫入的數據和方式是可以移植的,它們能用對應的DataInputStream類讀取, 
  240.        它的構造方法如下: 
  241.        public DataOutputStream(OutputStream out)  out是一個OutputStream對象,所以必須創建一個OutputStream對象 
  242.     DataOutputStream提供了多個方法來輸出各種類型的數據,除了一般輸出流具有的void write 
  243.     還有以下方法: 
  244.     public final void writeBoolean(boolean v) 
  245.     public final void writeByte(int v) 
  246.     public final void writeChar(int v) 
  247.     public final void writeInt(int v) 
  248.     public final void writeDouble(double v) 
  249.     public final void writeBytes(String s) 
  250.     public final void writeUTF(String str)//寫的是一個採用UTF_8編碼的字符串,使得字符串數據獨立於平臺 
  251.     例子如下: 
  252.     import java.io.*; 
  253.     public class javadir{ 
  254.         public static void main(String args[]){ 
  255.             double e=2.7
  256.             int i=8
  257.             boolean ok=true
  258.             char cc='A'
  259.             String s="2010 expo"
  260.             try
  261.                 FileOutputStream fs_out=new FileOutputStream("t.txt"); 
  262.                 DataOutputStream out=new DataOutputStream(fs_out); 
  263.                 out.writeDouble(e); 
  264.                 out.writeInt(i); 
  265.                 out.writeBoolean(ok); 
  266.                 out.writeChar(cc); 
  267.                 out.writeUTF(s); 
  268.                 out.close(); 
  269.             }catch(FileNotFoundException fe){ 
  270.                 System.out.println(fe); 
  271.             }catch(IOException ioe){ 
  272.                 System.out.println(ioe); 
  273.             } 
  274.         } 
  275.     } 
  276.        
  277.     數據輸入流(DataInputStream) 
  278.     這個類已與機器無關的方式從一個流中讀取java的基本類型,所讀的數據應是DataOutputStream對象所寫入的 
  279.     它的構造方法如下: 
  280.     public DataOutputStream(InputStream) 此構造方法需要一個底層的輸入對象作爲參數,如FileInputStream的對象 
  281.     該類除了int read()等方法外,還有一些直接讀取基本類型的成員方法 
  282.     public final int skipBytes(int n) 
  283.     public final boolean readBoolean() 
  284.     public final byte readByte() 
  285.     public final char readChar() 
  286.     public final int readInt() 
  287.     public final double readDouble() 
  288.     public final String readString() 
  289.     例子如下: 
  290.     import java.io.*; 
  291.     public class javadir{ 
  292.         public static void main(String args[]){ 
  293.             try
  294.                 FileInputStream fs_in=new FileInputStream("t.txt"); 
  295.                 DataInputStream in=new DataInputStream(fs_in); 
  296.                 double e=in.readDouble();System.out.println(e); 
  297.                 int i=in.readInt();System.out.println(i); 
  298.                 boolean ok=in.readBoolean();System.out.println(ok); 
  299.                 char cc=in.readChar();System.out.println(cc); 
  300.                 String s=in.readUTF();System.out.println(s); 
  301.                 in.close(); 
  302.             }catch(FileNotFoundException fd){ 
  303.                 System.out.println(fd); 
  304.             }catch(IOException ioe){ 
  305.                 System.out.println(ioe); 
  306.             } 
  307.         } 
  308.     } 
  309.      
  310. 11. 緩存輸出流(BufferedOutputStream) 
  311.        此類增強了輸出流的批量數據輸出能力,它與另一個輸出流相連,將它作爲自己的輸入, 
  312.        它可以從與它相連的輸出流中填充數據到內部緩存,一次輸出 
  313.        它的構造方法如下: 
  314.        public BufferedOutputStream(OutputStream out) 
  315.        public BufferedOutputStream(OutputStream out,int len)  len是緩存大小 
  316.        其主要方法除了 void write(int b)等 還有一個常用的方法,void flush() throws IOException該方法清空流並強制將緩存區的數據寫入到流中 
  317.        例子如下: 
  318.        import java.io.*; 
  319.     public class javadir{ 
  320.         public static void main(String args[]){ 
  321.             try
  322.                 long start=System.currentTimeMillis(); 
  323.                 FileOutputStream fs_out=new FileOutputStream("t.txt"); 
  324.                 BufferedOutputStream bfs_out=new BufferedOutputStream(fs_out); 
  325.                 DataOutputStream out=new DataOutputStream(bfs_out); 
  326.                 for(int i=0;i<10000;i++){ 
  327.                     out.writeDouble(Math.random()); 
  328.                 } 
  329.                 out.close(); 
  330.                 long stop=System.currentTimeMillis(); 
  331.                 System.out.println("時間差爲:"+(stop-start)); 
  332.             }catch(FileNotFoundException fd){ 
  333.                 System.out.println(fd); 
  334.             }catch(IOException ioe){ 
  335.                 System.out.println(ioe); 
  336.             } 
  337.         } 
  338.     }        
  339.      
  340.     緩存輸入流(BufferedInputStream) 
  341.     此類增強了輸入流的批量數據處理能力,它與另一個輸入流相連,將它作爲自己的輸入,當讀物==讀取或跳過流中的字節時, 
  342.     就會從與它相連的輸入流中在填充數據到內部緩存 
  343.     它的構造方法如下: 
  344.     public BufferedInputStream(InputStream in)  public BufferedInputStream(InputStream in,int len) 
  345.      
  346.      
  347. 10.  格式化輸出流(PrintStream) 
  348.      它的常用構造方法如下: 
  349.      PrintStream(OutputStream out)  PrintStream(OutputStream out,boolean flag) flag是個清理標識,flag爲true是,當換行符'\n'出現在輸出流中,PrintStream會自動輸出流中的數據; 
  350.      flag 爲false 時,當換行符'\n'出現在輸出流中,PrintStream則不會自動輸出流中的數據 
  351.      它有如下形式的write() 方法;  public void write(int b)   public void write(byte b[],int off,int len) 
  352.      例子如下: 
  353.         import java.io.*; 
  354.         public class javadir{ 
  355.             public static void main(String args[]) throws Exception{ 
  356.                PrintStream outfile=new PrintStream(new FileOutputStream("t.txt")); 
  357.                outfile.println("sin 30 degree="+Math.sin(Math.sin(Math.PI/6))); 
  358.                System.out.println("sin 30 degree="+Math.sin(Math.sin(Math.PI/6))); 
  359.                outfile.close(); 
  360.             } 
  361.         } 
  362.      
  363.  
  364. 11. 字符集輸入流(Reader) 
  365.     此方法以一個字符型數組作爲參數,可用於一次讀取多個字符,讀入字符直接放入數組中,並返回讀取的字符數 
  366.     該方法類似於上一中read方法,不同的是設置了偏移量off 。這裏的偏移量指的是可以從字符型數組的第off個位置起,讀取len個字符 
  367.     這個方法還可以用於防止數組越界,其用法是off設置成0,len設置成數組長度 
  368.     long skip(long  n) throws IOException 跳過指定的字符數 
  369.     Synchronized void mark(int readlimit) 在字符流中標記一個位置 
  370.     Synchronized void reset() 重新設置到上一次標記的位置 
  371.     public void close()  
  372.     boolean markSupport()  
  373.  
  374.    
  375.      
  376.     字符集輸出流(Writer) 
  377.     主要方法如下: 
  378.     write(int c) 向Write流對象中寫入一個字符 
  379.     write(char[] cc) 向流對象中寫入一個字符數組 
  380.     write(char[] cc,int off,int len) 從數組的第off個位置,寫入len個字符長度到流對象中 
  381.     write(String s) 向流對象中寫入一個字符串 
  382.     write(String str,int off,int len) 字符串的第off個位置,寫入len個字符 
  383.     void close() 
  384.     public void flush() throws IOException  
  385.      
  386.     上述兩個類都是最高層的類,一般並不直接使用它們 
  387.      
  388.      
  389.      
  390.      
  391.      
  392.      
  393.     FileReader類 
  394.     此類可以方便的將一個文件連接到其他需要以Reader作爲輸入參數的類上 
  395.     其構造方法如下: 
  396.     FileReader(String filename) 
  397.     FileReader(File file) 
  398.     FileReader(FileDescript fd) 
  399.     FileReader可用於讀取字符文件中的內容,但一次只能讀取一個字符,所以通常將FileReader對象內置於BufferedReader對象中 
  400.      
  401.      
  402.     BufferedReader類 
  403.     從字符輸入流中讀取文本,在必要時將字符存入緩存 
  404.     其構造方法如下所示: 
  405.     public BufferedReader(Reader in)   public BufferedReader(Reader in,int len) 
  406.     此類除了具有Reader類的一般方法外,還有以下方法 
  407.     public String readLine() throws IOException 該方法讀取一行文字,到達最後返回null 
  408.     import java.io.*; 
  409.     public class javadir{ 
  410.         public static void main(String args[]) throws IOException{ 
  411.             FileReader fr=new FileReader("t.class"); 
  412.             BufferedReader br=new BufferedReader(fr); 
  413.             String s; 
  414.             while((s=br.readLine())!=null) System.out.println(s); 
  415.             fr.close(); 
  416.         } 
  417.     } 
  418.     以上兩個類合起來用 
  419.      
  420.      
  421.     FileWrite類 
  422.     此類把字符輸入流與文件連接起來 
  423.     它的構造方法如下: 
  424.     FileWrite(File file) 
  425.     FileWrite(String filename) 
  426.     FileWrite(String filename,boolean append) 
  427.      
  428.     BufferedWrite類 
  429.     此類用來創建一個字符緩存輸入流,它主要爲其他類 如PrintWrite提供一個字符輸入流 
  430.     它的構造方法如下: 
  431.     public BufferedWrite(Writer out) 
  432.     public BufferedWrite(Write out,int len) 
  433.     此類除了提供了Writer類具有的方法外,還提供瞭如下方法: 
  434.     void newLine() 插入一個新行符 
  435.     void flush() 強迫刷新 
  436.      
  437.     PrintWrite類 
  438.     此類爲格式化字符流提供了一些實用的方法,它最好還是與其他輸出流一起使用,提高流的效率 
  439.     它的構造方法如下: 
  440.      
  441.  
  442.        
  443.        
  444.        
  445.        
  446.        
  447.        
  448.        
  449.        
  450.        
  451.        
  452.        
  453.        
  454.        
  455.        
  456.        
  457.        
  458.        
  459.        
  460.        
  461.        
  462.        
  463.        
  464.        
  465.        
  466.        
  467.        

 

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