摘要:一、file類的常用操作File file=new File("E:\\test\\javaIo"); System.out.println(file.isDirec
一、file類的常用操作
File file=new File("E:\\test\\javaIo"); System.out.println(file.isDirectory());//判斷該文件是否是目錄(如果該文件不存在也返回false) System.out.println(file.isFile());//判斷文件是否是具體的文件 System.out.println(file.exists());//判斷文件是否存在 System.out.println(file.mkdir());//創(chuàng)建目錄 返回boolean類型 System.out.println(file.delete());//刪除文件或目錄 返回boolean類型 System.out.println(file.mkdirs());//創(chuàng)建多級目錄 返回boolean類型 File fileF=new File("E:\\test\\javaIo","1.txt"); fileF.createNewFile();//創(chuàng)建文件 fileF.delete();//刪除文件 System.out.println(file.getAbsolutePath());//獲取文件的絕對路徑 E:\test\javaIo System.out.println(file.getName());//獲取文件名字 javaIo System.out.println(file.getParent());//獲取父文件路徑 E:\test System.out.println(file.getParentFile());//獲取父文件對象
/** * 列出 指定目錄下(包括子目錄)的所有文件 * @param file */ public static void directoryList(File dir){ if(!dir.exists()){ throw new IllegalArgumentException("目錄"+dir+"不存在"); } if(!dir.isDirectory()){ throw new IllegalArgumentException(dir+"不是目錄"); } //String[] file=dir.list(); //獲取該目錄下所有目錄和文件的名字 (返回字符串數(shù)組) File [] files=dir.listFiles();// 獲取 該目錄下所有目錄和文件 的file對象 if(files!=null && files.length>0){ for (File file : files) { if(file.isDirectory()){ //如果是目錄就做遞歸操作 directoryList(file); }else{ System.out.println(file.getName()); } } } }
二、字節(jié)流與字符流
字節(jié)流:字節(jié)流處理單元為1個字節(jié),操作字節(jié)和字節(jié)數(shù)組,主要處理二進制數(shù)據(jù)所以字節(jié)流可用于處理任何類型的對象。字節(jié)流操作的是文件本身(當我們對文件進行讀寫操作時如果不調(diào)用close() 或 flush()方法時能看到數(shù)據(jù)的變化)。字節(jié)流主要是操作byte類型數(shù)據(jù),以byte數(shù)組為準,主要操作類就是OutputStream、InputStream。
按照流的作用或者流向分又可分為讀寫流(輸入輸出流):讀(輸入)流 InputStream;寫(輸出流) OutputStream。
輸入(讀):將磁盤(文件)中的數(shù)據(jù)讀入內(nèi)存中。
輸出(寫):將內(nèi)存中的數(shù)據(jù)寫入磁盤(文件)中。
字符流:字符流處理的單元為2個字節(jié)的Unicode字符,分別操作字符、字符數(shù)組或字符串。字符流是由Java虛擬機將字節(jié)轉(zhuǎn)化為2個字節(jié)的Unicode字符為單位的字符而成的。字符流操作的是緩沖區(qū)(當我們對文件進行讀寫操作時如果不調(diào)用close() 或 flush()方法時不能看到數(shù)據(jù)的變化)。
注意: 所有文件的儲存是都是字節(jié)(byte)的儲存,在磁盤上保留的并不是文件的字符而是先把字符編碼成字節(jié),再儲存這些字節(jié)到磁盤。在讀取文件(特別是文本文件)時,也是一個字節(jié)一個字節(jié)地讀取以形成字節(jié)序列。
三、字節(jié)輸入流(InputStream)
輸入:將磁盤中的數(shù)據(jù)讀入內(nèi)存中
InputStream 抽象類是表示字節(jié)輸入流的所有類的超類。需要定義 InputStream 的子類的應用程序必須始終提供返回下一個輸入字節(jié)的方法。
public abstract class InputStream implements Closeable { private static final int MAX_SKIP_BUFFER_SIZE = 2048;//最大緩沖區(qū)大小 //讀取一個字節(jié)的數(shù)據(jù),并且返回讀到得數(shù)據(jù),如果返回-1,則表示讀到輸入流的末尾 public abstract int read() throws IOException; //從輸入流中讀取一定量的字節(jié),并將其存儲在字節(jié)數(shù)組b中,返回實際讀取的字節(jié)數(shù),如果返回-1,則表示讀到輸入流的末尾 public int read(byte b[]) throws IOException { return read(b, 0, b.length); } //將數(shù)據(jù)讀入一個字節(jié)數(shù)組,同時返回讀取的實際字節(jié)數(shù),如果返回-1,則表示讀到輸入流的末尾。off指定在數(shù)組b中存放數(shù)據(jù)的起始偏移位置,len指定讀取的最大字節(jié)數(shù) public int read(byte b[], int off, int len) throws IOException { } //跳過和放棄此輸入流中的 n 個數(shù)據(jù)字節(jié)。 public long skip(long n) throws IOException { } //返回此輸入流下一個方法調(diào)用可以不受阻塞地從此輸入流讀取或跳過的估計字節(jié)數(shù)。 public int available() throws IOException { return 0; } //關閉此輸入流并釋放與該流關聯(lián)的所有系統(tǒng)資源。 public void close() throws IOException {} //在此輸入流中標記當前的位置。 public synchronized void mark(int readlimit) {} //將此流重新定位到對此輸入流最后調(diào)用 mark 方法時的位置。 public synchronized void reset() throws IOException { throw new IOException("mark/reset not supported"); } //測試此輸入流是否支持 mark 和 reset 方法。 public boolean markSupported() { return false; } }
FileInputStream 讀取文件內(nèi)容:
public class InputStreamTest { public static void main(String[] args) throws IOException { readOne(); readByte(); } //public abstract int read() throws IOException //從輸入流中讀取數(shù)據(jù)的下一個字節(jié)。返回 0 到 255 范圍內(nèi)的 int 字節(jié)值。如果因為已經(jīng)到達流末尾而沒有可用的字節(jié),則返回值 -1。 //在輸入數(shù)據(jù)可用、檢測到流末尾或者拋出異常前,此方法一直阻塞。 public static void readOne() throws IOException{ InputStream is=new FileInputStream("E:\\javaTest\\1.txt"); int by=0; while((by=is.read())!=-1){ System.out.println((char)by);//轉(zhuǎn)為char類型 } is.close(); } //public int read(byte[] b)throws IOException //從輸入流中讀取一定數(shù)量的字節(jié),并將其存儲在緩沖區(qū)數(shù)組 b 中。以整數(shù)形式返回實際讀取的字節(jié)數(shù)。 //在輸入數(shù)據(jù)可用、檢測到文件末尾或者拋出異常前,此方法一直阻塞。 public static void readByte() throws IOException{ InputStream is=new FileInputStream("E:\\javaTest\\1.txt"); byte []bytes=new byte[1024]; int len=0; while((len=is.read(bytes))!=-1){ System.out.println(new String(bytes,0,len)); } is.close(); } }
BufferedInputStream 字節(jié)輸入(讀)緩沖流
字節(jié)流一字讀寫一個數(shù)組的速度明顯比一次讀寫一個字節(jié)的速度快,這是因為加入了數(shù)組緩沖的效果,java設計的本身也考慮到了這種情況(裝飾設計模式)所以提供了字節(jié)緩沖流(BufferedInputStream)。該流僅提供緩沖區(qū),是為提高讀寫效率而設計的。真正操作文件還是需要基本流對象來實現(xiàn)。BufferedInputStream 是 InputStream 的子類具有一切父類的方法。
public static void bufferedInputStreamTest() throws IOException{ BufferedInputStream bis=new BufferedInputStream(new FileInputStream("E:\\javaTest\\5.txt")); byte [] bytes=new byte[1024]; int len=0; while((len=bis.read(bytes))!=-1){ System.out.println(new String(bytes,0,len)); } bis.close(); }
四、字節(jié)輸出流(OutputStream)
輸出:將內(nèi)存中的數(shù)據(jù)寫入磁盤文件中
OutputStream類是Java IO API中所有輸出流的基類,主要是將內(nèi)存中的數(shù)據(jù)輸入到目標媒介中。
public abstract class OutputStream implements Closeable, Flushable { //寫入一個字節(jié)到stream中 public abstract void write(int b) throws IOException; //把字節(jié)數(shù)組中所有數(shù)據(jù)寫入到輸出流中 public void write(byte b[]) throws IOException { write(b, 0, b.length); } //把字節(jié)數(shù)據(jù)中從offset位置開始,length個字節(jié)的數(shù)據(jù)寫入到輸出流。 public void write(byte b[], int off, int len) throws IOException { } //方法將所有寫入到OutputStream的數(shù)據(jù)沖刷到相應的目標媒介中,即使所有數(shù)據(jù)都寫入到了FileOutputStream,這些數(shù)據(jù)還是有可能保留在內(nèi)存的緩沖區(qū)中。通過調(diào)用flush()方法,可以把緩沖區(qū)內(nèi)的數(shù)據(jù)刷新到磁盤 public void flush() throws IOException { } //結束數(shù)據(jù)寫入時,需要關閉OutputStream public void close() throws IOException { } }
FileOutputStream 將數(shù)據(jù)寫入文件
夠造函數(shù):FileOutputStream(String name, boolean append) 創(chuàng)建一個向具有指定 name 的文件中寫入數(shù)據(jù)的輸出文件流。 (在文件末尾追加寫入)
FileOutputStream(String name) 創(chuàng)建一個向具有指定名稱的文件中寫入數(shù)據(jù)的輸出文件流。(在文件開頭重新寫入)
//write(byte[] b) 將 b.length 個字節(jié)從指定的 byte 數(shù)組寫入此輸出流 public static void writeByte() throws IOException{ OutputStream os=new FileOutputStream("E:\\javaTest\\1.txt"); String str="中國"; os.write(str.getBytes()); //flush() 刷新此輸出流并強制寫出所有緩沖的輸出字節(jié)。 os.flush(); //close() 1、將流對象變?yōu)槔阌趈vm垃圾回收機制回收 2、通知系統(tǒng)釋放與該文件相關的資源 os.close(); } //write(int b) 將指定的字節(jié)寫入此輸出流。 public static void writeInt() throws IOException{ OutputStream os=new FileOutputStream("E:\\javaTest\\1.txt"); //我們都知道計算機磁盤存入的是二進制數(shù)據(jù),在這里將97當作底層的二進制數(shù)據(jù)寫入磁盤,當我們通過記事本打開文件時, //該文件會根據(jù)ASCII字碼表進行解碼所以我們看到的是字符"a" os.write(97); os.flush(); os.close(); } //write(byte[] b, int off, int len) 將指定 byte 數(shù)組中從偏移量 off 開始的 len 個字節(jié)寫入此輸出流。 public static void writeOffLen() throws IOException{ OutputStream os=new FileOutputStream("E:\\test\\javaIo\\1.txt"); byte [] bytes="中國".getBytes(); os.write(bytes, 0, bytes.length); os.flush(); os.close(); }
BufferedOutputStream 字節(jié)輸出(寫)緩沖流
字節(jié)流一字讀寫一個數(shù)組的速度明顯比一次讀寫一個字節(jié)的速度快,這是因為加入了數(shù)組緩沖的效果,java設計的本身也考慮到了這種情況(裝飾設計模式)所以提供了字節(jié)緩沖流(BufferedOutputStream)。該流僅提供緩沖區(qū),是為提高讀寫效率而設計的。真正操作文件還是需要基本流對象來實現(xiàn)。BufferedOutputStream是 OutputStream的子類具有一切父類的方法。
public static void bufferOutPutStreamTest() throws IOException{ BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream("E:\\javaTest\\5.txt")); bos.write("中".getBytes()); bos.flush(); bos.close(); }
五、字節(jié)輸入流與字節(jié)輸出流的使用
使用基礎字節(jié)流實現(xiàn)文件copy:
public static void readWriter() throws IOException{ String readFileName="D:"+File.separator+"html"+File.separator+"1.txt"; String writerFileName="D:"+File.separator+"html"+File.separator+"2.txt"; InputStream is=new FileInputStream(new File(readFileName));// OutputStream out=new FileOutputStream(new File(writerFileName)); byte [] b=new byte[1024]; int len=0; while((len=is.read(b))!=-1){ out.write(b, 0, len); } is.close(); out.close(); }
使用字節(jié)緩沖流實現(xiàn)文件的copy:
public static void BufferFileCopy() throws IOException{ BufferedInputStream bis=new BufferedInputStream(new FileInputStream("E:\\javaTest\\5.txt")); BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream("E:\\javaTest\\6.txt")); byte[]bytes=new byte[1024]; int len=0; while((len=bis.read(bytes))!=-1){ bos.write(bytes, 0, len); } bos.flush(); bos.close(); bis.close(); }