天天看點

java學習之位元組流--InputStream和OutputStream

位元組流:可以處理一切檔案,包括二進制 音頻,視訊, doc等

節點流: InputStream  FIleInputStream

OutputStream FileOutputStream

一、檔案的讀取

 * 1、建立聯系   File對象 源頭

2、選擇流     檔案輸入流  InputStream FileInputStream

3、操作  : byte[] car =new byte[1024];  +read+讀取大小

              輸出

4、釋放資源 :關閉

public class Demo01 {
	
	public static void main(String[] args) {
		//1、建立聯系   File對象
		File src =new File("e:/xp/test/a.txt");
		//2、選擇流
		InputStream is =null; //提升作用域
		try {
			is =new FileInputStream(src);
			//3、操作 不斷讀取 緩沖數組
			byte[] car =new byte[1024];
			int len =0; //接收 實際讀取大小
			//循環讀取
			StringBuilder sb =new StringBuilder();
			while(-1!=(len=is.read(car))){
				//輸出  位元組數組轉成字元串
				String info =new String(car,0,len);
				sb.append(info);
			}
			System.out.println(sb.toString());
			
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			System.out.println("檔案不存在");
		} catch (IOException e) {
			e.printStackTrace();
			System.out.println("讀取檔案失敗");
		}finally{
			try {
				//4、釋放資源
				if (null != is) {
					is.close();
				}
			} catch (Exception e2) {
				System.out.println("關閉檔案輸入流失敗");
			}
		}
	}

}
           

二、寫出檔案

1、建立聯系   File對象  目的地

2、選擇流     檔案輸出流  OutputStream FileOutputStream

3、操作  :  write() +flush

4、釋放資源 :關閉

public class Demo02 {

	public static void main(String[] args) {
		//1、建立聯系   File對象  目的地
		File dest =new File("e:/xp/test/test.txt");
		//2、選擇流   檔案輸出流  OutputStream FileOutputStream
		OutputStream os =null;
		//以追加形式 寫出檔案 必須為true 否則為覆寫
		try {
			os =new FileOutputStream(dest,true);
			//3、操作
			String str="bjsxt is very good \r\n";
			//字元串轉位元組數組
			byte[] data =str.getBytes();
			os.write(data,0,data.length);
			
			os.flush(); //強制重新整理出去
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			System.out.println("檔案未找到");
		} catch (IOException e) {
			e.printStackTrace();
			System.out.println("檔案寫出失敗");
		}finally{
			//4、釋放資源 :關閉
			try {
				if (null != os) {
					os.close();
				}
			} catch (Exception e2) {
				System.out.println("關閉輸出流失敗");
			}
		}
	}
}
           

三、檔案拷貝 程式為橋梁

1、建立聯系 源(存在且為檔案) + 目的地(檔案可以不存在)

2、選擇流

3. 檔案拷貝, 操作:循環+讀取+寫出

4. 釋放資源

public class CopyFileDemo {
	
	public static void main(String[] args) {
		String src ="E:/xp/test";
		String dest="e:/xp/test/4.jpg";
		try {
			copyFile(src,dest);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			System.out.println("檔案不存在");
		} catch (IOException e) {
			e.printStackTrace();
			System.out.println("拷貝檔案失敗|關閉流失敗");
		}
	}
	/**
	 * 檔案的拷貝
	 * @param  源檔案路徑
	 * @param  目錄檔案路徑
	 * @throws FileNotFoundException,IOException
	 * @return 
	 */
		public static void copyFile(String srcPath,String destPath) throws FileNotFoundException,IOException {
			//1、建立聯系 源(存在且為檔案) +目的地(檔案可以不存在)  
			File src =new File(srcPath);
			File dest =new File(destPath);
			if(! src.isFile()){ //不是檔案或者為null
				System.out.println("隻能拷貝檔案");
				throw new IOException("隻能拷貝檔案");
			}
			//2、選擇流
			InputStream is =new FileInputStream(src);
			OutputStream os =new FileOutputStream(dest);
			//3、檔案拷貝   循環+讀取+寫出
			byte[] flush =new byte[1024];
			int len =0;
			//讀取
			while(-1!=(len=is.read(flush))){
				//寫出
				os.write(flush, 0, len);
			}
			os.flush(); //強制刷出
			
			//關閉流
			os.close();
			is.close();
		}
	}
           

四、目錄檔案的拷貝

1、檔案 指派  copyFile

 2、檔案 建立 mkdirs()

 3、遞歸查找子孫級

public class CopyDir {
	
	public static void main(String[] args) {
		//源目錄
		String srcPath="E:/xp/test/a"; 	
		//目标目錄
		String destPath="E:/xp/test/a/b";
		copyDir(srcPath,destPath);
	}
	/**
	 * 拷貝檔案夾
	 * @param src 源路徑
	 * @param dest 目标路徑
	 */
	public static void copyDir(String  srcPath,String destPath){
		File src=new File(srcPath);
		File dest =new File(destPath);
		copyDir(src,dest);		
	}	
	/**
	 * 拷貝檔案夾
	 * @param src 源File對象
	 * @param dest 目标File對象
	 */
	public static void copyDir(File src,File dest){
		if(src.isDirectory()){ //檔案夾
			dest =new File(dest,src.getName());			
		}		
		copyDirDetail(src,dest);
	}
	
	/**
	 * 拷貝檔案夾細節
	 * @param src
	 * @param dest
	 */
	public static void copyDirDetail(File src,File dest){
		if(src.isFile()){ //檔案
			try {
				//拷貝檔案操作,省略
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}else if(src.isDirectory()){ //檔案夾
			//確定目标檔案夾存在
			dest.mkdirs();
			//擷取下一級目錄|檔案
			for(File sub:src.listFiles()){
				copyDirDetail(sub,new File(dest,sub.getName()));
			}
		}
	}
}