java IO流(二)文件的复制

时间:2022-05-15 20:59:16
    在上篇我们提到了文本文件的读写,既然能够读写。那么我们这篇说下文件的复制,有以下方法:

利用FileWriter和FileReader复制文本文件:

  • 方式一
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;

public class WriterAndReaderTest {
public static void main(String[] args) throws IOException {
//被复制的文件路径
String txt = "G:" + File.separator + "Test.txt";
//复制文件的路径
String copy = "G:" +File.separator + "Test1.txt";
//读取
Reader reader = new FileReader(txt);
//写入
Writer writer = new FileWriter(copy);
//若不用外部变量接收会丢失数据
int temp = 0;
while((temp = reader.read()) != -1){
writer.write(temp);
}
//关闭流
reader.close();
writer.close();
}
}
  • 方式二
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;

public class WriterAndReaderTest1 {
public static void main(String[] args) throws IOException {
// 被复制的文件路径
String txt = "G:" + File.separator + "Test.txt";
// 复制文件的路径
String copy = "G:" + File.separator + "Test1.txt";
//读取
Reader reader = new FileReader(txt);
//写入
Writer writer = new FileWriter(copy);
//使用字符数组接收
char[] c = new char[1024];

while( reader.read(c) != -1){
writer.write(c);
}
//关闭流
reader.close();
writer.close();
}
}
  • 方式三(利用字符流缓冲区复制文本文件<提高效率>)
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;

public class WriterAndReaderTest2 {
public static void main(String[] args) throws IOException {
// 被复制的文件路径
String txt = "G:" + File.separator + "Demo.txt";
// 复制文件的路径
String copy = "G:" + File.separator + "Demo1.txt";
// 读取
Reader reader = new FileReader(txt);
// 写入
Writer writer = new FileWriter(copy);
//创建缓冲区对象
BufferedReader br = new BufferedReader(reader);
BufferedWriter bw = new BufferedWriter(writer);
//读取行,直到返回null
String line = null;
while ((line = br.readLine()) != null) {
bw.write(line);
}
////关闭缓冲区就是关闭缓冲区中的流对象
bw.close();
br.close();

}
}
    虽然字符流可以复制文本文件,但比如图片之类的复制就会导致文件无法打开或者损坏,那我们就需要用到字节流来读写。可以使用以下方法:

利用FileInputStream和FileOutputStream复制图片:

  • 方式一
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class InputOutputStreamTest {
public static void main(String[] args) throws IOException {
//被复制图片的路径
String png = "G:" + File.separator + "01.png";
//复制图片的路径
String copy = "G:" + File.separator + "02.png";
//读取
InputStream in = new FileInputStream(png);
//写入
OutputStream os = new FileOutputStream(copy);

int temp = 0;
while((temp = in.read()) != -1){
os.write(temp);
}
//关闭流
os.close();
in.close();
}
}
  • 方式二(利用字节流缓冲区复制图片<提高效率>)
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class InputOutputStreamTest1 {
public static void main(String[] args) throws IOException {
//被复制图片的路径
String png = "G:" + File.separator + "01.png";
//复制图片的路径
String copy = "G:" + File.separator + "02.png";
//读取
InputStream in = new FileInputStream(png);
//写入
OutputStream os = new FileOutputStream(copy);
//创建缓冲区
BufferedInputStream bis = new BufferedInputStream(in);
BufferedOutputStream bos = new BufferedOutputStream(os);

byte[] buf = new byte[1024];
int temp = 0;
while((temp = bis.read(buf)) != -1){
bos.write(buf, 0 ,temp);
}

//关闭缓冲区
bos.close();
bis.close();
}
}

总结

看了上面的实例相信大家应该明白了,到底该什么时候用字符流和字节流了
1:字节流读取二进制文件(如图片、mp3)
2:字符流用来操作字符文件较多的文本
用缓冲区能够在读写大文件的时候有效提高效率