IO流簡述

IO體系圖

art

File類

  • 在java程序中,對磁盤文件進行描述的類
  • 通常使用File類的構造方法
File類文件屬性方法
static String pathseparator:與系統有關的路徑分隔符,爲了方便,它被表示爲一個字符串(;)
static char pathSeparatorChar;
static String Separator:與系統有關的默認名稱分隔符,爲了方便,它被表示爲一個字符串(\)
static char Separator
  • 實例
package waking.test.io;

import java.io.File;

/**
 * file類
 * @author waking
 *
 */
public class Demo01 {
	public static void main(String[] args) {
		//;
		System.out.println(File.pathSeparator);
		System.out.println(File.pathSeparatorChar);
		
		//\
		System.out.println(File.separator);
		System.out.println(File.separatorChar);
		
	}
}
常用方法

返回值 方法名/描述
boolean canExecute()測試應用程序是否可以執行此抽象路徑名錶示的文件
boolean canRead()測試應用程序是否可以讀取此抽象路徑名錶示的文件
boolean canWrite()測試應用程序是否可以修改此抽象路徑名錶示的文件
int compareTo(File)按字母順序比較兩個抽象路徑名
boolean exists()測試此抽象路徑名錶示的文件或目錄是否存在
boolean createNewFile()當且僅當不存在具有此路徑名指定的文件時,創建文件
boolean delete()刪除此抽象路徑名錶示的文件或目錄
File getAbsoluteFile()返回此抽象路徑名的絕對路徑名形式
String getName()返回由此抽象路徑名錶示的文件或目錄的名稱
String getParent()返回此抽象路徑名父路徑名字符串,如果此路徑名沒有指定父目錄,則返回null
String getPath()將此抽象路徑名裝換爲一個路徑名字符串
boolean isFile()測試此抽象路徑名錶示的文件是否是一個標準文件
boolean isDirectory()測試此抽象路徑名錶示的文件是否是一個目錄
boolean ishidden()測試此抽象路徑名指定的文件是否是一個隱藏文件
String[] list()返回一個字符串數組,這些字符串指定此抽象路徑名錶示的目錄中的文件和目錄
boolean mkdir()創建此抽象路徑名指定的目錄
boolean renameTo(File dest)重新命名此抽象路徑名錶示的文件
File[] listFiles()返回一個抽象路徑名數組,這些路徑名錶示此抽象路徑名錶示的目錄的文件
  • 代碼示例
  • 遞歸查詢文件和文件目錄
package waking.test.io;

import java.io.File;

/**
 * File類查詢文件和文件路徑
 * @author waking
 *
 */
public class Demo02 {
	public static void main(String[] args) {
		File file = new File("E:\\diary\\1909java");
		isFile(file);
	}
	public static void isFile(File f) {
		if(f.isFile()) {
			System.out.println(f.getPath());
		}else {
			File[] listFiles = f.listFiles();
			for (int i = 0; i < listFiles.length; i++) {
				isFile(listFiles[i]);
			}
			
		}
	}
}


  • 其它的方法您可以簡單試試

IO流

  • 在工作中,經常回去操作磁盤上的資源這個過程中實現了數據的輸入和輸出操作,磁盤上的文件和內存之間進行交互,數據的交互需要有一個媒介或者管道,把這個媒介或者管道就稱爲IO流,也被稱爲輸入輸出流
流的作用和原理
  • 流是一組有順序的,有起點和終點的字節集合,是對數據傳輸的總稱或抽象。
  • 即數據在兩設備間的傳輸稱爲流,流的本質是數據傳輸,根據數據傳輸特性將流抽象爲各種類,方便更直觀的進行數據操作
IO流的種類
  • 按照流的流向分:輸入流,輸出流
輸入流:表示將數據讀取到java程序(內存)中使用的流
輸出流:表示從java程序(內存)向外傳輸使用的流
  • 按照數據單位分:字符流、字節流
字節流:一次性傳輸一個字節數據,將數據已字節的形式傳輸
字符流:一次性傳輸一個字符數據,將數據以字符的形式傳輸
  • 按照層次分:節點流,處理流
節點流:可以從或向一個特定的地方(節點)讀寫數據
處理流:是對一個已存在的流的連接和封裝,通過所封裝的流的功能調用實現數據讀寫
字節輸入流
  • InputStream
    • 是一個抽象類,不能實例化對象

