IO流的了解

1.IO流的区分

        按流向区分:
                输入流 InPutStream  FileInPutStream Reader
                输出流 OutPutStream FileOutPutStream Writer

        按读取类型区分
                字节流 InPutStream  OutPutStream
                字符流 Reader Writer

2.高效缓冲输入流,和输出流

        缓冲输入流 BufferedInPutStream
        缓冲输出流 BufferedOutPutStream

public class Test55 {
public static void main(String[] args) throws  IOException {
BufferedInputStream  bis =  new BufferedInputStream(new FileInputStream("aa.mp4"));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("bb.mp4"));
int b;
while((b= bis.read()) !=-1){
bos.write(b);
}
bis.close();
bos.close();
}
}

 3.刷新flush、 和close方法的区别

        当我们写出的时候,担心有字节没有写出完全,可以用刷新的方法

        一般我们关闭流通道的时候,用close()方法。
        底层他会每次先刷新流通道,调用flush()方法

 4.字节流读写中文的问题

        中文是字符。一个字符占两个字节的位置。所以按照字节去读取的时候,有可能读取到半中文,那么就会造成乱码!!写出的时候,也是按照字节来写出的时候,也有可能把相邻的两个
字节拼接出问,导致,和我们预期输出的中文错乱

public class Test6 {
public static void main(String[] args) throws IOException {
// input(); //测试读取中文,可能出现乱码问题。
output(); //测试写出中文,可能会出现的乱码问题。
}

private static void output() throws IOException {
// TODO 这里还有没完成的功能,需要测试输出流,写出中文乱码问题。
FileOutputStream fos = new FileOutputStream("abc.txt");
//将字符串,转化为字节数组来输出。
fos.write("我读书少,你不要骗我!".getBytes());  
fos.close();
}

//测试读取中文,可能出现乱码问题。
public static void input() throws IOException{
FileInputStream fis =  new FileInputStream("a.txt");
byte[] arr = new byte[3];

int len; //读取到的字节,转化为int类型数据。
while(( len = fis.read(arr)) != -1){
//自己读取,都是半个半个字符读取,可能会造成乱码原因。
System.out.println(new String(arr,0,len));  
}
fis.close();
}
}

 5.字符流 Reader  Writer

        (1)什么时候用字节流,什么时候用字符流

                用字节流
                        用来拷贝文件的时候用字节流。

                用字符流
                        用来把文件中的 文本内容,读取到内存中用的时候用字符流。
                        或者把程序中的字符串,写出到文件中的时候,用字符流。

        (2)字符流能用来拷贝非文本文件吗 

                不可以 

6.高效缓冲字符输入流,和高效缓冲字符输出流

        BufferedReader

        BufferedWriter

public class Test8 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new FileReader("a.txt")); // 创建高效读取流。
BufferedWriter bw = new BufferedWriter(new FileWriter("bbbb.txt")); // 创建高效输出流。

int b; // 读取到的字符数。
while ((b = br.read()) != -1) { // 循环读取 缓冲字符数组序列。
bw.write(b); // 把内存缓冲区的字符数组,输出。
}
// 关闭输入和输出流通道。
br.close();
bw.close();
System.out.println("拷贝完毕!");
}
}

 7.通过io流给文件加密解密

/**
 *  通过io流操作,图片加密。
 *  //加密,是输出的时候,亦或 ^ 一个数。
 *  
 *  解密的时候,把加密过的文件。再次亦或同一个 密钥数。
 *  那么就解密出来了。
 */
public class Test9 {
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream("a.jpeg");
FileOutputStream fos = new FileOutputStream("aa.jpeg");

int b;
while((b=fis.read()) !=-1){
fos.write(b ^ 123);   //加密,是输出的时候,亦或 ^ 一个数。
}
fis.close();
fos.close();
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值