黑马程序员技术交流社区
标题:
IO—字节流拷贝图片
[打印本页]
作者:
bowen-xiao
时间:
2015-1-19 13:46
标题:
IO—字节流拷贝图片
/**
* 需求:用字节流拷贝图片
* 如果用字符流实现,可能会出现图片打不开
* (字符流每读取到数据会去查码表进行相应的转换)
* @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();
}
复制代码
作者:
bowen-xiao
时间:
2015-1-19 14:47
代码简写一下
public void inAoutTest3() throws Exception {
// 先接收键盘录入
// 创建一个转化流,把字节流转换成字符流
// 创建字符流缓冲区,为提高读取效率
BufferedReader bfr = new BufferedReader(new InputStreamReader(System.in));
// 用同样的方法创建带字符流转换缓冲输出流
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
String stb = null;
while((stb = bfr.readLine()) != null){
if("over".equals(stb)){
bfr.close();
System.exit(0);
}
// System.out.println(stb.toUpperCase());
// 下面写法与上面效果一样
bw.write(stb.toUpperCase());
bw.newLine();
bw.flush();
}
}
复制代码
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2