hpe實訓課(IO-字符流)

FileReader和FileWriter 處理字符流(文本文件)

1.讀取一個文件的內容
public void testFileReader() {
		FileReader fr = null;
		try {
			// 1.準備好一個讀入的文件
			File file = new File("oop.txt");
			// 2.創建一個基本字符流的FileReader對象
			fr = new FileReader(file);
			// 3.讀數據
			// 存放每次讀取的字符
			char[] c = new char[24];
			int len;
			while ((len = fr.read(c)) != -1) {
				// 每一次讀取長度爲len的字符,從索引爲0的位置開始讀
				String str = new String(c, 0, len);
				System.out.print(str);
			}
			
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			// 關閉流
			try {
				fr.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
2.實現文本文件的複製(通過FileReader和FileWriter)
 對於非文本文件(圖片、視頻)  只能使用字節流
public void testFileReaderWriter() {
		FileReader fr = null;
		FileWriter fw = null;
		try {
			// 原始文件
			File src = new File("oop.txt");
			// 目標文件
			File dest = new File("oop");
			// 字符輸入流對象
			fr = new FileReader(src);
			// 字符輸出流對象
			fw = new FileWriter(dest);
			// 用來存放讀取的數據
			char[] c= new char[24];
			int len;
			while ((len = fr.read(c)) != -1) {
				// 寫入數據
				fw.write(c, 0, len);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			// 釋放資源
			if (fw != null) {
				try {
					fw.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (fr != null) {
				try {
					fr.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
3.把一個圖片的內容寫入到新的文件
public void testBufferedInputOutputStream() {
		// 字節輸入緩衝流
		BufferedInputStream bis = null;
		// 字節輸出緩衝流
		BufferedOutputStream bos = null;
		
		try {
			// 1.提供讀寫的文件
			File file1 = new File("one.jpg");
			File file2 = new File("1.jpg");
			// 2.創建相應的節點流,FileInputStream,FileOutputStream
			FileInputStream fis= new FileInputStream(file1);
			FileOutputStream fos = new FileOutputStream(file2);
			// 3.將創建的節點流對象作爲形參傳遞給緩衝流的構造器
			bis = new BufferedInputStream(fis);
			bos = new BufferedOutputStream(fos);
			// 4.實現文件複製的操作
			byte[] b = new byte[1024];
			int len;
			// 讀數據
			while ((len = bis.read(b)) != -1) {
				// 寫數據
				bos.write(b, 0, len);
				// 清空緩衝區數據
				bos.flush();
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (bos != null) {
				try {
					bos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (bis != null) {
				try {
					bis.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
4.使用緩衝流實現文件複製
public void copyFile(String src,String dest) {
				// 字節輸入緩衝流
				BufferedInputStream bis = null;
				// 字節輸出緩衝流
				BufferedOutputStream bos = null;
				
				try {
					// 1.提供讀寫的文件
					File file1 = new File(src);
					File file2 = new File(dest);
					// 2.創建相應的節點流,FileInputStream,FileOutputStream
					FileInputStream fis= new FileInputStream(file1);
					FileOutputStream fos = new FileOutputStream(file2);
					// 3.將創建的節點流對象作爲形參傳遞給緩衝流的構造器
					bis = new BufferedInputStream(fis);
					bos = new BufferedOutputStream(fos);
					// 4.實現文件複製的操作
					byte[] b = new byte[1024];
					int len;
					// 讀數據
					while ((len = bis.read(b)) != -1) {
						// 寫數據
						bos.write(b, 0, len);
						// 清空緩衝區數據
						bos.flush();
					}
				} catch (Exception e) {
					e.printStackTrace();
				} finally {
					if (bos != null) {
						try {
							bos.close();
						} catch (IOException e) {
							e.printStackTrace();
						}
					}
					if (bis != null) {
						try {
							bis.close();
						} catch (IOException e) {
							e.printStackTrace();
						}
					}
				}
	}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章