Java IO (三)字節流對文件進行相關操作

一、字節流讀取文件

常規造作如下:
    1、建立聯繫   File對象 源頭
    2、選擇流     文件輸入流  InputStream FileInputStream
    3、操作  : byte[] car =new byte[1024];  +read+讀取大小
              輸出
    4、釋放資源 :關閉

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

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、釋放資源 :關閉

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
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、建立聯繫   File對象   源頭 目的地

2、選擇流     

       文件輸入流  InputStream FileInputStream

        文件輸出流  OutputStream FileOutputStream

3、操作  :  拷貝

       byte[] flush =new byte[1024]; 

       int len =0;

        while(-1!=(len=輸入流.read(flush))){  

             輸出流.write(flush,0,len)

        }

             輸出流.flush

4、釋放資源 :關閉 兩個流

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

public class CopyFileDemo {

	/**
	 * @param args
	 * @throws FileNotFoundException 
	 */
	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、遞歸查找子孫級

 

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


public class CopyDir {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		//源目錄
		String srcPath="E:/xp/test/a"; 	
		//目標目錄
		String destPath="E:/xp/test/a/b";
		try {
			FileUtil.copyDir(srcPath,destPath);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
	/**
	 * 拷貝文件夾
	 * @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 {
				FileUtil.copyFile(src, dest);
			} 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()));
			}
		}
	}

}

 

 

 

 

 

五、工具類

將文件的拷貝文件夾的拷貝等工具類封裝到一個工具包裏。包括文件拷貝和文件夾的拷貝。

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;
import java.io.InputStream;
import java.io.OutputStream;
/**
 * 文件操作
 * 1、文件拷貝
 * 2、文件夾拷貝  拒絕自己拷貝給自己
 * @author Administrator
 *
 */
public class FileUtil {
	/**
	 * 拷貝文件夾
	 * @param src 源路徑
	 * @param dest 目標路徑
	 * @throws IOException 
	 * @throws FileNotFoundException 
	 */
	public static void copyDir(String  srcPath,String destPath) throws FileNotFoundException, IOException{
		//拒絕自己拷貝給自己
		if(srcPath.equals(destPath)){
			return ;
		}
		File src=new File(srcPath);
		File dest =new File(destPath);
		copyDir(src,dest);		
	}
	
	
	
	/**
	 * 拷貝文件夾
	 * @param src 源File對象
	 * @param dest 目標File對象
	 * @throws IOException 
	 * @throws FileNotFoundException 
	 */
	public static void copyDir(File src,File dest) throws FileNotFoundException, IOException{
		if(src.isDirectory()){ //文件夾
			dest =new File(dest,src.getName());
			if(dest.getAbsolutePath().contains(src.getAbsolutePath())){
				System.out.println("父目錄不能拷貝到子目錄中");
				return;
			}
		}		
		copyDirDetail(src,dest);
	}
	
	/**
	 * 拷貝文件夾細節
	 * @param src
	 * @param dest
	 */
	public static void copyDirDetail(File src,File dest) throws FileNotFoundException,IOException{
		if(src.isFile()){ //文件
			try {
				FileUtil.copyFile(src, dest);
			} catch (FileNotFoundException e) {
				//e.printStackTrace();
				throw e;
			} catch (IOException e) {
				//e.printStackTrace();
				throw e;
			}
		}else if(src.isDirectory()){ //文件夾
			//確保目標文件夾存在
			dest.mkdirs();
			//獲取下一級目錄|文件
			for(File sub:src.listFiles()){
				copyDirDetail(sub,new File(dest,sub.getName()));
			}
		}
	}
	
	
	/**
	 * 文件的拷貝
	 * @param  源文件路徑
	 * @param  目錄文件路徑
	 * @throws FileNotFoundException,IOException
	 * @return 
	 */
	public static void copyFile(String srcPath,String destPath) throws FileNotFoundException,IOException {
		//1、建立聯繫 源(存在且爲文件) +目的地(文件可以不存在) 
		copyFile(new File(srcPath),new File(destPath));
	}
	/**
	 * 文件的拷貝
	 * @param  源文件File對象
	 * @param  目錄文件File對象
	 * @throws FileNotFoundException,IOException
	 * @return 
	 */
	public static void copyFile(File src,File dest) throws FileNotFoundException,IOException {
		if(! src.isFile()){ //不是文件或者爲null
			System.out.println("只能拷貝文件");
			throw new IOException("只能拷貝文件");
		}
		//dest爲已經存在的文件夾,不能建立於文件夾同名的文件
		if(dest.isDirectory()){
			System.out.println(dest.getAbsolutePath()+"不能建立於文件夾同名的文件");
			throw new IOException(dest.getAbsolutePath()+"不能建立於文件夾同名的文件");
		}
		
		
		//2、選擇流
		InputStream is =new BufferedInputStream(new FileInputStream(src));
		OutputStream os =new BufferedOutputStream(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();
	}
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章