方法名 描述
void close() 關閉此輸入流並釋放與該流關聯的所有系統資源
int read() 從輸入流中讀取數據的下一個字節
int read(byte[] b) 從輸入流中讀取一定數量的字節,並將其存儲在緩衝區數組b中
int read(byte[] b,int off,int len) 將輸入流中最多len個數據字節讀入byte數組
package waking.test.io;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * InputStream
 * @author waking
 *
 */
public class Demo03 {
	public static void main(String[] args) {
		InputStream is =null;
		try {
			is = new FileInputStream(new File("E:\\diary\\1909java\\預習\\javaEE\\Java8.pdf"));
			byte[] b = new byte[1024];
			int len = -1;
			while((len=is.read(b))!=-1) {
				
				System.out.println(new String(b, 0, len));
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			if(is!=null) {
				try {
					is.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
}

字節輸出流
  • OutputStream
  • 是個抽象類,不能實例化對象

方法名 描述
void close() 關閉此輸出流並釋放與此流有關的所有系統資源
void flush() 刷新此輸出流並強制寫出所有緩衝的輸出字節
void write(byte[] b) 將b.length個字節從指定的byte數組寫入此輸出流
  • 示例
package waking.test.io;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

/**
 * OutputStream
 * @author waking
 *
 */
public class Demo04 {
	public static void main(String[] args) {
		OutputStream os=null;
		try {
			 os = new FileOutputStream(new File("E://a.txt"));
			String s = "waking";
			os.write(s.getBytes(), 0,s.length());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			if(os!=null) {
				try {
					os.flush();
					os.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
}
  • 文件的複製
package waking.test.io;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * 字節流的文件的複製
 * @author 1
 *
 */
public class Demo05 {
	public static void main(String[] args) {
		//字節輸入流
		InputStream is = null;
		//字節輸出流
		OutputStream os = null;
		try {
			is=new FileInputStream(new File("E:\\\\diary\\\\1909java\\\\預習\\\\javaEE\\\\Java8.pdf"));
			os = new FileOutputStream(new File("E://wakingjava8.pdf"));
			byte[] b = new byte[1024*8];
			int len = -1;
			while((len=is.read(b))!=-1) {
				os.write(b, 0, len);
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			try {
				if(is!=null) {
					is.close();
				}
				if(os!=null) {
					os.flush();
					os.close();
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

}
字符輸入流 Reader
  • 是所有字符輸入流的父類,爲一個抽象類,不能實例化對象,使用它的子類FileReader類
  • 常用方法和以上方法類似
  • 示例
package waking.test.io;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;

/**
 * 字符輸入流Reader
 * @author waking
 *
 */
public class Demo06 {
	public static void main(String[] args) {
		//字符輸入流
		Reader r=null;
		try {
			r = new FileReader(new File("E://a.txt"));
			char[] c = new char[1];
			int len = -1;
			while((len=r.read(c))!=-1) {
				System.out.println(new String(c, 0, len));
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			if(r!=null) {
				try {
					r.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
}
字符輸出流Writer
  • Writer:是所有字符輸出流的父類,爲一個抽象類,不能實例化對象,使用它的子類FileWriter
  • 常用方法和以上類似就不一一介紹
  • 示例
package waking.test.io;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;

/**
 * 字符輸出流Writer
 * @author waking
 *
 */
public class Demo07 {
	public static void main(String[] args) {
		Writer w = null;
		try {
			w= new FileWriter(new File("E://aa.txt"));
			String c = "waking to love";
			w.write(c.toCharArray(), 0, c.length());
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			try {
				if(w!=null) {
					w.flush();
					w.close();
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}
字節字符轉換輸入流InputstreamReader
  • 將字節轉換成字符輸入流
  • 示例
package waking.test.io;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;

/**
 * 字節字符轉換輸入流
 * @author waking
 *
 */
public class Demo08 {
	public static void main(String[] args) {
		InputStreamReader isr = null;
		try {
			isr = new InputStreamReader(new FileInputStream(new File("E://a.txt")),"utf-8");
			char[] c =new char[1024];
			int len = -1;
			while((len=isr.read(c))!=-1) {
				System.out.println(new String(c, 0, len));
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			if(isr!=null) {
				try {
					isr.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
}
字節字符輸出流OutputStreamWriter
  • 字節轉換字符輸出流
  • 示例
package waking.test.io;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;

/**
 * 字節字符輸入流
 * 複製
 * @author waking
 *
 */
public class Demo09 {
	public static void main(String[] args) {
		OutputStreamWriter osw = null;
		InputStreamReader isr = null;
		try {
			isr = new InputStreamReader(new FileInputStream(new File("E://a.txt")),"utf-8");
			osw = new OutputStreamWriter(new FileOutputStream(new File("E://b.txt")),"utf-8");
			char[] c =new char[1024*8];
			int len = -1;
			while((len=isr.read(c))!=-1) {
				osw.write(c, 0, len);
			}
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			try {
				if(isr!=null) {
					isr.close();
				}
				if(osw!=null) {
					osw.flush();
					osw.close();
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}
緩衝輸入流BufferedInputStream
  • 作用:主要是爲了增強基礎流的功能而存在,提高了流的工作效率(讀寫效率)
  • 示例
package waking.test.io;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

/**
 * 緩衝輸入流
 * @author waking
 *
 */
public class Demo10 {
	public static void main(String[] args) {
		BufferedInputStream bis = null;
		try {
			bis = new BufferedInputStream(new FileInputStream(new File("E://a.txt")));
			byte[] b =new byte[1024];
			int len = -1;
			while((len=bis.read(b))!=-1) {
				System.out.println(new String(b, 0, len));
			}
			
			
			} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
		}finally {
			if(bis!=null) {
				try {
					bis.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
}
緩衝輸出流BufferedOutputStream
  • 複製示例
package waking.test.io;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * 緩衝字節輸出流
 * @author waking
 *
 */
public class Demo11 {
	public static void main(String[] args) {
		BufferedOutputStream bos = null;
		BufferedInputStream bis = null;
		try {
			bis = new BufferedInputStream(new FileInputStream(new File("E://a.txt")));
			bos = new BufferedOutputStream(new FileOutputStream(new File("E://c.txt")));
			byte[] b = new byte[1024];
			int len =-1;
			while((len=bis.read(b))!=-1) {
				bos.write(b, 0, len);
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			try {
				if(bos!=null) {
					bos.flush();
					bos.close();
				}
				if(bis!=null) {
					bis.close();
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}
  • 緩衝字符輸入輸出流和緩衝字節輸入輸出流類似,區別在於對字符的操作
內存流ByteArrayInputStream
  • 將字節寫到內存中,是InputStream的子類
  • 示例
package waking.test.io;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

/**
 * 內存流ByteArrayInputStream
 * ByteArrayOutputStream
 * 大小寫轉換
 * @author waking
 *
 */
public class Demo12 {
	public static void main(String[] args) throws IOException {
		ByteArrayInputStream bais = null;
		ByteArrayOutputStream baos = null;
		baos = new ByteArrayOutputStream();
		bais = new ByteArrayInputStream("waking".getBytes());
		int len = -1;
		while((len = bais.read())!=-1) {
			char c = (char)len;
			baos.write(Character.toUpperCase(c));
		}
		
		String bao = baos.toString();
		baos.close();
		bais.close();
		
		System.out.println(bao);
				
	}
}
標準輸入輸出流
  • System.in
  • System.out
package waking.test.io;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintStream;

/**
 * 標準輸入輸出流
 * @author waking
 *
 */
public class Demo13 {
	public static void main(String[] args) {
		PrintStream ps = null;
		try {
			ps = new PrintStream(new File("E:/d.txt"));
			System.setOut(ps);
			
			System.out.println("waking");
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			if(ps!=null) {
				ps.close();
			}
		}
	}
}
package waking.test.io;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Scanner;

/**
 * 標準輸入輸出流
 * @author waking
 *
 */
public class Demo14 {
	public static void main(String[] args) {
		try {
			FileInputStream fis = new FileInputStream(new File("E://a.txt"));
			
			System.setIn(fis);
			
			Scanner sc = new Scanner(System.in);
			String line = sc.nextLine();
			
			System.out.println(line);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
				
	}
}
IO流就介紹到這裏,後面還用序列化和反序列化,對象流,RandomAccessFile等,在下一個博客介紹
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章