package cn.itcast_03;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
public class Demo03 {
public static void main(String[] args) {
multi_byte_excute("123.mp4", "332.mp4");
}
// 字節流操作
// 單字節文件復制(FileInputStreams,FileOutputStreams),適合文件的操作,即不用拿來看的,速度快,因為沒有編碼的操作,純粹字節碼
public static void single_byte_excute(String infile, String outfile) {
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream(infile);
fos = new FileOutputStream(outfile);
int by = 0;
while ((by = fis.read()) != -1) {
fos.write(by);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
// 字節數組文件復制(FileInputStreams,FileOutputStreams),適合文件的操作,即不用拿來看的,速度快,因為沒有編碼的操作,純粹字節碼
public static void multi_byte_excute(String infile, String outfile) {
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream(infile);
fos = new FileOutputStream(outfile);
int len = 0;
byte[] bys = new byte[1024];
while ((len = fis.read(bys, 0, bys.length)) != -1) {
fos.write(bys, 0, len);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
// 高效單字節文件復制(BufferedInputStreams,BufferedOutputStreams),增加了緩沖區,減少多次IO操作,IO操作消耗很大,有緩沖區能夠增加效率,適合文件的操作,即不用拿來看的,速度快,因為沒有編碼的操作,純粹字節碼
public static void buffer_single_byte_excute(String infile, String outfile) {
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
bis = new BufferedInputStream(new FileInputStream(infile));
bos = new BufferedOutputStream(new FileOutputStream(outfile));
int by = 0;
while ((by = bis.read()) != -1) {
bos.write(by);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (bos != null) {
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
// 高效字節數組文件復制(BufferedInputStreams,BufferedOutputStreams),增加了緩沖區,減少多次IO操作,IO操作消耗很大,有緩沖區能夠增加效率,適合文件的操作,即不用拿來看的,速度快,因為沒有編碼的操作,純粹字節碼
public static void buffer_multi_byte_excute(String infile, String outfile) {
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
bis = new BufferedInputStream(new FileInputStream(infile));
bos = new BufferedOutputStream(new FileOutputStream(outfile));
int len = 0;
byte[] array = new byte[1024];
while ((len = bis.read(array, 0, array.length)) != -1) {
bos.write(array, 0, len);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (bos != null) {
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
// ------------------------------------------------------------------------
// 字符流操作
// 單個字符操作(OutputStreamWriter,稱為字符流轉字節流,InputStreamReader,稱為字節流轉字符流)適合文本文件
public static void sinle_char_excute(String infile, String outfile) {
OutputStreamWriter osw = null;
InputStreamReader isr = null;
try {
osw = new OutputStreamWriter(new FileOutputStream(outfile));
isr = new InputStreamReader(new FileInputStream(infile));
int ch = 0;
while ((ch = isr.read()) != -1) {
osw.write(ch);
osw.flush();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (osw != null) {
try {
osw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (isr != null) {
try {
isr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
// 字符數組操作(OutputStreamWriter,稱為字符流轉字節流,InputStreamReader,稱為字節流轉字符流)適合文本文
public static void multi_char_excute(String infile, String outfile) {
OutputStreamWriter osw = null;
InputStreamReader isr = null;
try {
osw = new OutputStreamWriter(new FileOutputStream(outfile));
isr = new InputStreamReader(new FileInputStream(infile));
int len = 0;
char[] chs = new char[1024];
while ((len = isr.read(chs, 0, chs.length)) != -1) {
osw.write(chs, 0, len);
osw.flush();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (osw != null) {
try {
osw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (isr != null) {
try {
isr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
// 簡便字符操作包裝類,單字符輸入輸出復制(FileWriter,FileReader)
public static void simple_single_char_excute(String infile, String outfile) {
FileWriter fw = null;
FileReader fr = null;
try {
fw = new FileWriter(outfile);
fr = new FileReader(infile);
int ch = 0;
while ((ch = fr.read()) != -1) {
fw.write(ch);
fw.flush();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException 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();
}
}
}
}
// 簡便字符操作包裝類,字符數組輸入輸出復制(FileWriter,FileReader)
public static void simple_multi_char_excute(String infile, String outfile) {
FileWriter fw = null;
FileReader fr = null;
try {
fw = new FileWriter(outfile);
fr = new FileReader(infile);
int len = 0;
char[] chs = new char[1024];
while ((len = fr.read(chs, 0, chs.length)) != -1) {
fw.write(chs, 0, len);
fw.flush();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException 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();
}
}
}
}
// 高效單字符緩沖流復制操作
public static void buffer_single_char_excute(String infile, String outfile) {
BufferedWriter bw = null;
BufferedReader br = null;
try {
// bw = new BufferedWriter(new OutputStreamWriter(new
// FileOutputStream(outfile)));
bw = new BufferedWriter(new FileWriter(outfile));
// br = new BufferedReader(new InputStreamReader(new
// FileInputStream(infile)));
br = new BufferedReader(new FileReader(infile));
int ch = 0;
while ((ch = br.read()) != -1) {
bw.write(ch);
bw.flush();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} 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();
}
}
}
}
// 高效字符數組緩沖流復制操作
public static void buffer_multi_char_excute(String infile, String outfile) {
BufferedWriter bw = null;
BufferedReader br = null;
try {
// bw = new BufferedWriter(new OutputStreamWriter(new
// FileOutputStream(outfile)));
bw = new BufferedWriter(new FileWriter(outfile));
// br = new BufferedReader(new InputStreamReader(new
// FileInputStream(infile)));
br = new BufferedReader(new FileReader(infile));
int len = 0;
char[] chs = new char[1024];
while ((len = br.read(chs, 0, chs.length)) != -1) {
bw.write(chs, 0, len);
bw.flush();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} 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();
}
}
}
}
// 高效緩沖字符流特有功能進行文本文件復制操作,讀行寫行操作
public static void buffer_special_char_file_copy(String infile, String outfile) {
BufferedWriter bw = null;
BufferedReader br = null;
try {
bw = new BufferedWriter(new FileWriter(outfile));
br = new BufferedReader(new FileReader(infile));
String str = null;
//默認br.readLine()只讀該行內容,不會讀取該行的換行符
while ((str = br.readLine()) != null) {
bw.write(str);
bw.newLine();
bw.flush();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} 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();
}
}
}
}
}