您的位置 首页 java

java的IO字节流复制文件对比(2)

采纳FileInputStream批量文件复制 和 BufferedInputStream缓冲流的文件复制

// RandomAccess file 读写操作

public static void ranfile(File file, File copyFile) throws IOException {

RandomAccessFile r = new RandomAccessFile(file, “r”);

RandomAccessFile rw = new RandomAccessFile(copyFile, “rw”);

Long start = System.nanoTime();

String str = null;

while((str = r.readLine()) != null) {

rw.writeBytes(str);

}

Long end = System.nanoTime();

// 562885

System.out.println(“ranfile复制文件耗时:” + (end – start));

r. close ();

rw.close();

}

// RandomAccessFile 读写操作

public static void ranfileOne(File file, File copyFile) throws IOException {

RandomAccessFile r = new RandomAccessFile(file, “r”);

RandomAccessFile rw = new RandomAccessFile(copyFile, “rw”);

Long start = System.nanoTime();

// 文件复制没有完成

int b = r.read();

rw.write(b);

Long end = System.nanoTime();

// 408812

System.out.println(“ranfileOne复制文件耗时:” + (end – start));

r.close();

rw.close();

}

// RandomAccessFile 读写操作

public static void ranfiletwo(File file, File copyFile) throws IOException {

RandomAccessFile r = new RandomAccessFile(file, “r”);

RandomAccessFile rw = new RandomAccessFile(copyFile, “rw”);

Long start = System.nanoTime();

int t = -1;

while ((t = r.read()) != -1) {

rw.write(t);

}

Long end = System.nanoTime();

// 594078

System.out.println(“ranfiletwo复制文件耗时:” + (end – start));

r.close();

rw.close();

}

// RandomAccessFile 读写操作 批量

public static void ranfileBatchThree(File file, File copyFile) throws IOException {

RandomAccessFile r = new RandomAccessFile(file, “r”);

RandomAccessFile rw = new RandomAccessFile(copyFile, “rw”);

Long start = System.nanoTime();

byte[] bs = new byte[(int) r.length()];

int t = -1;

while ((t = r.read(bs)) != -1) {

rw.write(bs);

}

Long end = System.nanoTime();

// 427245

System.out.println(“ranfileBatchThree复制文件耗时:” + (end – start));

r.close();

rw.close();

}

// RandomAccessFile 读写操作 批量

public static void ranfileBatch(File file, File copyFile) throws IOException {

RandomAccessFile r = new RandomAccessFile(file, “r”);

RandomAccessFile rw = new RandomAccessFile(copyFile, “rw”);

Long start = System.nanoTime();

byte[] bs = new byte[(int) r.length()];

int t = -1;

while ((t = r.read(bs, 0, bs.length)) != -1) {

rw.write(bs, 0, bs.length);

}

Long end = System.nanoTime();

// 427245

System.out.println(“ranfileBatch复制文件耗时:” + (end – start));

r.close();

rw.close();

}

public static void fileStream(File file, File copyFile) throws IOException {

// 使用FileInputStream 和 FileOutputStream 一个字节一个字节读写操作

FileInputStream in = new FileInputStream(file);

FileOutputStream out = new FileOutputStream(copyFile);

Long start = System.nanoTime();

int t = -1;

while ((t = in.read()) != -1) {

out.write(t);

}

Long end = System.nanoTime();

// 871503

System.out.println(“fileStream复制文件耗时:” + (end – start));

in.close();

out.close();

}

// 使用FileInputStream 和 FileOutputStream 批量操作

public static void fileStreamBatchOne(File file, File copyFile) throws IOException {

FileInputStream in = new FileInputStream(file);

FileOutputStream out = new FileOutputStream(copyFile);

Long start = System.nanoTime();

int t = -1;

byte[] bs = new byte[(int) file.length()];

while ((t = in.read(bs)) != -1) {

out.write(bs);

}

Long end = System.nanoTime();

// 609675

System.out.println(“fileStreamBatchOne复制文件耗时:” + (end – start));

in.close();

out.close();

}

// 使用FileInputStream 和 FileOutputStream 批量操作

public static void fileStreamBatch(File file, File copyFile) throws IOException {

FileInputStream in = new FileInputStream(file);

FileOutputStream out = new FileOutputStream(copyFile);

Long start = System.nanoTime();

int t = -1;

byte[] bs = new byte[(int) file.length()];

while ((t = in.read(bs,0,bs.length)) != -1) {

out.write(bs,0,bs.length);

}

Long end = System.nanoTime();

// 609675

System.out.println(“fileStreamBatch复制文件耗时:” + (end – start));

in.close();

out.close();

}

// DataInputStream 和 DataOutputStream 进行读写操作

public static void dataFile(File file, File copyFile) throws IOException {

DataInputStream dis = new DataInputStream(new FileInputStream(file));

DataOutputStream dos = new DataOutputStream(new FileOutputStream(copyFile));

Long start = System.nanoTime();

int t = -1;

while ((t = dis.read()) != -1) {

dos.write(t);

}

Long end = System.nanoTime();

// 691910

System.out.println(“dataFile复制文件耗时:” + (end – start));

dis.close();

dos.close();

}

