Byte stream copying mp3 files (with buffer), mp3 Buffer
// Custom Buffer
Import java. io .*;
Class MyBufferedInputStream
{
Private byte [] buf = new byte [1, 1024];
Private InputStream in;
Private int pos = 0, count = 0;
MyBufferedInputStream (InputStream in ){
This. in = in;
}
/*
Read one byte at a time from the buffer byte array.
*/
Public int myRead (){
// Read hard disk data through the in object and store the data in buf
If (count = 0 ){
Count = in. read (buf );
If (count <0)
Return-1;
Pos = 0;
Byte B = buf [pos];
Count --;
Pos ++;
Return B;
} Else if (count> 0 ){
Byte B = buf [pos];
Count --;
Pos ++;
Return B;
}
Return-1;
}
Public void myClose (){
In. close ();
}
}
Call Buffer:
Import java. io. *; class CopyMp3 {public static void main (String [] args) throws Exception {long start = System. currentTimeMillis (); copy_1 (); long end = System. currentTimeMillis (); System. out. println (end-start);} // copy public static void copy_2 () throws Exception {BufferedInputStream bufd = new BufferedInputStream (new FileInputStream ("c: \ 1.mp3 "); BufferedOutputStream bufos = new Buff EredOutputStream (new FileOutputStream ("c: \ 2.mp3"); int by = 0; while (by = bufcm. myRead ())! =-1) {bufos. write (by);} bufos. close (); bufcm. myClose ();}}
-----------------------
Problem:
The copied file contains 0 bytes.
0000-0001
1111-1110
1111-1111
Byte:-1 ---> int:-1
He will add 1 to the front. In order to add 0 to the front, & 255 is required.
Minimum four places & 15
1 1 1 1
Minimum 8 bits & 255
1111 1111
How does the data copied from the buffer zone to another buffer zone pass through the memory? I don't feel like this, but the answer to the question is not ......
If the intermediate variables do not pass through the intermediate variables, they cannot be copied. The advantage of the intermediate variables exists in the memory. Therefore, data copied from the buffer to another buffer must go through the memory.
Copy the file content in java
The idea is to read the file content, store it in the byte stream, and output it to different places. This is the copy function.
The procedure is as follows:
Public static void copyfile (String source, String des ){
File s = new File (source );
File d = new File (des );
Try {
FileInputStream FCM = new FileInputStream (s );
FileOutputStream fos = new FileOutputStream (d );
Byte [] data = new byte [FCM. available ()];
If (FS. read (data)> 0 ){
Fos. write (data );
}
FCM. close ();
Fos. close ();
} Catch (Exception e ){
// TODO: handle exception
}
}
Public static void main (String [] args ){
Copyfile ("c:/a.txt", "c:/upload/a.txt ");
}