当前位置:网站首页>Problems encountered in byte stream exercises and Solutions
Problems encountered in byte stream exercises and Solutions
2022-07-26 12:17:00 【Wang Xiaoya】
stem :
Use byte stream and byte buffer stream respectively , Realize text file / picture / Copy of video files . And compare their efficiency in data replication ( Tips : Date class
Test process and problems encountered :
Mingming video is only short 3 second , Take up memory 1.25MB, But byte stream copy video often calculation method 2 ( Read and write one byte array at a time ) But with 7854 millisecond , There's something wrong ,
Later, the inspection revealed that

about 3 second 1.25MB In the video , The byte stream replication speed is as follows :
Method 1 : Byte stream copy video is often calculated ( Read and write one byte at a time )
Total time consuming :7645 millisecond
-----------
Method 2 : Byte stream copy video is often calculated ( Read and write one byte array at a time )
Total time consuming :10 millisecond
-----------
Method 3 : The byte buffer stream reads and writes one byte at a time
Total time consuming :26 millisecond
-----------
Method four : The byte buffer stream reads and writes one byte array at a time )
Total time consuming :30 millisecond
Process ended , Exit code 0
Try to enlarge the video size to 20MB above :

give the result as follows :
Method 1 : Byte stream copy video is often calculated ( Read and write one byte at a time )
Total time consuming :232989 millisecond
-----------
Method 2 : Byte stream copy video is often calculated ( Read and write one byte array at a time )
Total time consuming :299 millisecond
-----------
Method 3 : The byte buffer stream reads and writes one byte at a time
Total time consuming :564 millisecond
-----------
Method four : The byte buffer stream reads and writes one byte array at a time )
Total time consuming :648 millisecond
Process ended , Exit code 0
Finally, the comparison results are obtained :
Sort by output speed :
Byte stream The frequent calculation of copying video ( Read and write one byte at a time Array ) > Byte buffer stream Read and write one at a time byte > Byte buffer stream Read and write one byte at a time Array > Byte stream The frequent calculation of copying video ( Read and write one at a time byte )
The complete code is as follows :
package com.B.IOStream_14.IODemo;
import java.io.*;
//1.25MB3 Second video
//1. Use byte stream and byte buffer stream respectively , Realize text file / picture / Copy of video files .
// And compare their efficiency in data replication ( Tips : Date class )
public class A1Copymp4 {
public A1Copymp4() throws FileNotFoundException {
}
public static void main(String[] args) throws IOException {
// Record the start time
long startTime1 = System.currentTimeMillis();
// Copy video
// Call method 1 :
time1();
System.out.println(" Method 1 : Byte stream copy video is often calculated ( Read and write one byte at a time )");
// Record the end time
long endTime1 = System.currentTimeMillis();
System.out.println(" Total time consuming :" + (endTime1 - startTime1) + " millisecond ");
System.out.println("-----------");
// Call method 2 :
long startTime2 = System.currentTimeMillis();
time2();
System.out.println(" Method 2 : Byte stream copy video is often calculated ( Read and write one byte array at a time )");
long endTime2 = System.currentTimeMillis();
System.out.println(" Total time consuming :" + (endTime2 - startTime2) + " millisecond ");
System.out.println("-----------");
// Call method three :
long startTime3 = System.currentTimeMillis();
time3();
System.out.println(" Method 3 : The byte buffer stream reads and writes one byte at a time ");
long endTime3 = System.currentTimeMillis();
System.out.println(" Total time consuming :" + (endTime3 - startTime2) + " millisecond ");
System.out.println("-----------");
// Call method four :
long startTime4 = System.currentTimeMillis();
time4();
System.out.println(" Method four : The byte buffer stream reads and writes one byte array at a time )");
long endTime4 = System.currentTimeMillis();
System.out.println(" Total time consuming :" + (endTime4 - startTime2) + " millisecond ");
}
// Method 1 : Byte stream copy video is often calculated ( Read and write one byte at a time )
public static void time1() throws IOException {
// Create a byte input stream object from the data source
FileInputStream fis = new FileInputStream("D:\\ My document duck \\ Picture material \\ A young man in high spirits .mp4");
// Create a byte output stream object based on the destination
FileOutputStream fos = new FileOutputStream("E:\\yangzhou\\ Back end level 8-16\\ checkpoint 14_ file IO flow \\02_ Byte stream \\ Homework \\Homework\\ Is envy duck .mp4");
// Read and write data , Copy the picture ( Read one byte at a time , Write one byte at a time )
int by;
while ((by = fis.read()) != -1) {
fos.write(by);
}
// Release resources
fis.close();
fos.close();
}
// Method 2 : Byte stream copy video is often calculated ( Read and write one byte array at a time )
public static void time2() throws IOException {
// Create a byte input stream object from the data source
FileInputStream fis = new FileInputStream("D:\\ My document duck \\ Picture material \\ A young man in high spirits .mp4");
// Create a byte output stream object based on the destination
FileOutputStream fos = new FileOutputStream("E:\\yangzhou\\ Back end level 8-16\\ checkpoint 14_ file IO flow \\02_ Byte stream \\ Homework \\Homework\\ Is envy duck .mp4");
// Read and write data , Copy the picture ( Read one byte array at a time , Write one byte array at a time )
byte[] bys = new byte[1024];
int len;
while ((len = fis.read(bys)) != -1) {
fos.write(bys, 0, len);
}
// Release resources
fis.close();
fos.close();
}
// Method 3 : The byte buffer stream reads and writes one byte at a time
public static void time3() throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("D:\\ My document duck \\ Picture material \\ A young man in high spirits .mp4"));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("E:\\yangzhou\\ Back end level 8-16\\ checkpoint 14_ file IO flow \\02_ Byte stream \\ Homework \\Homework\\ Is envy duck .mp4"));
// Read and write data , Copy the picture ( Read one byte at a time , Write one byte at a time )
int by;
while ((by = bis.read()) != -1) {
bos.write(by);
}
// Release resources
bos.close();
bis.close();
}
// Method four : The byte buffer stream reads and writes one byte array at a time
public static void time4() throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("D:\\ My document duck \\ Picture material \\ A young man in high spirits .mp4"));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("E:\\yangzhou\\ Back end level 8-16\\ checkpoint 14_ file IO flow \\02_ Byte stream \\ Homework \\Homework\\ Is envy duck .mp4"));
// Read and write data , Copy the picture ( Read one byte array at a time , Write one byte array at a time )
byte[] bys = new byte[1024];
int len;
while ((len = bis.read(bys)) != -1) {
bos.write(bys, 0, len);
}
// Release resources
bos.close();
bis.close();
}
}
package com.B.IOStream_14.IODemo;
import java.io.*;
//11:02 Long ,37.5MB Big video
//1. Use byte stream and byte buffer stream respectively , Realize text file / picture / Copy of video files .
// And compare their efficiency in data replication ( Tips : Date class )
public class A1Copyavi {
public static void main(String[] args) throws IOException {
// Record the start time
long startTime1 = System.currentTimeMillis();
// Copy video
// Call method 1 :
time1();
System.out.println(" Method 1 : Byte stream copy video is often calculated ( Read and write one byte at a time )");
// Record the end time
long endTime1 = System.currentTimeMillis();
System.out.println(" Total time consuming :" + (endTime1 - startTime1) + " millisecond ");
System.out.println("-----------");
// Call method 2 :
long startTime2 = System.currentTimeMillis();
time2();
System.out.println(" Method 2 : Byte stream copy video is often calculated ( Read and write one byte array at a time )");
long endTime2 = System.currentTimeMillis();
System.out.println(" Total time consuming :" + (endTime2 - startTime2) + " millisecond ");
System.out.println("-----------");
// Call method three :
long startTime3 = System.currentTimeMillis();
time3();
System.out.println(" Method 3 : The byte buffer stream reads and writes one byte at a time ");
long endTime3 = System.currentTimeMillis();
System.out.println(" Total time consuming :" + (endTime3 - startTime2) + " millisecond ");
System.out.println("-----------");
// Call method four :
long startTime4 = System.currentTimeMillis();
time4();
System.out.println(" Method four : The byte buffer stream reads and writes one byte array at a time ");
long endTime4 = System.currentTimeMillis();
System.out.println(" Total time consuming :" + (endTime4 - startTime2) + " millisecond ");
}
// Method 1 : Byte stream copy video is often calculated ( Read and write one byte at a time )
public static void time1() throws IOException {
// Create a byte input stream object from the data source
FileInputStream fis = new FileInputStream("E:\\yangzhou\\ Back end level 8-16\\ checkpoint 14_ file IO flow \\02_ Byte stream \\ video \\18_ Byte stream copy video .avi");
// Create a byte output stream object based on the destination
FileOutputStream fos = new FileOutputStream("E:\\yangzhou\\ Back end level 8-16\\ checkpoint 14_ file IO flow \\02_ Byte stream \\ Homework \\Homework\\ Teach me byte stream replication .avi");
// Read and write data , Copy the picture ( Read one byte at a time , Write one byte at a time )
int by;
while ((by = fis.read()) != -1) {
fos.write(by);
}
// Release resources
fis.close();
fos.close();
}
// Method 2 : Byte stream copy video is often calculated ( Read and write one byte array at a time )
public static void time2() throws IOException {
// Create a byte input stream object from the data source
// Create a byte input stream object from the data source
FileInputStream fis = new FileInputStream("E:\\yangzhou\\ Back end level 8-16\\ checkpoint 14_ file IO flow \\02_ Byte stream \\ video \\18_ Byte stream copy video .avi");
// Create a byte output stream object based on the destination
FileOutputStream fos = new FileOutputStream("E:\\yangzhou\\ Back end level 8-16\\ checkpoint 14_ file IO flow \\02_ Byte stream \\ Homework \\Homework\\ Teach me byte stream replication .avi");
// Read and write data , Copy the picture ( Read one byte array at a time , Write one byte array at a time )
byte[] bys = new byte[1024];
int len;
while ((len = fis.read(bys)) != -1) {
fos.write(bys, 0, len);
}
// Release resources
fis.close();
fos.close();
}
// Method 3 : The byte buffer stream reads and writes one byte at a time
public static void time3() throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("E:\\yangzhou\\ Back end level 8-16\\ checkpoint 14_ file IO flow \\02_ Byte stream \\ video \\18_ Byte stream copy video .avi"));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("E:\\yangzhou\\ Back end level 8-16\\ checkpoint 14_ file IO flow \\02_ Byte stream \\ Homework \\Homework\\ Teach me byte stream replication .avi"));
// Read and write data , Copy the picture ( Read one byte at a time , Write one byte at a time )
int by;
while ((by = bis.read()) != -1) {
bos.write(by);
}
// Release resources
bos.close();
bis.close();
}
// Method four : The byte buffer stream reads and writes one byte array at a time
public static void time4() throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("E:\\yangzhou\\ Back end level 8-16\\ checkpoint 14_ file IO flow \\02_ Byte stream \\ video \\18_ Byte stream copy video .avi"));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("E:\\yangzhou\\ Back end level 8-16\\ checkpoint 14_ file IO flow \\02_ Byte stream \\ Homework \\Homework\\ Teach me byte stream replication .avi"));
// Read and write data , Copy the picture ( Read one byte array at a time , Write one byte array at a time )
byte[] bys = new byte[1024];
int len;
while ((len = bis.read(bys)) != -1) {
bos.write(bys, 0, len);
}
// Release resources
bos.close();
bis.close();
}
}
边栏推荐
- Pytoch deep learning quick start tutorial -- mound tutorial notes (II)
- Real time synchronization and conversion of massive data based on Flink CDC
- Customize browser default right-click menu bar
- pytest接口自动化测试框架 | pytest常用插件
- Ssj-21b time relay
- 腾讯云与智慧产业事业群(CSIG)调整组织架构,成立数字孪生产品部
- Introduction to FPGA (II) - one out of two selector
- DS-112时间继电器
- Introduction to FPGA (III) - 38 decoder
- Pytorch深度学习快速入门教程 -- 土堆教程笔记(二)
猜你喜欢

