package com.atguigu.java;
import org.junit.Test;
import java.io.*;
/**
*
* 一、流的分類:
* 1.操作數據單位:字節流、字符流
* 2.數據的流向:輸入流、輸出流
* 3.流的角色:節點流、處理流
*
* 二、流的體系結構
* 抽象基類 節點流(或文件流) 緩衝流(處理流的一種)
* InputStream FileInputStream (read(byte[] buffer)) BufferedInputStream (read(byte[] buffer))
* OutputStream FileOutputStream (write(byte[] buffer,0,len) BufferedOutputStream (write(byte[] buffer,0,len) / flush()
* Reader FileReader (read(char[] cbuf)) BufferedReader (read(char[] cbuf) / readLine())
* Writer FileWriter (write(char[] cbuf,0,len) BufferedWriter (write(char[] cbuf,0,len) / flush()
*
*
*
* @author shkstart
* @create 2019 上午 10:40
*/
public class FileReaderWriterTest {
public static void main(String[] args) {
File file = new File("hello.txt");//相較於當前工程
System.out.println(file.getAbsolutePath());
File file1 = new File("day09\\hello.txt");
System.out.println(file1.getAbsolutePath());
}
/*
將day09下的hello.txt文件內容讀入程序中,並輸出到控制檯
說明點:
1. read()的理解:返回讀入的一個字符。如果達到文件末尾,返回-1
2. 異常的處理:爲了保證流資源一定可以執行關閉操作。需要使用try-catch-finally處理
3. 讀入的文件一定要存在,否則就會報FileNotFoundException。
*/
@Test
public void testFileReader(){
FileReader fr = null;
try {
//1.實例化File類的對象,指明要操作的文件
File file = new File("hello.txt");//相較於當前Module
//2.提供具體的流
fr = new FileReader(file);
//3.數據的讀入
//read():返回讀入的一個字符。如果達到文件末尾,返回-1
//方式一:
// int data = fr.read();
// while(data != -1){
// System.out.print((char)data);
// data = fr.read();
// }
//方式二:語法上針對於方式一的修改
int data;
while((data = fr.read()) != -1){
System.out.print((char)data);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
//4.流的關閉操作
// try {
// if(fr != null)
// fr.close();
// } catch (IOException e) {
// e.printStackTrace();
// }
//或
if(fr != null){
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
//對read()操作升級:使用read的重載方法
@Test
public void testFileReader1() {
FileReader fr = null;
try {
//1.File類的實例化
File file = new File("hello.txt");
//2.FileReader流的實例化
fr = new FileReader(file);
//3.讀入的操作
//read(char[] cbuf):返回每次讀入cbuf數組中的字符的個數。如果達到文件末尾,返回-1
char[] cbuf = new char[5];
int len;
while((len = fr.read(cbuf)) != -1){
//方式一:
//錯誤的寫法
// for(int i = 0;i < cbuf.length;i++){
// System.out.print(cbuf[i]);
// }
//正確的寫法
// for(int i = 0;i < len;i++){
// System.out.print(cbuf[i]);
// }
//方式二:
//錯誤的寫法,對應着方式一的錯誤的寫法
// String str = new String(cbuf);
// System.out.print(str);
//正確的寫法
String str = new String(cbuf,0,len);
System.out.print(str);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if(fr != null){
//4.資源的關閉
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/*
從內存中寫出數據到硬盤的文件裏。
說明:
1. 輸出操作,對應的File可以不存在的。並不會報異常
2.
File對應的硬盤中的文件如果不存在,在輸出的過程中,會自動創建此文件。
File對應的硬盤中的文件如果存在:
如果流使用的構造器是:FileWriter(file,false) / FileWriter(file):對原有文件的覆蓋
如果流使用的構造器是:FileWriter(file,true):不會對原有文件覆蓋,而是在原有文件基礎上追加內容
*/
@Test
public void testFileWriter() {
FileWriter fw = null;
try {
//1.提供File類的對象,指明寫出到的文件
File file = new File("hello1.txt");
//2.提供FileWriter的對象,用於數據的寫出
fw = new FileWriter(file,false);
//3.寫出的操作
fw.write("I have a dream!\n");
fw.write("you need to have a dream!");
} catch (IOException e) {
e.printStackTrace();
} finally {
//4.流資源的關閉
if(fw != null){
try {
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
@Test
public void testFileReaderFileWriter() {
FileReader fr = null;
FileWriter fw = null;
try {
//1.創建File類的對象,指明讀入和寫出的文件
File srcFile = new File("hello.txt");
File destFile = new File("hello2.txt");
//不能使用字符流來處理圖片等字節數據
// File srcFile = new File("愛情與友情.jpg");
// File destFile = new File("愛情與友情1.jpg");
//2.創建輸入流和輸出流的對象
fr = new FileReader(srcFile);
fw = new FileWriter(destFile);
//3.數據的讀入和寫出操作
char[] cbuf = new char[5];
int len;//記錄每次讀入到cbuf數組中的字符的個數
while((len = fr.read(cbuf)) != -1){
//每次寫出len個字符
fw.write(cbuf,0,len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
//4.關閉流資源
//方式一:
// try {
// if(fw != null)
// fw.close();
// } catch (IOException e) {
// e.printStackTrace();
// }finally{
// try {
// if(fr != null)
// fr.close();
// } catch (IOException e) {
// e.printStackTrace();
// }
// }
//方式二:
try {
if(fw != null)
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if(fr != null)
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
package com.atguigu.java;
import org.junit.Test;
import java.io.*;
/**
* 測試FileInputStream和FileOutputStream的使用
*
* 結論:
* 1. 對於文本文件(.txt,.java,.c,.cpp),使用字符流處理
* 2. 對於非文本文件(.jpg,.mp3,.mp4,.avi,.doc,.ppt,...),使用字節流處理
*
*
*
* @author shkstart
* @create 2019 下午 2:13
*/
public class FileInputOutputStreamTest {
//使用字節流FileInputStream處理文本文件,可能出現亂碼。
@Test
public void testFileInputStream() {
FileInputStream fis = null;
try {
//1. 造文件
File file = new File("hello.txt");
//2.造流
fis = new FileInputStream(file);
//3.讀數據
byte[] buffer = new byte[5];
int len;//記錄每次讀取的字節的個數
while((len = fis.read(buffer)) != -1){
String str = new String(buffer,0,len);
System.out.print(str);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if(fis != null){
//4.關閉資源
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/*
實現對圖片的複製操作
*/
@Test
public void testFileInputOutputStream() {
FileInputStream fis = null;
FileOutputStream fos = null;
try {
//
File srcFile = new File("愛情與友情.jpg");
File destFile = new File("愛情與友情2.jpg");
//
fis = new FileInputStream(srcFile);
fos = new FileOutputStream(destFile);
//複製的過程
byte[] buffer = new byte[5];
int len;
while((len = fis.read(buffer)) != -1){
fos.write(buffer,0,len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if(fos != null){
//
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(fis != null){
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
//指定路徑下文件的複製
public void copyFile(String srcPath,String destPath){
FileInputStream fis = null;
FileOutputStream fos = null;
try {
//
File srcFile = new File(srcPath);
File destFile = new File(destPath);
//
fis = new FileInputStream(srcFile);
fos = new FileOutputStream(destFile);
//複製的過程
byte[] buffer = new byte[1024];
int len;
while((len = fis.read(buffer)) != -1){
fos.write(buffer,0,len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if(fos != null){
//
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(fis != null){
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
@Test
public void testCopyFile(){
long start = System.currentTimeMillis();
String srcPath = "C:\\Users\\Administrator\\Desktop\\01-視頻.avi";
String destPath = "C:\\Users\\Administrator\\Desktop\\02-視頻.avi";
// String srcPath = "hello.txt";
// String destPath = "hello3.txt";
copyFile(srcPath,destPath);
long end = System.currentTimeMillis();
System.out.println("複製操作花費的時間爲:" + (end - start));//618
}
}
package com.atguigu.java;
import org.junit.Test;
import java.io.*;
/**
* 處理流之一:緩衝流的使用
*
* 1.緩衝流:
* BufferedInputStream
* BufferedOutputStream
* BufferedReader
* BufferedWriter
*
* 2.作用:提供流的讀取、寫入的速度
* 提高讀寫速度的原因:內部提供了一個緩衝區
*
* 3. 處理流,就是“套接”在已有的流的基礎上。
*
* @author shkstart
* @create 2019 下午 2:44
*/
public class BufferedTest {
/*
實現非文本文件的複製
*/
@Test
public void BufferedStreamTest() throws FileNotFoundException {
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
//1.造文件
File srcFile = new File("愛情與友情.jpg");
File destFile = new File("愛情與友情3.jpg");
//2.造流
//2.1 造節點流
FileInputStream fis = new FileInputStream((srcFile));
FileOutputStream fos = new FileOutputStream(destFile);
//2.2 造緩衝流
bis = new BufferedInputStream(fis);
bos = new BufferedOutputStream(fos);
//3.複製的細節:讀取、寫入
byte[] buffer = new byte[10];
int len;
while((len = bis.read(buffer)) != -1){
bos.write(buffer,0,len);
// bos.flush();//刷新緩衝區
}
} catch (IOException e) {
e.printStackTrace();
} finally {
//4.資源關閉
//要求:先關閉外層的流,再關閉內層的流
if(bos != null){
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(bis != null){
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
//說明:關閉外層流的同時,內層流也會自動的進行關閉。關於內層流的關閉,我們可以省略.
// fos.close();
// fis.close();
}
}
//實現文件複製的方法
public void copyFileWithBuffered(String srcPath,String destPath){
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
//1.造文件
File srcFile = new File(srcPath);
File destFile = new File(destPath);
//2.造流
//2.1 造節點流
FileInputStream fis = new FileInputStream((srcFile));
FileOutputStream fos = new FileOutputStream(destFile);
//2.2 造緩衝流
bis = new BufferedInputStream(fis);
bos = new BufferedOutputStream(fos);
//3.複製的細節:讀取、寫入
byte[] buffer = new byte[1024];
int len;
while((len = bis.read(buffer)) != -1){
bos.write(buffer,0,len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
//4.資源關閉
//要求:先關閉外層的流,再關閉內層的流
if(bos != null){
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(bis != null){
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
//說明:關閉外層流的同時,內層流也會自動的進行關閉。關於內層流的關閉,我們可以省略.
// fos.close();
// fis.close();
}
}
@Test
public void testCopyFileWithBuffered(){
long start = System.currentTimeMillis();
String srcPath = "C:\\Users\\Administrator\\Desktop\\01-視頻.avi";
String destPath = "C:\\Users\\Administrator\\Desktop\\03-視頻.avi";
copyFileWithBuffered(srcPath,destPath);
long end = System.currentTimeMillis();
System.out.println("複製操作花費的時間爲:" + (end - start));//618 - 176
}
/*
使用BufferedReader和BufferedWriter實現文本文件的複製
*/
@Test
public void testBufferedReaderBufferedWriter(){
BufferedReader br = null;
BufferedWriter bw = null;
try {
//創建文件和相應的流
br = new BufferedReader(new FileReader(new File("dbcp.txt")));
bw = new BufferedWriter(new FileWriter(new File("dbcp1.txt")));
//讀寫操作
//方式一:使用char[]數組
// char[] cbuf = new char[1024];
// int len;
// while((len = br.read(cbuf)) != -1){
// bw.write(cbuf,0,len);
// // bw.flush();
// }
//方式二:使用String
String data;
while((data = br.readLine()) != null){
//方法一:
// bw.write(data + "\n");//data中不包含換行符
//方法二:
bw.write(data);//data中不包含換行符
bw.newLine();//提供換行的操作
}
} catch (IOException e) {
e.printStackTrace();
} finally {
//關閉資源
if(bw != null){
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(br != null){
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
package com.atguigu.java;
import org.junit.Test;
import java.io.*;
/**
* 處理流之二:轉換流的使用
* 1.轉換流:屬於字符流
* InputStreamReader:將一個字節的輸入流轉換爲字符的輸入流
* OutputStreamWriter:將一個字符的輸出流轉換爲字節的輸出流
*
* 2.作用:提供字節流與字符流之間的轉換
*
* 3. 解碼:字節、字節數組 --->字符數組、字符串
* 編碼:字符數組、字符串 ---> 字節、字節數組
*
*
* 4.字符集
*ASCII:美國標準信息交換碼。
用一個字節的7位可以表示。
ISO8859-1:拉丁碼錶。歐洲碼錶
用一個字節的8位表示。
GB2312:中國的中文編碼表。最多兩個字節編碼所有字符
GBK:中國的中文編碼表升級,融合了更多的中文文字符號。最多兩個字節編碼
Unicode:國際標準碼,融合了目前人類使用的所有字符。爲每個字符分配唯一的字符碼。所有的文字都用兩個字節來表示。
UTF-8:變長的編碼方式,可用1-4個字節來表示一個字符。
*
*
* @author shkstart
* @create 2019 下午 4:25
*/
public class InputStreamReaderTest {
/*
此時處理異常的話,仍然應該使用try-catch-finally
InputStreamReader的使用,實現字節的輸入流到字符的輸入流的轉換
*/
@Test
public void test1() throws IOException {
FileInputStream fis = new FileInputStream("dbcp.txt");
// InputStreamReader isr = new InputStreamReader(fis);//使用系統默認的字符集
//參數2指明瞭字符集,具體使用哪個字符集,取決於文件dbcp.txt保存時使用的字符集
InputStreamReader isr = new InputStreamReader(fis,"UTF-8");//使用系統默認的字符集
char[] cbuf = new char[20];
int len;
while((len = isr.read(cbuf)) != -1){
String str = new String(cbuf,0,len);
System.out.print(str);
}
isr.close();
}
/*
此時處理異常的話,仍然應該使用try-catch-finally
綜合使用InputStreamReader和OutputStreamWriter
*/
@Test
public void test2() throws Exception {
//1.造文件、造流
File file1 = new File("dbcp.txt");
File file2 = new File("dbcp_gbk.txt");
FileInputStream fis = new FileInputStream(file1);
FileOutputStream fos = new FileOutputStream(file2);
InputStreamReader isr = new InputStreamReader(fis,"utf-8");
OutputStreamWriter osw = new OutputStreamWriter(fos,"gbk");
//2.讀寫過程
char[] cbuf = new char[20];
int len;
while((len = isr.read(cbuf)) != -1){
osw.write(cbuf,0,len);
}
//3.關閉資源
isr.close();
osw.close();
}
}