1.字节流的4种复制方式
(1)基本字节流一次读写一个字节
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class CopyDemoA {
public static void main(String[] args) throws IOException {
FileInputStream in = new FileInputStream("d:\\海绵宝宝1.jpg");
FileOutputStream out = new FileOutputStream("d:\\海绵宝宝2.jpg");
int len = 0;
while ((len = in.read()) != -1) {
out.write(len);
out.flush();
}
in.close();
out.close();
}
}
(2)基本字节流一次读写一个字节数组
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class CopyDemoB {
public static void main(String[] args) throws IOException {
FileInputStream in = new FileInputStream("D:\\海绵宝宝1.jpg");
FileOutputStream out = new FileOutputStream("D:\\海绵宝宝2.jpg");
byte[] bytes = new byte[1024 * 8];
int len = 0;
int i = 1;
while ((len = in.read(bytes)) != -1) {
System.out.println("循环次数为:" + (i++));
out.write(bytes, 0, len);
out.flush();
}
in.close();
out.close();
}
}
(3)高效字节流一次读写一个字节
import java.io.*;
public class CopyDemoC {
public static void main(String[] args) throws IOException {
BufferedInputStream in = new BufferedInputStream(new FileInputStream("D:\\海绵宝宝1.jpg"));
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream("D:\\海绵宝宝2.jpg"));
int len = 0;
while ((len = in.read()) != -1) {
out.write(len);
out.flush();
}
in.close();
out.close();
}
}
(4)高效字节流一次读写一个字节数组
public class CopyDemoD {
public static void main(String[] args) throws IOException {
BufferedInputStream in = new BufferedInputStream(new FileInputStream("D:\\海绵宝宝1.jpg"));
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream("D:\\海绵宝宝2.jpg"));
byte[] bytes = new byte[1024];
int len = 0;
while ((len = in.read(bytes)) != -1) {
out.write(bytes, 0, len);
out.flush();
}
in.close();
out.close();
}
}
2.字符流的5种复制方式
(1)基本字符流一次读写一个字符
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class CopyDemoE {
public static void main(String[] args) throws IOException {
FileReader fileReader = new FileReader("d:\\Test1.txt");
FileWriter fileWriter = new FileWriter("d:\\Test2.txt");
int ch = -1;
while ((ch = fileReader.read()) != -1) {
fileWriter.write(ch);
fileWriter.flush();
}
fileWriter.close();
fileReader.close();
}
}
(2)基本的字符流一次读写一个字符数组
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class CopyDemoF {
public static void main(String[] args) throws IOException {
FileReader fileReader = new FileReader("d:\\Test3.txt");
FileWriter fileWriter = new FileWriter("d:\\Test4.txt");
int ch = -1;
char[] chars = new char[1024];
while ((ch = fileReader.read(chars)) != -1) {
fileWriter.write(chars);
fileWriter.flush();
}
fileWriter.close();
fileReader.close();
}
}
(3)高效的字符流一次读写一个字符
import java.io.*;
public class CopyDemoG {
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new FileReader("d:\\Test5.txt"));
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("d:\\Test6.txt"));
int ch = -1;
while ((ch = bufferedReader.read()) != -1) {
bufferedWriter.write(ch);
bufferedWriter.flush();
}
bufferedWriter.close();
bufferedReader.close();
}
}
(4)高效的字符流一次读写一个字符数组
import java.io.*;
public class CopyDemoH {
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new FileReader("d:\\Test7.txt"));
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("d:\\Test8.txt"));
int ch = -1;
char[] chars = new char[1024];
while ((ch = bufferedReader.read(chars)) != -1) {
bufferedWriter.write(chars);
bufferedWriter.flush();
}
bufferedWriter.close();
bufferedReader.close();
}
}
(5)高效的字符流一次读写一行字符串
import java.io.*;
public class CopyDemoI {
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new FileReader("d:\\Test9.txt"));
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("d:\\Test10.txt"));
String line = null;
while ((line = bufferedReader.readLine()) != null) {
bufferedWriter.write(line);
bufferedWriter.flush();
}
bufferedWriter.close();
bufferedReader.close();
}
}
3.递归复制文件夹及其中的文件
import java.io.*;
public class CopyDemoTest {
public static void main(String[] args) {
File oldFile = new File("D:\\Test");
File newFile = new File("E:\\");
copy1(oldFile, newFile);
}
public static void copy1(File oldFile, File newFile) {
if (oldFile.isDirectory()) {
File file = new File(newFile, oldFile.getName());
file.mkdirs();
File[] files = oldFile.listFiles();
for (File f : files) {
copy1(f, file);
}
} else {
File file = new File(newFile, oldFile.getName());
copy2(oldFile, file);
}
}
public static void copy2(File oldFile, File newFile) {
try {
FileInputStream in = new FileInputStream(oldFile);
FileOutputStream out = new FileOutputStream(newFile);
byte[] bytes = new byte[1024];
in.read(bytes);
out.write(bytes);
in.close();
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}