1.使用字符流复制文件的5种方法
package com.dml.io2;
/**
* 使用字符流的方法复制文件
*/
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
public class Copy {
public static void main(String[] args) throws Exception{
method1("Hello.java","1.java");
method2("Hello.java","2.java");
method3("Hello.java","3.java");
method4("Hello.java","4.java");
method5("Hello.java","5.java");
}
/**
* 使用读单个字符
* @param read
* @param writer
* @throws Exception
*/
private static void method1(String read, String writer) throws Exception {
FileReader fr = new FileReader(read);
FileWriter fw = new FileWriter(writer);
int ch = 0;
while((ch = fr.read()) != -1){
fw.write(ch);
}
fw.close();
fr.close();
}
/**
* 使用读一个数组
* @param read
* @param writer
* @throws Exception
*/
private static void method2(String read, String writer) throws Exception {
FileReader fr = new FileReader(read);
FileWriter fw = new FileWriter(writer);
char []chs = new char[1024];
int len = 0;
while((len = fr.read(chs)) != -1){
fw.write(chs,0,len);
}
fw.close();
fr.close();
}
/**
* 使用缓冲流读单个字符
* @param read
* @param writer
* @throws Exception
*/
private static void method3(String read, String writer) throws Exception {
BufferedReader br = new BufferedReader(new FileReader(read));
BufferedWriter bw = new BufferedWriter(new FileWriter(writer));
int ch = 0;
while((ch = br.read()) != -1){
bw.write(ch);
bw.flush();
}
bw.close();
br.close();
}
/**
* 使用缓冲流读数组
* @param read
* @param writer
* @throws Exception
*/
private static void method4(String read, String writer) throws Exception {
BufferedReader br = new BufferedReader(new FileReader(read));
BufferedWriter bw = new BufferedWriter(new FileWriter(writer));
char []chs = new char[1024];
int len = 0;
while((len = br.read(chs)) != -1){
bw.write(chs, 0, len);
bw.flush();
}
bw.close();
br.close();
}
/**
* 使用缓冲流读一行
* @param read
* @param writer
* @throws Exception
*/
private static void method5(String read, String writer) throws Exception {
BufferedReader br = new BufferedReader(new FileReader(read));
BufferedWriter bw = new BufferedWriter(new FileWriter(writer));
String line = null;
while((line = br.readLine()) != null){
bw.write(line);
bw.newLine();
bw.flush();
}
bw.close();
br.close();
}
}
2.使用字节流复制文件的4种方法
package com.dml.io2;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
/**
* 使用字节流复制文件
* @author dml
*
*/
public class Copy_zijie {
public static void main(String[] args) throws Exception {
method1("a.jpg","C:\\Users\\dml\\Pictures\\Saved Pictures\\1.jpg");
method2("a.jpg","C:\\Users\\dml\\Pictures\\Saved Pictures\\2.jpg");
method3("a.jpg","C:\\Users\\dml\\Pictures\\Saved Pictures\\3.jpg");
method4("a.jpg","C:\\Users\\dml\\Pictures\\Saved Pictures\\4.jpg");
}
private static void method1(String input, String output) throws Exception {
FileInputStream fis = new FileInputStream(input);
FileOutputStream fos = new FileOutputStream(output);
int by = 0;
while((by = fis.read()) != -1){
fos.write(by);
}
fos.close();
fis.close();
}
/**
* 读一个字节数组
* @param input
* @param output
* @throws Exception
*/
private static void method2(String input, String output) throws Exception {
FileInputStream fis = new FileInputStream(input);
FileOutputStream fos = new FileOutputStream(output);
byte []bys = new byte[1024];
int len = 0;
while((len = fis.read(bys)) != -1){
fos.write(bys,0,len);
}
fos.close();
fis.close();
}
/**
* 使用缓冲流读一个字节数组
* @param input
* @param output
* @throws Exception
*/
private static void method3(String input, String output) throws Exception {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(input));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(output));
byte []bys = new byte[1024];
int len = 0;
while((len = bis.read(bys)) != -1){
bos.write(bys,0,len);
}
bos.close();
bis.close();
}
/**
* 使用缓冲流读
* @param input
* @param output
* @throws Exception
*/
private static void method4(String input, String output) throws Exception {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(input));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(output));
int by = 0;
while((by = bis.read()) != -1){
bos.write(by);
}
bos.close();
bis.close();
}
}