字节流
输入流InputStream
输出流OutputStream
字节流相对于字符流主要处理字节数据,图片、音乐等。
例
public static void main(String[] args)
{
FileOutputStream fo = null;//
FileInputStream fi = null;
try
{
fos = new FileOutputStream("c:\\2.bmp");//写入
fis = new FileInputStream("c:\\1.bmp");//读取
byte[] buf = new byte[1024];
int len =0;
while((len=fis.read())!=-1)
{
fos.write(buf,0,len);
}
}
catch(IOException e)
{
throw new RuntimeException("复制文件失败");
}
finally
{
try
{
fis.close();
}
catch(IOException e)
{
throw new RuntimeException("读取关闭失败");
}
try
{
fos.close();
}
catch(IOException e)
{
throw new RuntimeException("写入关闭失败");
}
}
}
字节流缓冲技术
BufferedInputStream类和BufferedOutputStream类
public static void main(String[] args) throws IOException
{
copy_1();
}
public static void copy_1() throws IOException //抛异常
{
BufferedInputStream bufis = new BufferedInputStream(new FileInputStream("c:\\0.mp3")); //与字符流的逻辑一致
BufferedOutputStream bufos = new BufferedOutputStream(new FileOutputStream("c:\\1.mp3"));
int by =0;
while((by=bufis.read())!=-1)
{
bufos.write(by);
}
bufos.close();
bufis.close();
}