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());//創建目錄 返回boolean類型
System.out.println(file.delete());//刪除文件或目錄 返回boolean類型
System.out.println(file.mkdirs());//創建多級目錄 返回boolean類型
File fileF=new File("E:\\test\\javaIo","1.txt");
fileF.createNewFile();//創建文件
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(); //獲取該目錄下所有目錄和文件的名字 (返回字符串數組)
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());
}
}
}
}
字節流:字節流處理單元為1個字節,操作字節和字節數組,主要處理二進制數據所以字節流可用於處理任何類型的對象。字節流操作的是文件本身(當我們對文件進行讀寫操作時如果不調用close() 或 flush()方法時能看到數據的變化)。字節流主要是操作byte類型數據,以byte數組為准,主要操作類就是OutputStream、InputStream。
按照流的作用或者流向分又可分為讀寫流(輸入輸出流):讀(輸入)流 InputStream;寫(輸出流) OutputStream。
輸入(讀):將磁盤(文件)中的數據讀入內存中。
輸出(寫):將內存中的數據寫入磁盤(文件)中。
字符流:字符流處理的單元為2個字節的Unicode字符,分別操作字符、字符數組或字符串。字符流是由Java虛擬機將字節轉化為2個字節的Unicode字符為單位的字符而成的。字符流操作的是緩沖區(當我們對文件進行讀寫操作時如果不調用close() 或 flush()方法時不能看到數據的變化)。
注意: 所有文件的儲存是都是字節(byte)的儲存,在磁盤上保留的並不是文件的字符而是先把字符編碼成字節,再儲存這些字節到磁盤。在讀取文件(特別是文本文件)時,也是一個字節一個字節地讀取以形成字節序列。
輸入:將磁盤中的數據讀入內存中

InputStream 抽象類是表示字節輸入流的所有類的超類。需要定義 InputStream 的子類的應用程序必須始終提供返回下一個輸入字節的方法。
public abstract class InputStream implements Closeable {
private static final int MAX_SKIP_BUFFER_SIZE = 2048;//最大緩沖區大小
//讀取一個字節的數據,並且返回讀到得數據,如果返回-1,則表示讀到輸入流的末尾
public abstract int read() throws IOException;
//從輸入流中讀取一定量的字節,並將其存儲在字節數組b中,返回實際讀取的字節數,如果返回-1,則表示讀到輸入流的末尾
public int read(byte b[]) throws IOException {
return read(b, 0, b.length);
}
//將數據讀入一個字節數組,同時返回讀取的實際字節數,如果返回-1,則表示讀到輸入流的末尾。off指定在數組b中存放數據的起始偏移位置,len指定讀取的最大字節數
public int read(byte b[], int off, int len) throws IOException {
}
//跳過和放棄此輸入流中的 n 個數據字節。
public long skip(long n) throws IOException {
}
//返回此輸入流下一個方法調用可以不受阻塞地從此輸入流讀取或跳過的估計字節數。
public int available() throws IOException {
return 0;
}
//關閉此輸入流並釋放與該流關聯的所有系統資源。
public void close() throws IOException {}
//在此輸入流中標記當前的位置。
public synchronized void mark(int readlimit) {}
//將此流重新定位到對此輸入流最後調用 mark 方法時的位置。
public synchronized void reset() throws IOException {
throw new IOException("mark/reset not supported");
}
//測試此輸入流是否支持 mark 和 reset 方法。
public boolean markSupported() {
return false;
}
}
FileInputStream 讀取文件內容:

BufferedInputStream 字節輸入(讀)緩沖流
字節流一字讀寫一個數組的速度明顯比一次讀寫一個字節的速度快,這是因為加入了數組緩沖的效果,java設計的本身也考慮到了這種情況(裝飾設計模式)所以提供了字節緩沖流(BufferedInputStream)。該流僅提供緩沖區,是為提高讀寫效率而設計的。真正操作文件還是需要基本流對象來實現。BufferedInputStream 是 InputStream 的子類具有一切父類的方法。
1 public static void bufferedInputStreamTest() throws IOException{
2 BufferedInputStream bis=new BufferedInputStream(new FileInputStream("E:\\javaTest\\5.txt"));
3 byte [] bytes=new byte[1024];
4 int len=0;
5 while((len=bis.read(bytes))!=-1){
6 System.out.println(new String(bytes,0,len));
7 }
8 bis.close();
9 }
輸出:將內存中的數據寫入磁盤文件中

OutputStream類是Java IO API中所有輸出流的基類,主要是將內存中的數據輸入到目標媒介中。
public abstract class OutputStream implements Closeable, Flushable {
//寫入一個字節到stream中
public abstract void write(int b) throws IOException;
//把字節數組中所有數據寫入到輸出流中
public void write(byte b[]) throws IOException {
write(b, 0, b.length);
}
//把字節數據中從offset位置開始,length個字節的數據寫入到輸出流。
public void write(byte b[], int off, int len) throws IOException {
}
//方法將所有寫入到OutputStream的數據沖刷到相應的目標媒介中,即使所有數據都寫入到了FileOutputStream,這些數據還是有可能保留在內存的緩沖區中。通過調用flush()方法,可以把緩沖區內的數據刷新到磁盤
public void flush() throws IOException {
}
//結束數據寫入時,需要關閉OutputStream
public void close() throws IOException {
}
}
FileOutputStream 將數據寫入文件
夠造函數:FileOutputStream(String name, boolean append) 創建一個向具有指定 name 的文件中寫入數據的輸出文件流。 (在文件末尾追加寫入)
FileOutputStream(String name) 創建一個向具有指定名稱的文件中寫入數據的輸出文件流。(在文件開頭重新寫入)

BufferedOutputStream 字節輸出(寫)緩沖流
字節流一字讀寫一個數組的速度明顯比一次讀寫一個字節的速度快,這是因為加入了數組緩沖的效果,java設計的本身也考慮到了這種情況(裝飾設計模式)所以提供了字節緩沖流(BufferedOutputStream)。該流僅提供緩沖區,是為提高讀寫效率而設計的。真正操作文件還是需要基本流對象來實現。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();
}
使用基礎字節流實現文件copy:
1 public static void readWriter() throws IOException{
2 String readFileName="D:"+File.separator+"html"+File.separator+"1.txt";
3 String writerFileName="D:"+File.separator+"html"+File.separator+"2.txt";
4 InputStream is=new FileInputStream(new File(readFileName));//
5 OutputStream out=new FileOutputStream(new File(writerFileName));
6 byte [] b=new byte[1024];
7 int len=0;
8 while((len=is.read(b))!=-1){
9 out.write(b, 0, len);
10 }
11 is.close();
12 out.close();
13 }
使用字節緩沖流實現文件的copy:
1 public static void BufferFileCopy() throws IOException{
2 BufferedInputStream bis=new BufferedInputStream(new FileInputStream("E:\\javaTest\\5.txt"));
3 BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream("E:\\javaTest\\6.txt"));
4 byte[]bytes=new byte[1024];
5 int len=0;
6 while((len=bis.read(bytes))!=-1){
7 bos.write(bytes, 0, len);
8 }
9 bos.flush();
10 bos.close();
11 bis.close();
12 }