- 基础字节流 数组 和单个字节 完成文件复制
public class BasiceByteFileIO {
public static void main(String[] args) throws Exception {
// 01.创建抽象路径
File start = new File("a.txt");// 起始路径
File end = new File("E://a.txt");// 目标路径
// ··单个字节复制
// method01(start, end);
// ··数组复制文件
method02(start, end);
}
// ``单个字节复制文件
private static void method01(File start, File end) throws Exception {
// 02.创建基础字节输入输出流
FileInputStream fis = new FileInputStream(start);
FileOutputStream fos = new FileOutputStream(end);
// 03.进行复制
int len;
while ((len = fis.read()) != -1) {
fos.write(len);
}
fis.close();
fos.close();
}
// ··数组复制文件
private static void method02(File start, File end) throws Exception {
// 02.创建基础字节输入输出流
FileInputStream fis = new FileInputStream(start);
FileOutputStream fos = new FileOutputStream(end);
// 03.进行复制
int len;
byte[] bys = new byte[1024];
while ((len = fis.read(bys)) != -1) {
fos.write(bys, 0, len);
}
fis.close();
fos.close();
}
}
- 基础字符流 单个字符、数组完成文件复制
public class BasiceCharFileIO {
public static void main(String[] args) throws Exception {
// 起始路径与目标路径位置
String start = "c.txt";
String end = "E://c.txt";
// ··单个字符完成复制
// method01(start, end);
// ··数组完成文件复制
method02(start, end);
}
// ··单个字符完成复制
private static void method01(String start, String end) throws Exception {
// 01.创建基础字符流对象
Writer w = new FileWriter(end);
Reader r = new FileReader(start);
// 02.进行复制
int len;
while ((len = r.read()) != -1) {
w.write(len);
}
// 03.关闭流
w.close();
r.close();
}
// ··数组完成文件复制
private static void method02(String start, String end) throws Exception {
// 01.创建基础字符流对象
FileWriter w = new FileWriter(end);
FileReader r = new FileReader(start);
// 02.进行复制
int len;
char[] chr = new char[1024];
while ((len = r.read(chr)) != -1) {
w.write(chr, 0, len);
}
// 03.关闭流
w.close();
r.close();
}
}
- 高效字节流 数组 和单个字节 完成文件复制
public class EfficientByteFileIO {
public static void main(String[] args) throws Exception {
// 创建抽象路径
File start = new File("b.txt");// 起始路径
File end = new File("E://b.txt");// 目标路径
// ··单个字节完成文件复制
// method01(start, end);
// ··数组完成文件复制
method02(start, end);
}
// ··单个字节完成文件复制
private static void method01(File start, File end) throws Exception {
// 01.创建高效字符流;
InputStream is = new FileInputStream(start);
OutputStream os = new FileOutputStream(end);
BufferedInputStream bis = new BufferedInputStream(is);
BufferedOutputStream bos = new BufferedOutputStream(os);
// 02.字节复制
int len;
while ((len = bis.read()) != -1) {
bos.write(len);
}
bis.close();
bos.close();
is.close();
os.close();
}
// ··数组完成文件复制
private static void method02(File start, File end) throws Exception {
// 01.创建高效字符流;
InputStream is = new FileInputStream(start);
OutputStream os = new FileOutputStream(end);
BufferedInputStream bis = new BufferedInputStream(is);
BufferedOutputStream bos = new BufferedOutputStream(os);
// 02.字节复制
int len;
byte[] bys = new byte[1024];
while ((len = bis.read(bys)) != -1) {
bos.write(bys, 0, len);
}
bis.close();
bos.close();
is.close();
os.close();
}
}
- 高效字符流 单个字符、数组完成文件复制
public class EfficientCharFileIO {
public static void main(String[] args) throws Exception {
// 起始路径与目标路径位置
String start = "c.txt";
String end = "E://c.txt";
// ··单个字符完成复制
method01(start, end);
// ··数组完成文件复制
// method02(start, end);
}
// ··单个字节完成复制
private static void method01(String start, String end) throws Exception {
// 01.创建高效字节流对象
Writer w = new FileWriter(end);
Reader r = new FileReader(start);
BufferedReader br = new BufferedReader(r);
BufferedWriter bw = new BufferedWriter(w);
// 02.进行复制
int len;
while ((len = br.read()) != -1) {
bw.write(len);
}
// 03.关闭流
br.close();
bw.close();
w.close();
r.close();
}
// ··数组完成文件复制
private static void method02(String start, String end) throws Exception {
// 01.创建高效字节流对象
FileWriter w = new FileWriter(end);
FileReader r = new FileReader(start);
BufferedReader br = new BufferedReader(r);
BufferedWriter bw = new BufferedWriter(w);
// 02.进行复制
int len;
char[] chr = new char[1024];
while ((len = br.read(chr)) != -1) {
bw.write(chr, 0, len);
}
// 03.关闭流
br.close();
bw.close();
w.close();
r.close();
}
}