- /**
- * 需求:用字节流拷贝图片
- * 如果用字符流实现,可能会出现图片打不开
- * (字符流每读取到数据会去查码表进行相应的转换)
- * @throws Exception
- * @since JDK 1.6
- */
- public void copyPic() throws Exception{
- FileInputStream fis = new FileInputStream("c:\\myEclipse.jpg");
- FileOutputStream fos = new FileOutputStream("c:\\copy_myEclipse.jpg");
- int leng = 0;
- byte[] tep = new byte[1024];
- while((leng=fis.read(tep))!=-1){
- fos.write(tep, 0, leng);
- }
- fis.close();
- fos.close();
- }
-
- /**
- * 需求:用还缓冲区的字节流拷贝mp3
- *
- * @throws Exception
- * @since JDK 1.6
- */
- public void copyMp3() throws Exception {
- FileInputStream fis = new FileInputStream("c:\\test.mp3");
- FileOutputStream fos = new FileOutputStream("c:\\copy_test.mp3");
- BufferedInputStream fdis = new BufferedInputStream(fis);
- BufferedOutputStream fdos = new BufferedOutputStream(fos);
- int leng = 0;
- byte[] tep = new byte[1024];
- while ((leng = fdis.read(tep)) != -1) {
- fdos.write(tep, 0, leng);
- }
- fdis.close();
- fdos.close();
- }
复制代码 |