package iotestdemo;
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;
/*
* 1、使用至少9种方式拷贝文件
*
* 字节输入流 + 字节输出流 拷贝文件
*
* FileDemo01.java --> test.txt
* 数据源: FileDemo01.java
* 目的地: test.txt
* 交通工具:
* 文件 -> 内存 FileInputStream
* 内存 -> 文件 FileOutputStream
* 拷贝方式:
* 1.一次性拷贝一个字节
* 2.一次性拷贝一个字节数组
* 3.高效缓输入输出流
*/
public class IoTest01 {
public static void main(String[] args) {
copy1 ("src/iotestdemo/IoTest01.java", "b.txt");///day20demo/src/iodemo/FileDemo01.java
copy2 ("src/iodemo/FileDemo01.java", "a.txt");///day20demo/src/iodemo/FileDemo01.java
copy3 ("src/iodemo/FileDemo01.java", "c.txt");
copy4 ("src/iotestdemo/IoTest01.java", "d.txt");
copy5 ("src/iotestdemo/IoTest01.java", "e.txt");
copy6 ("src/iotestdemo/IoTest01.java", "f.txt");
copy7 ("src/iotestdemo/IoTest01.java", "g.txt");
copy8 ("src/iotestdemo/IoTest01.java", "h.txt");
copy9 ("src/iotestdemo/IoTest01.java", "i.txt");
copy10 ("src/iotestdemo/IoTest01.java", "j.txt");
copy11 ("src/iotestdemo/IoTest01.java", "k.txt");
}
/*第十一种:
* 功能: 高效缓冲字节输入输出流
* 返回值类型 : void
* 参数列表: String srcFileName, String loadingFilename
* 方法名: copy11
*/
private static void copy11 (String srcFileName, String loadingFilename) {
FileInputStream fis = null;
FileOutputStream fos = null;
try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcFileName));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(loadingFilename));) {
int len = 0;
byte[] bys = new byte[1024];
while ((len = bis.read(bys)) != -1) {
bos.write(bys, 0, len);
bos.flush();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
autoCloseResource1(fis,fos);
}
}
public static void autoCloseResource1(AutoCloseable... args) {//关闭所有开关
for (AutoCloseable c : args) {
if (c != null) {
try {
c.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
@SafeVarargs
public static <T extends AutoCloseable> void autoCloseResource2(T... args) {//关闭所有开关
for (T c : args) {
if (c != null) {
try {
c.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
/*第十种:
* 功能: 一次性拷贝字符拷贝文件
* 返回值类型 : void
* 参数列表: String srcFileName, String loadingFilename
* 方法名: copy10
*/
public static void copy10 (String srcFileName, String loadingFilename) {
try (FileReader fr = new FileReader(srcFileName);
FileWriter fw = new FileWriter(loadingFilename);) {
int chr = 0;
while ((chr = fr.read()) != -1) {
fw.write(chr);
fw.flush();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/*第九种:
* 功能: 一次性拷贝字符数组拷贝文件
* 返回值类型 : void
* 参数列表: String srcFileName, String loadingFilename
* 方法名: copy9
*/
public static void copy9 (String srcFileName, String loadingFilename) {
try (FileReader fr = new FileReader(srcFileName);
FileWriter fw = new FileWriter(loadingFilename);) {
int len = 0;
char[] chr = new char[1024];
while ((len = fr.read(chr)) != -1) {
fw.write(chr,0,len);
fw.flush();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/*第八种:
* 功能: 高效缓冲字符数组输入输出流
* 返回值类型 : void
* 参数列表: String srcFileName, String loadingFilename
* 方法名: copy8
*/
public static void copy8 (String srcFileName, String loadingFilename) {
try (BufferedReader bis = new BufferedReader(new FileReader(srcFileName));
BufferedWriter bos = new BufferedWriter(new FileWriter(loadingFilename));) {
int len = 0;
char[] chr = new char[1024];
while ((len = bis.read(chr)) != -1) {
bos.write(chr,0,len);
bos.flush();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/*第七种:
* 功能: 高效缓冲字符输入输出流
* 返回值类型 : void
* 参数列表: String srcFileName, String loadingFilename
* 方法名: copy7
*/
public static void copy7 (String srcFileName, String loadingFilename) {
try (BufferedReader bis = new BufferedReader(new FileReader(srcFileName));
BufferedWriter bos = new BufferedWriter(new FileWriter(loadingFilename));) {
String line = null;
while ((line = bis.readLine()) != null) {
bos.write(line);
bos.newLine();
bos.flush();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/*第六种:
* 功能: 高效缓冲字节数组输入输出流
* 返回值类型 : void
* 参数列表: String srcFileName, String loadingFilename
* 方法名: copy6
*/
public static void copy6 (String srcFileName, String loadingFilename) {
try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcFileName));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(loadingFilename));) {
int len = 0;
byte[] bys = new byte[1024];
while ((len = bis.read(bys)) != -1) {
bos.write(bys,0,len);
bos.flush();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/*第五种:
* 功能: 高效缓冲字节输入输出流
* 返回值类型 : void
* 参数列表: String srcFileName, String loadingFilename
* 方法名: copy5
*/
public static void copy5 (String srcFileName, String loadingFilename) {
try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcFileName));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(loadingFilename));) {
int by = 0;
while ((by = bis.read()) != -1) {
bos.write(by);
bos.flush();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/*第四种:
* 功能: 高效缓冲字节输入输出流
* 返回值类型 : void
* 参数列表: String srcFileName, String loadingFilename
* 方法名: copy4
*/
public static void copy4 (String srcFileName, String loadingFilename) {
FileInputStream fis = null;
FileOutputStream fos = null;
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
fis = new FileInputStream(srcFileName);
fos = new FileOutputStream(loadingFilename);
bis = new BufferedInputStream(fis);
bos = new BufferedOutputStream(fos);
int by = 0;
while ((by = bis.read()) != -1) {
bos.write(by);
bos.flush();
}
} 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();
}
}
}
}
/*第三种:
* 功能: 一次性拷贝一个字节数组拷贝文件
* 返回值类型 : void
* 参数列表: String srcFileName, String loadingFilename
* 方法名: copy3
*/
public static void copy3 (String srcFileName, String loadingFilename) {
try (FileInputStream fis = new FileInputStream(srcFileName);
FileOutputStream fos = new FileOutputStream(loadingFilename);) {
int len = 0;
byte[] bys = new byte[1024];
while ((len = fis.read(bys)) != -1) {
fos.write(bys, 0, len);
fos.flush();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
/*第二种:
* 功能: 一次性拷贝一个字节拷贝文件
* 返回值类型 : void
* 参数列表: String srcFileName, String loadingFilename
* 方法名: copy2
*/
public static void copy2 (String srcFileName, String loadingFilename) {
try (FileInputStream fis = new FileInputStream(srcFileName);
FileOutputStream fos = new FileOutputStream(loadingFilename);) {//这样写法是程序执行完就自动动释放,就不用写close();了
int by = 0;
while ((by = fis.read()) != -1) {
fos.write(by);
fos.flush();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
/*第一种:
* 功能: 一次性拷贝一个字节拷贝文件
* 返回值类型 : void
* 参数列表: String srcFileName, String loadingFilename
* 方法名: copy1
*/
public static void copy1 (String srcFileName, String loadingFilename) {
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream(srcFileName);
fos = new FileOutputStream(loadingFilename);
int by = 0;
while ((by = fis.read()) != -1) {
fos.write(by);
fos.flush();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception 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();
}
}
}
}
}
使用至少9种方式拷贝文件
最新推荐文章于 2022-07-16 11:59:15 发布