4.14-4.15学习总结 IO流:缓冲流+转换流+序列化流+打印流+压缩流+Commons—io工具包+Hutool工具包
图片加密操作:
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class test {public static void main(String[] args) throws IOException {FileInputStream fis=null;FileOutputStream fos=null;try{fis=new FileInputStream("src\\lyx.jpg");fos=new FileOutputStream("lyx1.jpg");byte[] buffer=new byte[1024];int len;while((len=fis.read(buffer))!=-1){for(int i=0;i<len;i++){buffer[i]=(byte)(buffer[i]^5);}fos.write(buffer,0,len);}}catch(IOException e){e.printStackTrace();}finally{if(fos!=null){try{fos.close();}catch(IOException e){e.printStackTrace();}}if(fis!=null){try{fis.close();}catch(IOException e){e.printStackTrace();}}}}
}
加密前:
加密后:
缓冲流:
字节缓冲流拷贝文件操作:
将a.txt中的内容拷贝到b.txt中。
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class test {public static void main(String[] args) throws IOException {BufferedInputStream bis=new BufferedInputStream (new FileInputStream("src\\a.txt"));BufferedOutputStream bos=new BufferedOutputStream (new FileOutputStream("src\\b.txt"));byte[] bytes=new byte[1024];int len;while((len=bis.read(bytes))!=-1){bos.write(bytes,0,len);}bos.close();bis.close();}
}
字节/字符缓冲流:
转换流:
是字符流和字节流之间的桥梁。
序列化流:
属于字节流,负责输出数据。与之对应的反序列化流,负责输入数据。
创建的对象要继承Serializable接口
反序列化流:
打印流:
只能进行输出。
字节打印流:
特有方法可以实现数据的原样写出。
printf()方法中%s是字符串,%n是换行,%c可以把字符变成大写,%b是bool类型的占位符,%d是小数的占位符,
字符打印流:
输出语句就是一个打印流。
解压缩流/压缩流:
Java中只能识别zip格式的压缩包。
Commons—io工具包:
使用步骤:
常见方法:
Hutool工具包: