码迷,mamicode.com
首页 > 其他好文 > 详细

黑马程序员——字节流

时间:2014-09-07 01:03:24      阅读:283      评论:0      收藏:0      [点我收藏+]

标签:http   os   io   使用   java   ar   文件   数据   art   

 
 
 
package Excr19;
 
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
 
/*
字节流 媒体文件
思路:
1.用字节读取流对象和图片关联
2.用字节写入流对象创建一个图片文件用于储存获取到的图片数据
3.通过循环读写,完成数据的储存
4.关闭资源
 
只是本个磁盘进行拷贝
 
*/
public class Demo9 {
 
/**
 * @param args
 */
public static void main(String[] args) {
// TODO Auto-generated method stub
FileOutputStream fileOutputStream=null;
FileInputStream fileInputStream=null;
try {
fileOutputStream =new FileOutputStream("C:\\Taylor Swift - You Belong With Me1.mp3");
fileInputStream=new FileInputStream("C:\\Taylor Swift - You Belong With Me.mp3");
byte [] buf=new byte[1024*5];
int len=0;
while ((len=fileInputStream.read(buf))!=-1) {
fileOutputStream.write(buf,0,len);
}
} catch (IOException e ) {
throw new RuntimeException("复制文件失败");
// TODO: handle exception
}
finally{
try{
if(fileInputStream!=null);
fileInputStream.close();
 
}
 
catch (IOException e ){
throw new RuntimeException("读取文件失败");
}
 
}
 
try{
if(fileInputStream!=null);
fileInputStream.close();
 
}
 
catch (IOException e ){
throw new RuntimeException("写入数据失败");
}
 
}
 
}
 
package Excr19;
 
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
 
public class Demo10 {
 
/**map3的复制:通过缓冲区
 * 1.定义数组
 * 2.定义指针
 * 3.定义计数器
 * @param args
 * @throws IOException 
 */
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
long start=System.currentTimeMillis();
 copy_1();
long end=System.currentTimeMillis();
System.out.println((end-start)+"毫秒");
}
//通过字节流的缓冲区完成复制
public static void copy_1() throws IOException {
BufferedInputStream bufferedInputStream=new BufferedInputStream(new FileInputStream("C:\\Taylor Swift - Love Story.mp3"));
BufferedOutputStream bufferedOutputStream=new BufferedOutputStream(new FileOutputStream("D:\\Taylor Swift - Love Story1.mp3"));
int by=0;
while ((by=bufferedInputStream.read())!=-1) {
bufferedOutputStream.write(by);
}
bufferedOutputStream.close();
bufferedInputStream.close();
}
 
}
package Excr19;
 
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
 
public class Demo13 {
 
/**
 * @param args
 * @throws IOException 
 */
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
//获取键盘录入对象
InputStream inputStream=System.in;
//将字节流对象转成字符流对象,使用转换率,inputStreamReader
InputStreamReader inputStreamReader=new InputStreamReader(inputStream);
//为了提高效率,将字符流串进行缓冲区技术高效操作,使用BufferedReader
BufferedReader bufferedReader=new BufferedReader(inputStreamReader);
String lineString=null;
while ((lineString=bufferedReader.readLine())!=null) {
if ("over".equals(lineString)) {
break;
}
System.out.println(lineString.toUpperCase());
}
bufferedReader.close();
}
 
 
}
package Excr19;
 
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
 
public class Demo14 {
 
/**
 * 字符转字节
 * 录得时候是字符,但是存的时候是字节
 * @param args
 * @throws IOException 
 */
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
InputStream inputStream=System.in;
InputStreamReader inputStreamReader=new InputStreamReader(inputStream);
OutputStream out=System.out;
BufferedReader bufferedReader=new BufferedReader(inputStreamReader);
OutputStreamWriter outputStreamWriter=new OutputStreamWriter(out);
BufferedReader bufferedReader2=new BufferedReader(outputStreamWriter);
String lineString=null;
while((lineString=bufferedReader.readLine())!=null){
if ("over".equals(lineString)) {
break;
outputStreamWriter.write(lineString.toUpperCase()+"\r\n");
outputStreamWriter.flush();
}
}
bufferedReader.close();
}
 
}
 
 

详情请查看:http://edu.csdn.net/heima

黑马程序员——字节流

标签:http   os   io   使用   java   ar   文件   数据   art   

原文地址:http://www.cnblogs.com/yzcorange/p/3960011.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!