el-form 每行显示两列,底部按钮居中

Li Kai: the interesting and cutting-edge audio and video industry has always attracted me

Hit the blackboard and draw the key points: a detailed explanation of seven common "distributed transactions"

敲黑板画重点:七种常见“分布式事务”详解

自定义浏览器默认右击菜单栏

详解勒让德变换与共轭函数

What is per title encoding?

QT入门引导 及其 案例讲解

CVPR 2022 new SOTA for monocular depth estimation new CRFs: neural window fullyconnected CRFs

Some common writing methods and skills
随机推荐
Yuancosmos daily | yuancosmos social app "Party Island" product off the shelves; Guangzhou Nansha yuanuniverse industrial agglomeration zone was unveiled; The inter ministerial joint conference system
Flink 在 讯飞 AI 营销业务的实时数据分析实践
File类的学习过程中出现的问题及解决方法
Audio and video technology development weekly | 255
干货|语义网、Web3.0、Web3、元宇宙这些概念还傻傻分不清楚?(中)
pytest接口自动化测试框架 | 使用装饰器修饰需要运行的用例
Introduction to FPGA (II) - one out of two selector
V00 - 年纪大了,想做啥就做啥吧
Redis为什么这么快?Redis的线程模型与Redis多线程
Oracle的Windows版本能在linux中使用吗?
10. 509. Introduction to PKCs file format
Flutter's learning path
网络协议:TCP/IP协议
MySQL之数据查询(聚合函数)
Why is redis so fast? Redis threading model and redis multithreading
Jsj-3/ac220v time relay
You Yuxi recommends vite to beginners [why use vite]
《多线程下ThreadLocal使用场景实例》
JSJ-3/AC220V时间继电器
Customize browser default right-click menu bar