当前位置:网站首页>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();
}
}
边栏推荐
- Flink 在 讯飞 AI 营销业务的实时数据分析实践
- pytest接口自动化测试框架 | pytest之fixture介绍
- Pytest interface automation test framework | use decorators to decorate the use cases that need to be run
- pytest接口自动化测试框架 | pytest配置文件
- Use the jsonobject object in fastjason to simplify post request parameter passing
- 微软关闭了两种攻击途径:Office 宏、RDP 暴力破解
- Map函数统计字符出现的次数
- Ds-112 time relay
- 【Mysql约束】
- Oracle的Windows版本能在linux中使用吗?
猜你喜欢

Implementation of dynamic and static libraries (packaging dynamic and static libraries for others to use)

Pytorch深度学习快速入门教程 -- 土堆教程笔记(二)

扫雷小游戏——轻松玩上瘾(C语言版)

11 "pocket" universities in China! Running on campus and leaving the school before accelerating

Jsj-3/ac220v time relay

Beauty salon management system unified management system?

Here blog: running a large language model in a production environment - overview of the reasoning framework

Audio and video technology development weekly | 255

The difference between JVM memory overflow and memory leak

Dry goods semantic web, Web3.0, Web3, metauniverse, these concepts are still confused? (medium)
随机推荐
什么是回调函数,对于“回”字的理解
Pytest interface automated testing framework | confitest.py
JVM内存溢出和内存泄漏的区别
剑指 Offer 24. 反转链表
Overseas app push (Part 2): Channel Integration Guide for overseas manufacturers
Audio and video+
X.509、PKCS文件格式介绍
pytest接口自动化测试框架 | pytest之fixture介绍
3D point cloud course (VIII) -- feature point matching
面试官:如何处理高并发?
14.2字节流学习
Pytorch深度学习快速入门教程 -- 土堆教程笔记(一)
Here blog: running a large language model in a production environment - overview of the reasoning framework
Detailed explanation of Legendre transformation and conjugate function
On the construction and management of low code technology in logistics transportation platform
Pytest interface automation test framework | use decorators to decorate the use cases that need to be run
行业案例|指标中台如何助力银行业普惠金融可持续发展
腾讯云与智慧产业事业群(CSIG)调整组织架构,成立数字孪生产品部
Introduction to FPGA (II) - one out of two selector
物联网设备加密的意义