拷贝文件
/**
* 使用FileInputStream + FileOutputStream完成文件拷贝
* 使用以上的字节流拷贝文件的时候,不用在意文件类型。任意文件类型都能拷贝。
*
* @author lwx
* @since 20220408
*/
public class CopyTest {
public static void main(String[] args) {
FileInputStream fis = null;
FileOutputStream fos = null;
try {
// 创建输入流对象
fis = new FileInputStream("E:/Users/DIY/IdeaProjects/JavaSE/567/temp1");
// 创建输出流对象
fos = new FileOutputStream("E:/Users/DIY/IdeaProjects/JavaSE/567/temp2");
// 一边读一边写
byte[] bytes = new byte[1024 * 1024]; // 1MB(一次最多拷贝1MB)
int readCount = 0;
while ((readCount = fis.read(bytes)) != -1){
fos.write(bytes, 0, readCount);
}
// 刷新
fos.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();
}
}
}
}
}
拷贝普通文本
/**
* 使用 FileReader + FileWriter 进行拷贝。
* 只能拷贝普通文本。
*
* @author lwx
* @since 20220408
*/
public class CopyTest {
public static void main(String[] args) {
FileReader reader = null;
FileWriter writer = null;
try {
// 创建文件字符输入流
reader = new FileReader("E:/Users/DIY/IdeaProjects/JavaSE/567/File.java");
// 创建文件字符输出流
writer = new FileWriter("E:/Users/DIY/IdeaProjects/JavaSE/567/File1.java");
char[] chars = new char[1024 * 512]; //1MB
int readCount = 0;
while ((readCount = reader.read(chars)) != -1) {
writer.write(chars,0,readCount);
}
// 刷新
writer.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (writer != null) {
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
拷贝目录
/**
* 拷贝目录下所有文件
* 使用File + FileInputStream + FileOutputStream完成拷贝
*
* @author lwx
* @since 20220412
*/
public class CopyAll {
public static void main(String[] args) {
// 拷贝源
File srcFile = new File("E:\\a");
// 拷贝目标
File destFile = new File("E:\\file");
// 调用拷贝方法
copyDir(srcFile, destFile);
}
/**
* 拷贝目录
* @param srcFile 拷贝源
* @param destFile 拷贝目标
*/
private static void copyDir(File srcFile, File destFile) {
FileInputStream fis = null;
FileOutputStream fos = null;
// 获取当前目录下所有的子文件
File[] files = srcFile.listFiles();
for (File file : files) {
if (file.isDirectory()){ //如果是目录
//System.out.println(file.getPath());
// 源路径
String srcDir = file.getAbsolutePath();
// 目标路径
String destDir = destFile.getAbsolutePath() + srcDir.substring(2);
File file1 = new File(destDir);
if (!file1.exists()) { //如果路径不存在
file1.mkdirs();
}
// 递归调用
copyDir(file, destFile);
}else if (file.isFile()) { //如果是文件
//System.out.println(file.getPath());
try {
// 创建输入流对象
fis = new FileInputStream(file.getAbsolutePath());
// 创建输出流对象
fos = new FileOutputStream(destFile.getAbsolutePath() + file.getAbsolutePath().substring(2));
// 一边读一边写
byte[] bytes = new byte[1024 * 1024]; // 1MB(一次最多拷贝1MB)
int readCount = 0;
while ((readCount = fis.read(bytes)) != -1){
fos.write(bytes, 0, readCount);
}
// 刷新
fos.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();
}
}
}
}
//System.out.println(file.getPath());
}
}
}