JAVA三种文件读写操作,四种文件拷贝操作
一、文件读写操作
1.使用FileWriter和FileReader,对文件内容按字符读取
String dir = "D:\\aaa\\a.txt";
File file = new File(dir);
//如果文件不存在,创建文件
if (!file.exists())
file.createNewFile();
//创建FileWriter对象
FileWriter writer = new FileWriter(file);
//向文件中写入内容
writer.write("the first way to write and read");
writer.flush(); //刷新该流的缓冲。
writer.close();
//创建FileReader对象,读取文件中的内容
FileReader reader = new FileReader(file);
char[] ch = new char[100];
reader.read(ch);
for(char c:ch) {
System.out.print(c);
}
System.out.println();
reader.close();
2.使用包装类BuffredReader和BufferedWriter,对文件内容进行整行读取
String dir = "D:\\aaa\\b.txt";
File file = new File(dir);
//如果文件不存在,创建文件
if (!file.exists())
file.createNewFile();
//创建BufferedWriter对象并向文件写入内容
BufferedWriter bw = new BufferedWriter(new FileWriter(file));
//向文件中写入内容
bw.write("the second way to write and read");
bw.flush();
bw.close();
//创建BufferedReader读取文件内容
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
while ((line=br.readLine())!=null) {
System.out.println(line);
}
br.close();
3.使用FileInputStream和FileOutputStream,这种方法以字节的形式写入文件,读取文件时先读取字节数组,再将字节数组转换为字符串形式
String dir = "D:\\aaa\\c.txt";
File file = new File(dir);
//如果文件不存在,创建文件
if (!file.exists())
file.createNewFile();
//创建FileOutputStream对象,写入内容
FileOutputStream fos = new FileOutputStream(file);
//向文件中写入内容
fos.write("the third way to write and read".getBytes());
fos.close();
//创建FileInputStream对象,读取文件内容
FileInputStream fis = new FileInputStream(file);
byte[] bys = new byte[100];
while (fis.read(bys, 0, bys.length)!=-1) {
//将字节数组转换为字符串
System.out.println(new String(bys));
}
fis.close();
二、文件拷贝操作以及效率对比
四种拷贝方式概述:1.字节流的基本流:一次读写一个字节
2.字节流的基本流:一次读写一个字节数组
3.字节缓冲流:一次读写一个字节
4.字节缓冲流:一次读写一个字节数组
这里我们以一次读取10个字节为例
public static void main(String[] args) throws IOException {
long start = System.currentTimeMillis();
mothed1();//字节流的基本流:一次读写一个字节
long end = System.currentTimeMillis();
System.out.println("字节流的基本流:一次读写一个字节" + (end - start) / 1000.0 + "秒");
start = System.currentTimeMillis();
mothed2();//字节流的基本流:一次读写一个字节数组
end = System.currentTimeMillis();
System.out.println("字节流的基本流:一次读写一个字节数组" + (end - start) / 1000.0 + "秒");
start = System.currentTimeMillis();
mothed3();//字节缓冲流:一次读写一个字节
end = System.currentTimeMillis();
System.out.println("字节缓冲流:一次读写一个字节" + (end - start) / 1000.0 + "秒");
start = System.currentTimeMillis();
mothed4();//字节缓冲流:一次读写一个字节数组
end = System.currentTimeMillis();
System.out.println("字节缓冲流:一次读写一个字节数组" + (end - start) / 1000.0 + "秒");
}
private static void mothed4() throws IOException {
String pathstart = "D:\\javadevelop\\javastart\\src\\javaNotebook\\3.txt";
String pathend = "src\\copy4.txt";
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(pathstart));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(pathend));
int len;
byte[] b = new byte[10];
while ((len = bis.read(b)) != -1) {
bos.write(b, 0, len);
}
bos.close();
bis.close();
}
private static void mothed3() throws IOException {
String pathstart = "D:\\javadevelop\\javastart\\src\\javaNotebook\\3.txt";
String pathend = "src\\copy3.txt";
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(pathstart));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(pathend));
int b;
while ((b = bis.read()) != -1) {
bos.write(b);
}
bos.close();
bis.close();
}
private static void mothed2() throws IOException {
String pathstart = "D:\\javadevelop\\javastart\\src\\javaNotebook\\3.txt";
String pathend = "src\\copy2.txt";
FileInputStream fis = new FileInputStream(pathstart);
FileOutputStream fos = new FileOutputStream(pathend);
int len;
byte[] b = new byte[10];
while ((len = fis.read(b)) != -1) {
fos.write(b, 0, len);
}
fos.close();
fis.close();
}
private static void mothed1() throws IOException {
String pathstart = "D:\\javadevelop\\javastart\\src\\javaNotebook\\3.txt";
String pathend = "src\\copy1.txt";
FileInputStream fis = new FileInputStream(pathstart);
FileOutputStream fos = new FileOutputStream(pathend);
int b;
while ((b = fis.read()) != -1) {
fos.write(b);
}
fos.close();
fis.close();
}
运行结果: