当前位置:网站首页>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();
}
}
边栏推荐
- 代码实例详解【可重入锁】和【不可重入锁】区别?
- transformer一统天下?depth-wise conv有话要说
- Introduction to FPGA (II) - one out of two selector
- Access数据库无法连接
- 干货|语义网、Web3.0、Web3、元宇宙这些概念还傻傻分不清楚?(中)
- Access database cannot connect
- 2、 Container_
- JVM内存溢出和内存泄漏的区别
- Audio and video technology development weekly | 255
- Pytest interface automated testing framework | confitest.py
猜你喜欢
随机推荐
Redis为什么这么快?Redis的线程模型与Redis多线程
There are various signs that apple is expected to support AV1
请问下有人知道FlinkSQL 的 Retrack 在哪里可以指定吗? 网上资料只看到API 代码设
Introduction to FPGA (III) - 38 decoder
空洞卷积详解(输入输出大小分析)
尤雨溪向初学者推荐Vite 【为什么使用Vite】
el-form 每行显示两列,底部按钮居中
Flink 在 讯飞 AI 营销业务的实时数据分析实践
大量if else判断如何优化?@Valib详解
The difference between JVM memory overflow and memory leak
Audio and video+
Real time synchronization and conversion of massive data based on Flink CDC
Flutter's learning path
pytest接口自动化测试框架 | pytest之fixture介绍
DS-24C/DC220V时间继电器
Jsj-3/ac220v time relay
Pytest interface automated testing framework | common plug-ins of pytest
Flink's real-time data analysis practice in iFLYTEK AI marketing business
uniapp h5、app引用外部在线js
Is it easy to find a job after programmer training?









