标签:
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();
}
}
}
}
}
标签:
原文地址:http://www.cnblogs.com/canceler/p/4631629.html