一、S02E152_01IO_緩衝流_BufferedInputStream、BufferedOutputStream、BufferedReader、BuffereWriter
處理流:增強功能、提供性能,節點流之上
一、緩衝流
1、字節緩衝流
BufferedInputStream
BufferedOutputStream
2、字符緩衝流
BufferedReader 比字節流字符流新增String readLine() 讀取一個文本行
BuffeedWriter 比字節流字符流新增void newLine() 寫入一個行分隔符
package com.test.io.buffered;
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;
/**
* 字節流文件拷貝 + 緩衝流,提高性能
* 緩衝流(節點流)
* 程序爲橋樑
a.建立聯繫:File對象 源頭和目的地
b.選擇流:文件輸入流 InputStream FileInputStream
文件輸出流 OutputStream FileOutputStream
c.操作:拷貝
byte[] flush = new byte[1024];
int len = 0;
while(-1 != (len = 輸入流.read(flush))){
輸出流.write(flush,0,len)
}
輸出流.flush
d.釋放資源:關閉兩個流
*/
public class CopyFileByByte {
public static void main(String[] args) {
String srcPath = "G:/java/test/background.jpg";
String desPath = "G:/java/test/100.jpg";
try {
copyFile(srcPath,desPath);
} catch (FileNotFoundException e) {
e.printStackTrace();
System.out.println("文件沒有找到");
} catch (IOException e) {
e.printStackTrace();
System.out.println("文件拷貝失敗或者關閉流失敗");
}
}
/**
* 文件的拷貝,傳入File參數
* @param src
* @param des
* @throws FileNotFoundException
* @throws IOException
*/
public static void copyFile(File src,File des) throws FileNotFoundException,IOException {
//1.建立聯繫 源頭(存在且爲文件) + 目的地(文件可以不存在)
if(!src.isFile()){
throw new IOException("只能拷貝文件");
}
//des爲存在的文件夾,不能建立與文件夾同名的文件
if(des.isDirectory()){
System.out.println(des.getAbsolutePath() + "不能建立與文件夾同名的文件");
throw new IOException(des.getAbsolutePath() + "不能建立與文件夾同名的文件");
}
//2.選擇流,用緩衝流進行包裝,提高性能
InputStream is = new BufferedInputStream(new FileInputStream(src));
OutputStream os = new BufferedOutputStream(new FileOutputStream(des));
//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();
}
/**
* 文件的拷貝,傳入String參數,路徑
* @param srcPath
* @param desPath
* @throws FileNotFoundException
* @throws IOException
*/
public static void copyFile(String srcPath,String desPath) throws FileNotFoundException,IOException {
copyFile(new File(srcPath),new File(desPath));
}
}
package com.test.io.buffered;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
/**
* 純文本文件的拷貝
* 字符緩衝流 + 新增方法(不能發生多態)讀取一行和回車換行
*/
public class CopyFileByChar {
public static void main(String[] args) {
File src = new File("G:/java/test/path.java");
File des = new File("G:/java/test/Path1.java");
BufferedReader charStreamR = null;
BufferedWriter charStreamW = null;
try {
//用緩衝流包裝,提高性能
charStreamR = new BufferedReader(new FileReader(src));
charStreamW = new BufferedWriter(new FileWriter(des));
/*
char[] flush = new char[2];
int len = 0;
while(-1 != (len=charStreamR.read(flush))){
charStreamW.write(flush, 0, len);
}
*/
//新增方法的操作
String line = null;
while(null != (line=charStreamR.readLine())){//讀取一行
charStreamW.write(line);
// charStreamW.append("\r\n");
charStreamW.newLine();//回車換行,\r\n
}
charStreamW.flush();
} catch (IOException e) {
e.printStackTrace();
System.out.println("文件夾或不可創建或不可打開");
} finally {
try {
charStreamW.close();
charStreamR.close();
} catch (IOException e) {
e.printStackTrace();
System.out.println("關閉流失敗");
}
}
}
}
二、S02E153_01IO_轉換流_字節轉爲字符、亂碼分析、編碼與解碼字符集
三、S02E154_01IO_轉換流_字節轉爲字符、InputStreamReader、OutputStreamWriter、文件編碼與解碼
轉換流:字節流轉爲字符流,處理亂碼(編碼集、解碼集)
1、編碼與解碼概念,以程序爲中心
編碼:字符 –編碼字符集-> 二進制
解碼:二進制 –解碼字符集-> 字符
2、亂碼
1、編碼與解碼的字符集不統一
2、字節缺少,長度丟失
3、文件亂碼
package com.test.conver;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
/**
* 轉換流
*/
public class Charset {
public static void main(String[] args) throws IOException {
test1();
test2();
test3();
}
/**
* 編碼與解碼字符集必須統一,否則亂碼
* @throws UnsupportedEncodingException
*/
public static void test1() throws UnsupportedEncodingException{
System.out.println("-------編碼與解碼字符集必須統一,否則亂碼-------------------");
//解碼byte-->>char
String str = "中國";//當前默認字符集gbk
//編碼char-->>byte
byte[] data = str.getBytes();
//使用平臺的默認字符集解碼指定的 byte數組,構造一個新的 String
System.out.println(new String(data));//編碼與解碼字符集統一,返回"中國"
data = str.getBytes("utf-8");//設定編碼字符集
System.out.println(new String(data));//編碼爲utf-8,解碼爲gbk,字符集不統一出現亂碼,返回"涓浗"
//編碼
byte[] data2 = "中國".getBytes("utf-8");
//解碼
str = new String(data2,"utf-8");
System.out.println(str);
}
/**
* 字節數不完整,出現亂碼
*/
public static void test2(){
System.out.println("----------字節數不完整,出現亂碼--------------------");
String str = "中國";
byte[] data = str.getBytes();
System.out.println("\"中國\"這兩個字的字節長度:" + data.length);
System.out.println("字節長度減1後的字符:" + new String(data,0,data.length -1));
}
/**
* 轉換流:字節流轉爲字符流(確保源不爲亂碼)
* 1、輸出流:OutputStreamWriter 編碼
* 2、輸入流:InputStreamReader 解碼
* @throws IOException
*/
public static void test3() throws IOException{
System.out.println("--------轉換流:字節流轉爲字符流(確保源不爲亂碼)-------------------");
//指定解碼字符集,path.java編碼爲utf-8
BufferedReader br = new BufferedReader(
new InputStreamReader(
new FileInputStream(new File("G:/java/test/path.java")),"utf-8")//沒有亂碼
//new FileInputStream(new File("G:/java/test/path.java")))//默認gbk,出現亂碼
);
//寫出文件,按字符集編碼,不會出現亂碼
BufferedWriter bw = new BufferedWriter(
new OutputStreamWriter(
new FileOutputStream(new File("G:/java/test/encode.txt")),"utf-8")
//new FileOutputStream(new File("G:/java/test/encode.txt")))//ANSI字符集,不會出現亂碼
);
String info = null;
while(null != (info=br.readLine())){//解碼
System.out.println(info);
bw.write(info);
bw.newLine();
}
bw.flush();
bw.close();
br.close();
}
}