- 1. File 類用來代表一個文件,一個目錄名或一個目錄名和文件名組合。
- File類共提供了3中構造方法
- 1. File(String path) Path可是帶絕對路徑的文件名,帶相對路徑的文件名,或者某個目錄
- 2. File(String path,String name) path表示所對應的文件或目錄的絕對或相對路徑,name表示文件或目錄名
- 3. File(File dir,String name) 使用一個已有的File對象作爲參數,表示文件或目錄的路徑,第二個字符表示文件名或目錄名
- 2. 建立與刪除文件
- 創建文件
- public boolean createNewFile();創建一個空文件
- 重命名文件
- public boolean renameTo(File newFile);將文件重命名成newFile對應的文件名
- 刪除文件
- public void delete();刪除文件
- 創建目錄
- public boolean mkdir();創建當前目錄的子目錄
- 下面一組句子將文件改名並且設置成只讀
- File f=new File("c:\t.txt");
- f.renameTo(new File("c:\tt.txt"));
- f.setReadOnly();
- 下面的語句建立目錄
- File f=new File("c:\demo"); 其中的參數是要建立文件名
- f.mkdir();
- 下面的語句建立文件後將其刪除
- File f=new File("demo.txt");
- f.delete();
- 3. 獲取文件或目錄的屬性
- 判斷文件或目錄是否存在
- public boolean exists();
- 判斷是文件還是目錄
- public boolean isFile();
- public boolean isDirectory();
- 獲取文件或目錄名稱與路徑
- public String getName();
- public String getPath();
- 獲取文件的長度
- public long length();
- 獲取文件的讀寫屬性
- public boolean canRead();
- public boolean canWrite();
- 比較兩個文件或目錄
- public boolean equals(File f);
- 4. 獲取目錄成員
- File類有個String list[] 方法,可以將目錄中所有文件名保存在一個字符串數組中,然後逐一的將這些文件顯示出來
- 代碼如下:
- import java.io.File;
- public class tt{
- public void static print(String s){
- System.out.print(s);
- }
- public static void main(String args[]){
- String path=".";
- File f=new File(path);
- if(f.isDirectory()){
- print("文件路徑"+path+"\n");
- String s[]=f.list();
- for(int i=0;i<f.length;i++){
- File ff=new File(path,s[i]);
- if(ff.isDrectory()){
- print(s[i]+"是個目錄\n");
- }elseif(ff.isFile()){
- print(s[i]+"是個文件\n");
- print(ff.canRead()?"可以讀取\n":"不可以讀取\n");
- }
- }
- }
- }
- }
- 5. 文件名過濾
- 可以利用文件名過濾器接口(FilenameFilter)將其他類型的文件過濾掉,只保留所需要的文件
- 該接口包含唯一的抽象方法 boolean accept (File dir,String name);
- 建立一個文件名過濾器的方法如下:
- 1. 定義一個實現FilenameFilter 接口的類,如 FileFilterDemo
- 2. 定義要保留文件名和後綴名的成員變量 name 和 ext
- 3. 定義該類的構造方法,並設置參數能夠初始化成員變量
- 4. 定義accept方法,利用字符串的startWith(name)方法和endWith(ext)方法,判斷是否符合文件名和後綴名
- 例子如下:
- class FilenameFilterDemo implements FilenameFilter{
- private name;
- private ext;
- public FilenameFilterDemo(String name,String ext){
- this.name=name;
- this.ext=ext;
- }
- public boolean accept(File dir,String filename){
- boolean fString=true;
- if(name!=null){
- fString &= filename.startWith(name);
- }
- if(ext!=null){
- fString &= filename.endWith(ext);
- }
- return (fString);
- }
- }
- 定義好文件名過濾器後,將工作目錄作爲參數,建立File 對象
- 創建文件名過濾對象,並將要提取的文件名和後綴名作爲創建時的構造方法參數
- 使用File對象調用list() 對象,並將過濾類的對象作爲list()的參數
- 例子如下:
- import java.io.*;
- class FilenameFilterDemo implements FilenameFilter{
- private name;
- private ext;
- public FilenameFilterDemo(String name,String ext){
- this.name=name;
- this.ext=ext;
- }
- public boolean accept(File dir,String filename){
- boolean fString=true;
- if(name!=null){
- fString &= filename.startWith(name);
- }
- if(ext!=null){
- fString &= filename.endWith(ext);
- }
- return (fString);
- }
- public class javadir{
- static void print(String s){
- System.out.print(s);
- }
- public static void main(String args[]){
- File path=new File(".");
- print("文件路徑"+path);
- FilenameFilter x=new FilenameFilterDemo(args[0],args[1]);
- String s[]=path.list(x);
- for(int i=0;i<s.length;i++){
- print(s[i]);
- }
- }
- }
- }
- 6. 文件輸入流(FileInputString)
- FileInputString類是從InputStream類派生出來的簡單輸入流類,它可以處理簡單的文件傳輸操作
- 它的構造方法有三種形式
- FileInputStream(String filename) FileInputStream(File file) FileInputStream(FileDescriptor fd)
- FileInputStream的構造方法用於建立一個從文件中讀取底層數據的字節流
- 此類的主要方法
- int read() 從流中讀入一個字節,並將該字節作爲一個整數返回,若沒有數據則返回-1
- int read(byte b[]) 該方法以一個字節型數組作爲參數,可以用於一次讀取多個字節,讀入的字節直接放入數組b[]中,並返回讀取的字節數
- long skip(long n)throws IOException 跳過指定的字節數
- int available()throws IOException 返回當前流中可用的字節數
- void close() 關閉當前流對象
- 例子如下:
- import java.io.*;
- public class javadir{
- public static void print(String s){
- System.out.print(s);
- }
- public static void main(String args[]) throws Exception{
- int size=f.available();
- FileInputStream f=new FileInputStream("t.txt");
- print("該文件共有"+size+"字節");
- byte data[]=new byte[size];
- if(f.read(data)!=size) print("不能讀取\n");
- else print(new String(data,0,size));
- f.close();
- }
- }
- 7. 文件輸出流(FileOutputStream)
- 它可以簡單的向文件寫入數據
- 它的構造方法有三種:
- FileOutputStream(String filename) FileOutputStream(File file) FileOutputStream(FileDescript fd)
- 需要注意的是,如果用一個已經存在的文件名創建一個FileOutputStream對象,則這個文件將在無警告的情況下被一個空文件覆蓋
- 常用的方法有如下幾種:
- write(int b) 向流中寫入一個字節
- write(byte[] b)向流中寫入一個字節數組
- write(byte[] b,int off,int len) 在從數組的第off個位置,寫入len個數據
- void close() 關閉流對象
- import java.io.*;
- public class javadir{
- public static void print(String s){
- System.out.print(s);
- }
- public static void main(String args[]) throws Exception{
- byte []data="you will never win,if you do not begin".getBytes();//“”部分是一個字符串對象,因此可以調用getBytes()方法,該方法將字符串對象根據所在平臺默認的字符集編碼成字節數據序列,然後存儲到data字節中
- FileOutputStream f=new FileOutputStream("t.txt");
- for(int i=0;i<data.length;i++){ f.write(data[i]); }
- FileOutputStream ff=new FileOutputStream("t.txt",true);
- ff.write(data);
- FileOutputStream fff=new FileOutputStream("t.txt",true);
- fff.write(data,0,18);
- f.close();
- ff.close();
- fff.close();
- }
- }
- 8. 字節數組輸入流(ByteArrayInputStream)
- 它可以用於讀取字節數組,並存入字節數組輸入流對象中
- 它的構造方法如下:
- ByteArrayInputStream(byte[] bytedata) ByteArrayInputStream(byte[] bytedata,int start,int len)
- 它的主要方法有:
- int read() int read(byte b[]) long skip(long n) int available() void close()
- Synchronized void mark(int readlimit) 在流中標記一個位置
- Synchronized void reset() throws IOException 重新設置到流中上一次mark方法所標記的位置
- boolean markSupport() 返回一個表示流是否支持mark,reset操作的布爾值
- 例子如下:
- import java.io.*;
- public class javadir{
- public static void print(char s){
- System.out.print(s);
- }
- public static void main(String args[]) throws Exception{
- byte[] data="success is never ending ".getBytes();
- ByteArrayInputStream f=new ByteArrayInputStream(data);
- int ch;
- while((ch=f.read())!=-1) print((char)ch);
- }
- }
- 字節數組輸出流(ByteArrayOutputStream)
- 它可以將字節數組中的數據輸出到ByteArrayOutputStream中
- 它的構造方法如下:
- ByteArrayOutputStream() ByteArrayOutputStream(int len)
- int len 爲輸出的字節數,如果建立該對象是未設置輸出長度,則預設32個字節
- 此類除了具有一般OutputStream所具有的方法外如 void write(int b) void write(byte b[]) write(byte b[],int off,int len) void close()
- 還有兩個常用的方法 byte[] toByteArray 配置一個新的字節組 void writeto(OutputStream out) 將ByteArrayOutputStream對象的內容寫入OUTputStrean對象中
- 此類的第一個構造方法直接讀取字節數組bytedata的全部元素到字節數組輸出流對象中,第二個構造方法從字節數組bytedata中一Start爲起點讀取len個字節到字節數組輸出流對象中
- 9. 數據輸出流(DataOutputStream)
- 向輸出流中寫Java的基本數據類型,寫入的數據和方式是可以移植的,它們能用對應的DataInputStream類讀取,
- 它的構造方法如下:
- public DataOutputStream(OutputStream out) out是一個OutputStream對象,所以必須創建一個OutputStream對象
- DataOutputStream提供了多個方法來輸出各種類型的數據,除了一般輸出流具有的void write
- 還有以下方法:
- public final void writeBoolean(boolean v)
- public final void writeByte(int v)
- public final void writeChar(int v)
- public final void writeInt(int v)
- public final void writeDouble(double v)
- public final void writeBytes(String s)
- public final void writeUTF(String str)//寫的是一個採用UTF_8編碼的字符串,使得字符串數據獨立於平臺
- 例子如下:
- import java.io.*;
- public class javadir{
- public static void main(String args[]){
- double e=2.7;
- int i=8;
- boolean ok=true;
- char cc='A';
- String s="2010 expo";
- try{
- FileOutputStream fs_out=new FileOutputStream("t.txt");
- DataOutputStream out=new DataOutputStream(fs_out);
- out.writeDouble(e);
- out.writeInt(i);
- out.writeBoolean(ok);
- out.writeChar(cc);
- out.writeUTF(s);
- out.close();
- }catch(FileNotFoundException fe){
- System.out.println(fe);
- }catch(IOException ioe){
- System.out.println(ioe);
- }
- }
- }
- 數據輸入流(DataInputStream)
- 這個類已與機器無關的方式從一個流中讀取java的基本類型,所讀的數據應是DataOutputStream對象所寫入的
- 它的構造方法如下:
- public DataOutputStream(InputStream) 此構造方法需要一個底層的輸入對象作爲參數,如FileInputStream的對象
- 該類除了int read()等方法外,還有一些直接讀取基本類型的成員方法
- public final int skipBytes(int n)
- public final boolean readBoolean()
- public final byte readByte()
- public final char readChar()
- public final int readInt()
- public final double readDouble()
- public final String readString()
- 例子如下:
- import java.io.*;
- public class javadir{
- public static void main(String args[]){
- try{
- FileInputStream fs_in=new FileInputStream("t.txt");
- DataInputStream in=new DataInputStream(fs_in);
- double e=in.readDouble();System.out.println(e);
- int i=in.readInt();System.out.println(i);
- boolean ok=in.readBoolean();System.out.println(ok);
- char cc=in.readChar();System.out.println(cc);
- String s=in.readUTF();System.out.println(s);
- in.close();
- }catch(FileNotFoundException fd){
- System.out.println(fd);
- }catch(IOException ioe){
- System.out.println(ioe);
- }
- }
- }
- 11. 緩存輸出流(BufferedOutputStream)
- 此類增強了輸出流的批量數據輸出能力,它與另一個輸出流相連,將它作爲自己的輸入,
- 它可以從與它相連的輸出流中填充數據到內部緩存,一次輸出
- 它的構造方法如下:
- public BufferedOutputStream(OutputStream out)
- public BufferedOutputStream(OutputStream out,int len) len是緩存大小
- 其主要方法除了 void write(int b)等 還有一個常用的方法,void flush() throws IOException該方法清空流並強制將緩存區的數據寫入到流中
- 例子如下:
- import java.io.*;
- public class javadir{
- public static void main(String args[]){
- try{
- long start=System.currentTimeMillis();
- FileOutputStream fs_out=new FileOutputStream("t.txt");
- BufferedOutputStream bfs_out=new BufferedOutputStream(fs_out);
- DataOutputStream out=new DataOutputStream(bfs_out);
- for(int i=0;i<10000;i++){
- out.writeDouble(Math.random());
- }
- out.close();
- long stop=System.currentTimeMillis();
- System.out.println("時間差爲:"+(stop-start));
- }catch(FileNotFoundException fd){
- System.out.println(fd);
- }catch(IOException ioe){
- System.out.println(ioe);
- }
- }
- }
- 緩存輸入流(BufferedInputStream)
- 此類增強了輸入流的批量數據處理能力,它與另一個輸入流相連,將它作爲自己的輸入,當讀物==讀取或跳過流中的字節時,
- 就會從與它相連的輸入流中在填充數據到內部緩存
- 它的構造方法如下:
- public BufferedInputStream(InputStream in) public BufferedInputStream(InputStream in,int len)
- 10. 格式化輸出流(PrintStream)
- 它的常用構造方法如下:
- PrintStream(OutputStream out) PrintStream(OutputStream out,boolean flag) flag是個清理標識,flag爲true是,當換行符'\n'出現在輸出流中,PrintStream會自動輸出流中的數據;
- flag 爲false 時,當換行符'\n'出現在輸出流中,PrintStream則不會自動輸出流中的數據
- 它有如下形式的write() 方法; public void write(int b) public void write(byte b[],int off,int len)
- 例子如下:
- import java.io.*;
- public class javadir{
- public static void main(String args[]) throws Exception{
- PrintStream outfile=new PrintStream(new FileOutputStream("t.txt"));
- outfile.println("sin 30 degree="+Math.sin(Math.sin(Math.PI/6)));
- System.out.println("sin 30 degree="+Math.sin(Math.sin(Math.PI/6)));
- outfile.close();
- }
- }
- 11. 字符集輸入流(Reader)
- 此方法以一個字符型數組作爲參數,可用於一次讀取多個字符,讀入字符直接放入數組中,並返回讀取的字符數
- 該方法類似於上一中read方法,不同的是設置了偏移量off 。這裏的偏移量指的是可以從字符型數組的第off個位置起,讀取len個字符
- 這個方法還可以用於防止數組越界,其用法是off設置成0,len設置成數組長度
- long skip(long n) throws IOException 跳過指定的字符數
- Synchronized void mark(int readlimit) 在字符流中標記一個位置
- Synchronized void reset() 重新設置到上一次標記的位置
- public void close()
- boolean markSupport()
- 字符集輸出流(Writer)
- 主要方法如下:
- write(int c) 向Write流對象中寫入一個字符
- write(char[] cc) 向流對象中寫入一個字符數組
- write(char[] cc,int off,int len) 從數組的第off個位置,寫入len個字符長度到流對象中
- write(String s) 向流對象中寫入一個字符串
- write(String str,int off,int len) 字符串的第off個位置,寫入len個字符
- void close()
- public void flush() throws IOException
- 上述兩個類都是最高層的類,一般並不直接使用它們
- FileReader類
- 此類可以方便的將一個文件連接到其他需要以Reader作爲輸入參數的類上
- 其構造方法如下:
- FileReader(String filename)
- FileReader(File file)
- FileReader(FileDescript fd)
- FileReader可用於讀取字符文件中的內容,但一次只能讀取一個字符,所以通常將FileReader對象內置於BufferedReader對象中
- BufferedReader類
- 從字符輸入流中讀取文本,在必要時將字符存入緩存
- 其構造方法如下所示:
- public BufferedReader(Reader in) public BufferedReader(Reader in,int len)
- 此類除了具有Reader類的一般方法外,還有以下方法
- public String readLine() throws IOException 該方法讀取一行文字,到達最後返回null
- import java.io.*;
- public class javadir{
- public static void main(String args[]) throws IOException{
- FileReader fr=new FileReader("t.class");
- BufferedReader br=new BufferedReader(fr);
- String s;
- while((s=br.readLine())!=null) System.out.println(s);
- fr.close();
- }
- }
- 以上兩個類合起來用
- FileWrite類
- 此類把字符輸入流與文件連接起來
- 它的構造方法如下:
- FileWrite(File file)
- FileWrite(String filename)
- FileWrite(String filename,boolean append)
- BufferedWrite類
- 此類用來創建一個字符緩存輸入流,它主要爲其他類 如PrintWrite提供一個字符輸入流
- 它的構造方法如下:
- public BufferedWrite(Writer out)
- public BufferedWrite(Write out,int len)
- 此類除了提供了Writer類具有的方法外,還提供瞭如下方法:
- void newLine() 插入一個新行符
- void flush() 強迫刷新
- PrintWrite類
- 此類爲格式化字符流提供了一些實用的方法,它最好還是與其他輸出流一起使用,提高流的效率
- 它的構造方法如下: