黑马程序员_温习 IO流二 (个人笔记)(字节流 -----转换流)

---------------------- <a href="http://edu.csdn.net"target="blank">ASP.Net+Android+IOS开发</a>、<a href="http://edu.csdn.net"target="blank">.Net培训</a>、期待与您交流! ----------------------


摘要(字节流 -----转换流)



字节流:
|--字节输入流:InputStream 不用刷新,自动输入到容器中
|--字节输出流:OutputStream 不用刷新,自动输出到指定容器


字节输入流:InputStream -->文件输入流:FileInputStream




输入流三种常见读取方法


方法一:建一个刚好长度的字节数组(缺点:无法判断文件长度是否超出内存空间)


概要:调用:int available()方法,获取文件长度


FileInputStream fis = new FileInputStream("Demo.txt");


int num = fis.available();//调用available方法,获取文件长度,并记录


byte[] buf = new byte[num];//建一个字节容器,长度为,文件长度(容易造成内存溢出,因为不知道长度是否超过内存)
fis.read(buf);//将内容存入字节数组


fis.close();//关闭流,字节流不用刷新




方法二:建一个1024整数倍的字节数组(常用)


概要:建一个byte[1024*x]的数组,当容器


byte[] buf = new byte[1024];//建一个1024整数倍的字节数组
int len = 0 //计数器,记录长度
while ((len = fis.read(buf))!=-1)  //若长度不位 -1 则一组一组循环
{
System.out.println(new String(buf,0,len)); //打印一组,并规定长度是 从 0 - 数据长度
}
fis.close();




方法三:一次读一个字节


概要:不用建数组,建一个int用于接收并记录长度即可


int ch = 0; //建一个int 即当计数器 又当容器
while ((ch=fis.read())!=-1)
{
System.out.println((char)ch); //若打印字符,要强转回字符型
}
fis.close();




字节输出流:OutputStream -->文件输出流:FileOutputStream


方法:调用write写入,不用刷新
概要:写入的文件若为文本文件则内容需要转为字节,再写入("abc".getBytes())


FileOutputStream fos = new FileOutputStream("Demo.txt");


fos.write("badie".getBytes());//将写入内容转为字节再写入指定文件


fos.close();关流,没有刷新,自动写入




练习:拷贝图片


概要:读取的内容存在1024整数倍的数组中,写入时要规定长度 0 ~ len
否则,当内容不足1024时,输出很多null


FileOutputStream  fos =null;
FileInputStream  fis = null;
try
{
fos = new FileOutputStream("d:\\2.png");
fis = new FileInputStream("c:\\1.png");
byte[] buf = new byte[1024];
int len = 0;
while ((len = fis.read(buf))!=-1)
{
fos.write(buf,0,len);
}
}
catch (IOException e)
{
..................
}
finally   //有几个流就要关闭几个
{
try
{
if(fos!=null)
fos.close();
}
catch (.......)
{
.........
}
try
{
if(fis!=null)
fis.close();
}
catch (......)
{
.........
}
}


练习:自定义字节缓冲区拷贝 .mp3


概要:一:获取的字节为8位,而返回值要求是int型即32位,这就造成了字节自动提升后数据失真的问题


即当字节为 11111111时,转为int后成 11111111-11111111-11111111-11111111即 -1

若为-1则会结束读取((...)!=-1) ,这是不对的


 二:为了避免这种事的发生可以手动提升 字节&255  或者 字节&0xff 即
 
11111111-11111111-11111111-11111111
  & 00000000-00000000-00000000-00001111
____________________________________
00000000-00000000-00000000-11111111   


这样转型后就避免了出现-1的情况,而且
write()方法写入时,会先将int型转为byte
即,只取最后8位,所以保证了数据的正确性




import java.io.*;
class myBufferedInputStream
{
private InputStream in;
private int pos = 0,count =0; //建一个计数器count 和 指针 pos
private byte[] buf = new byte[1024];
myBufferedInputStream(InputStream in)
{
this.in = in ;
}
public int myread()throws IOException //自制read方法
{
byte b;
if(count==0) //如果计数器为0
{
count = in.read(buf); //则读取一组数据,并记录长度
if(count<0)  //如果长度小于0
return -1;  //表示没有数据了,返回 -1
pos=0;  //否则,先将指针清0
b=buf[pos];  //一个一个读取装有数据的数组并让一个byte变量记录
pos++;  //指针自加
count--;  //长度自减
return b&255;  //返回的byte 要自动升级为int 但会造成错误。所以手动提升
}
else if(count>0) //若计数器大于零,表明已有数据则继续读取
{
b = buf[pos];  
pos++;
count--;
return b&0xff;//和 &255一样
}
return -1;
}
public void myclose()throws IOException //自制 close 方法
{
in.close();
}
}


class Demo2
{
public static void main(String[] args)throws IOException
{
BufferedOutputStream bfos = new BufferedOutputStream(new FileOutputStream("D:\\1.mp3"));
myBufferedInputStream bfis = new myBufferedInputStream(new FileInputStream("D:\\2.mp3"));
int len =0;
while ((len = bfis.myread())!=-1)
{
bfos.write(len);
}
bfos.close();
bfis.myclose();
}

}


练习:读取键盘录入


概要:System.in 标准输入:键盘
 System.out 标准输出:控制台(屏幕)


InputStream in = System.in; //键盘可直接录入到字节流中


StringBuilder  sb  = new StringBuilder();


while (ture)
{
int ch = in.read();
if(ch == '\r')
continue;
if(ch == '\n')
{
String s = sb.toString();
if("over".equals(s))
break;
System.out.println(s.toUpperCase());
sb.delete(0,sb.length());
}
else
sb.apend((char)ch);


}




转换流:主要作用(转换编码表)


|--字节输入流转字符输入流:InputStreamReader


|--字符输出流转字节输出流:OutputStreamWriter


例如:


键盘录入到字符流:


BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in))


字符流输出到控制台


BufferedWriter bufw = new BufferedWriter(new OutputStreamWriter(System.out))


---------------------- <a href="http://edu.csdn.net"target="blank">ASP.Net+Android+IOS开发</a>、<a href="http://edu.csdn.net"target="blank">.Net培训</a>、期待与您交流! ----------------------

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值