Java学习笔记————————IO流中文件创建、写入、读取、复制的相关笔记
本人小白,以下是B站的Java课程(强烈推荐)的学习笔记,仅作回顾之用,有误望指出。
注:效率测试部分不明显,若想比较程序执行效率,可忽略文件夹、文件创建的部分,用现成的大文件代替。
package buffered;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Arrays;
/*
* 文件复制&效率测试
*
* 1)字节或字符输入流/输出流的测试步骤
* 1、创建两个新的文件夹
* 2、在第一个文件夹里创建一个新的文件
* 3、往这个新的文件里写入数据
* 4、读取这个文件的数据
* 5、写入到第二个文件夹中
* 6、释放资源
*
* 2)缓冲流的测试步骤
* 1、创建两个新的文件夹
* 2、在第一个文件夹里创建一个新的文件
* 3、往这个新的文件里写入数据
* 4、读取这个文件的数据
* 5、写入到第二个文件夹中
* 6、释放资源
* */
public class CopyAndPaste2 {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
method1();
// method2();
}
/*
* 第二种
* 缓冲流
*
*
* */
private static void method2() throws IOException {
// 开始时间
long s=System.currentTimeMillis();
// TODO Auto-generated method stub
// 1、创建两个新的文件夹
File f1=new File("E:\\Java\\documents\\JavaFiles\\DAY29\\src\\method2\\f1");
File f2=new File("E:\\Java\\documents\\JavaFiles\\DAY29\\src\\method2\\f2");
f1.mkdirs();
f2.mkdirs();
// 2、在第一个文件夹里创建一个新的文件
// 创建FileOutputStream对象,构造方法中绑定要输出的目的地
FileOutputStream fos1=new FileOutputStream("E:\\Java\\documents\\JavaFiles\\DAY29\\src\\method2\\f1\\a.txt");
// 3、往这个新的文件里写入数据
// 创建BufferedOutputStream对象,构造方法中传递FileOutputStream对象,提高FileOutputStream对象效率
BufferedOutputStream bos1=new BufferedOutputStream(fos1);
// 使用BufferedOutputStream对象中的方法write,把数据写入内部缓冲区中
bos1.write("丁丁小可爱。".getBytes());
// 使用BufferedOutputStream对象中的方法flush,把内部缓冲区中的数据,刷新到文件中
bos1.flush();
// 释放资源(先调用flush方法刷新数据,可省略不写)
bos1.close();
// 4、读取这个文件的数据
// 创建FileInputStream对象,构造方法中绑定要读取的数据源
FileInputStream fis1=new FileInputStream("E:\\Java\\documents\\JavaFiles\\DAY29\\src\\method2\\f1\\a.txt");
// 创建FileOutputStream对象,构造方法中绑定要输出的目的地
FileOutputStream fos2=new FileOutputStream("E:\\Java\\documents\\JavaFiles\\DAY29\\src\\method2\\f2\\a.txt");
// 创建BufferedOutputStream对象,构造方法中传递FileOutputStream对象,提高FileOutputStream对象效率
BufferedOutputStream bos2=new BufferedOutputStream(fos2);
// 创建BufferedInputStream对象,构造方法中传递FileInputStream对象,提高FileInputStream对象的读取效率
BufferedInputStream bis1=new BufferedInputStream(fis1);
// 5、写入到第二个文件夹中
// 数组方式读取
byte[] bytes1=new byte[1024];
int len=0;
while((len=bis1.read(bytes1))!=-1) {
bos2.write(bytes1,0,len);
}
// 使用BufferedOutputStream对象中的方法flush,把内部缓冲区中的数据,刷新到文件中
bos2.flush();
// 6、释放资源
bos2.close();
// 结束时间
long e=System.currentTimeMillis();
System.out.println("程序耗时"+(e-s)+"毫秒。");
}
/*
* 第一种
* java.io 类 File
* 需要用到的构造方法
* File(String pathname) 通过将给定路径名字符串转换为抽象路径名来创建一个新 File 实例。
* boolean mkdirs()
创建此抽象路径名指定的目录,包括所有必需但不存在的父目录。
*
* */
private static void method1() throws IOException {
// 开始时间
long s=System.currentTimeMillis();
// TODO Auto-generated method stub
// 1、创建两个新的文件夹
File f1=new File("E:\\Java\\documents\\JavaFiles\\DAY29\\src\\method1\\f1");
File f2=new File("E:\\Java\\documents\\JavaFiles\\DAY29\\src\\method1\\f2");
f1.mkdirs();
f2.mkdirs();
// 2、在第一个文件夹里创建一个新的文件
// boolean createNewFile()当且仅当不存在具有此抽象路径名指定名称的文件时,不可分地创建一个新的空文件。
File file1=new File("E:\\Java\\documents\\JavaFiles\\DAY29\\src\\method1\\f1\\a.txt");
file1.createNewFile();
// 3、往这个新的文件里写入数据
// java.io类 FileOutputStream
// 其中的一个构造方法:FileOutputStream(String name) 创建一个向具有指定名称的文件中写入数据的输出文件流。
FileOutputStream fos1=new FileOutputStream(file1);
// void write(byte[] b, int off, int len) 将指定 byte 数组中从偏移量 off 开始的 len 个字节写入此文件输出流。
byte[] bytes1="丁丁小可爱。".getBytes();
System.out.println(Arrays.toString(bytes1));
fos1.write(bytes1);
fos1.close();
// 4、读取这个文件的数据
// 5、写入到第二个文件夹中
FileInputStream fis1=new FileInputStream("E:\\Java\\documents\\JavaFiles\\DAY29\\src\\method1\\f1\\a.txt");
FileOutputStream fos2=new FileOutputStream("E:\\Java\\documents\\JavaFiles\\DAY29\\src\\method1\\f2\\a.txt");
byte[] bytes2=new byte[1024];
int len=0;
while((len=fis1.read(bytes2))!=-1) {
fos2.write(bytes2,0,len);
}
// 6、释放资源
fos2.close();
fis1.close();
// 结束时间
long e=System.currentTimeMillis();
System.out.println("程序耗时"+(e-s)+"毫秒。");
}
}