// DataInputStream 和 DataOutputStream 进行批量读写操作

public static void dataFileBatchOne(File file, File copyFile) throws IOException {

DataInputStream dis = new DataInputStream(new FileInputStream(file));

DataOutputStream dos = new DataOutputStream(new FileOutputStream(copyFile));

Long start = System.nanoTime();

int t = -1;

byte[] bs = new byte[(int) file.length()];

while ((t = dis.read(bs)) != -1) {

dos.write(bs);

}

Long end = System.nanoTime();

// 692854

System.out.println(“dataFileBatchOne复制文件耗时:” + (end – start));

dis.close();

dos.close();

}

// DataInputStream 和 DataOutputStream 进行批量读写操作

public static void dataFileBatch(File file, File copyFile) throws IOException {

DataInputStream dis = new DataInputStream(new FileInputStream(file));

DataOutputStream dos = new DataOutputStream(new FileOutputStream(copyFile));

Long start = System.nanoTime();

int t = -1;

byte[] bs = new byte[(int) file.length()];

while ((t = dis.read(bs,0,bs.length)) != -1) {

dos.write(bs,0,bs.length);

}

Long end = System.nanoTime();

// 692854

System.out.println(“dataFileBatch复制文件耗时:” + (end – start));

dis.close();

dos.close();

}

// BufferedInputStream 和 BufferedOutputStream 进行复制文件操作

public static void bufferedFile(File file, File copyFile) throws IOException {

BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));

BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(copyFile));

Long start = System.nanoTime();

int t = -1;

while ((t = bis.read()) != -1) {

bos.write(t);

bos.flush();

}

Long end = System.nanoTime();

// 1443369

System.out.println(“bufferedFile复制文件耗时:” + (end – start));

bis.close();

bos.close();

}

// BufferedInputStream 和 BufferedOutputStream 进行批量复制文件操作

public static void bufferedFileBatchOne(File file, File copyFile) throws IOException {

BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));

BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(copyFile));

Long start = System.nanoTime();

int t = -1;

byte[] bs = new byte[(int) file.length()];

while ((t = bis.read(bs)) != -1) {

bos.write(bs);

bos.flush();

}

Long end = System.nanoTime();

// 589352

System.out.println(“bufferedFileBatch复制文件耗时:” + (end – start));

bis.close();

bos.close();

}

// BufferedInputStream 和 BufferedOutputStream 进行批量复制文件操作

public static void bufferedFileBatch(File file, File copyFile) throws IOException {

BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));

BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(copyFile));

Long start = System.nanoTime();

int t = -1;

byte[] bs = new byte[(int) file.length()];

while ((t = bis.read(bs,0,bs.length)) != -1) {

bos.write(bs,0,bs.length);

bos.flush();

}

Long end = System.nanoTime();

// 589352

System.out.println(“bufferedFileBatch复制文件耗时:” + (end – start));

bis.close();

bos.close();

}

public static void main(String[] args) throws IOException {

String oldPath = “E:\\ran.txt”;

String newPath = “E:\\one4.txt”;

File oldFile = new File(oldPath);

File copyFile = new File(newPath);

// 视频不可以复制

IOCompare.ranfile(oldFile,copyFile);

// 视频不可以复制

// IOCompare.ranfileOne(oldFile, copyFile);

// IOCompare.ranfiletwo(oldFile, copyFile);

// IOCompare.ranfileBatchThree(oldFile, copyFile);

// 3697745-mp4,2608839-wmv,2592769-mp3,

// IOCompare.ranfileBatch(oldFile, copyFile);

// IOCompare.fileStream(oldFile, copyFile);

// IOCompare.fileStreamBatchOne(oldFile, copyFile);

// 5078728

// IOCompare.fileStreamBatch(oldFile, copyFile);

// IOCompare.dataFile(oldFile, copyFile);

// IOCompare.dataFileBatchOne(oldFile, copyFile);

// 4670388,5995130,2657991

// IOCompare.dataFileBatch(oldFile, copyFile);

// IOCompare.bufferedFile(oldFile, copyFile);

// IOCompare.bufferedFileBatchOne(oldFile, copyFile);

// 3022850,3602277,2991658

// IOCompare.bufferedFileBatch(oldFile, copyFile);

}

一、字符流

(1)Reader是所有字符输入流的父类

(2)Writer是所有字符输出流的父类

(3)字符流以字符(char)为单位进行读写

(4)编码和解码

InputStreamReader,字符输入流,字节数据转字符数据(编码)

OutputStreamWriter,字节输出流,将字符数据转字节数据(解码)

(5)编码格式

可以指定编码格式的:OutputStreamWriter,InputStreamReader

二、字节流

(1)InputStream是所有字节输入流的父类

(2)OutputStream是所有字节流输出流的父类

(3)以字节为单位进行数据的读写

(4)缓冲流中的 void flush()方法,是将缓冲区的数据强制写出

(5)对象流

对象 —> 字节序列 : 对象序列化

字节序列 —> 对象 : 对象反序列化

文章来源:智云一二三科技

文章标题:java的IO字节流复制文件对比(2)

文章地址:https://www.zhihuclub.com/181779.shtml

关于作者: 智云科技

热门文章

网站地图