------<a href= "http://www.itheima.com" target= "blank" >java training, Android training, iOS training,. NET training </a>, looking forward to working with you
Communication! ------
By learning about IO, we know that there is a very useful method in InputStream: the Read () method learns to find its underlying array, then think
To define a myread () to achieve the same effect.
The code is as follows:
Import java.io.*;
public class ByteCopyMp3
{
public static void Main (string[] args) throws IOException
{
Copy_1 ();
}
public static void Copy_1 () throws IOException
{
Mybufferedinputstream bufis=new Mybufferedinputstream (New FileInputStream ("D:\\kugou\\ Leslie"-When Love Has
Into the past "Farewell My Concubine". mp3 "));
Bufferedoutputstream bufos=new Bufferedoutputstream (New FileOutputStream ("D:\\pos.mp3"));
int by=0;
while ((By=bufis.myread ())!=-1)
{
Bufos.write (by);
}
Bufis.myclose ();
Bufos.close ();
}
}
Class Mybufferedinputstream
{
Private InputStream in;
Private byte[] By=new byte[1024];
private int pos=0,count=0;
Mybufferedinputstream (InputStream in)
{
This.in=in;
}
public int Myread () throws IOException
{
if (count==0)
{
Count=in.read (by);
if (count<0)
return-1;
pos=0;
BYTE B=by[pos];
pos++;
count--;
return b;
}
else if (count>0)
{
BYTE B=by[pos];
pos++;
count--;
return b;
}
return-1;
}
public void Myclose () throws IOException
{
In.close ();
}
}
Operation Result:
Original file properties:
Files after copying:
Comparison found: The original file is much larger than the copied file, indicating that no replication was successful. What is the problem?
By looking at the material: the original InputStream inside the Read method encapsulates a number of "& Operations" to ensure the integrity of the data.
Although the code is fine, the Read method we define is the return value of type int, but the byte array actually operates on bytes.
Like the return b above, where B is the byte type. Then the b&255 can be replaced with return B above to return b&255
Look at the results:
Image
Copy's done!
------<a href= "http://www.itheima.com" target= "blank" >java training, Android training, iOS training,. NET training </a>, looking forward to working with you
Communication! ------
Customizing the Read () method in InputStream