1、 IO流的概念(什么是IO流,IO流的分类有哪些,主要的类或者接口对应有哪些)
流的基类:InputStream OutputStream Reader Writer
文件流:Filexxx
处理流:Bufferedxxx
数据单位分类:
字符流:char类型 Reader Writer 处理输入流和输出流
字节流 : byte 类型 inputStream OutputStream 处理输入流和输出流
数据流向分类:
输入流 :InputStream
输出流 :OutputStream
按照流的角色分类:
节点流 :直接作用在文件上FileInputStream FileOutputStream FileReader FileWriter
处理流:
缓冲流:作用在已有流上 BufferedInputStream BufferedOutputStream BufferedReader BufferedWriter
转换流:将输入的字节流转换为输入的字符流 InputStreamReader 将输出字符流转换为输出字节流OutputStreamWriter
标准的输出输入流:System.in 标准的输入流 从键盘读取数据 System.out 标准的输出流 默认控制台输出
数据流:DateInputStream和DateOutputStream
分别“套接”在inputStream和OutputStream子类的流上
作用:用于读取或写出基本数据类型的变量或字符窜
对象流:ObjectInputStream ,ObjectOutputStream
2、 FileReader和FileWriter读写数据操作的代码
@Test
public void teatReaderFileWriter(){
//1.创建file类的对象,指明读入和写出的文件
FileReader fe=null;
FileWriter fw=null;
try {
File file = new File("hello.txt");
File file1 = new File("hello1.txt");
//2.创建流的对象
fe = new FileReader(file);
fw = new FileWriter(file1);
//3.读入和写出的操作
char[] arr=new char[1024];
int len=0;
while ((len = fe.read(arr)) != -1) {
fw.write(arr, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
//4,关闭流
try {
if (fw!=null) {
fw.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if (fe!=null) {
fe.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
3、 FileInputStream和FileOutputStream的复制文件的方式
@Test
public void FileImg(){
FileInputStream from=null;
FileOutputStream to=null;
//创建File类指明读入和写出的对象
try {
File file = new File("QQ图片20191021105340.jpg");
File file1 = new File("QQ图片201910211053401.jpg");
//创建流对象
from = new FileInputStream(file);
to = new FileOutputStream(file1);
//读入和写出的操作
byte[] arr=new byte[1024];
int len=0;
while ((len = from.read(arr)) != -1) {
to.write(arr, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (to!=null) {
to.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if (from!=null) {
from.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
4、 缓冲流实现文本文件和非文本文件的复制方式
@Test
public void FileBufferInputStream(){
String srcpath="QQ图片20191021105340.jpg";
String outputh="QQ图片201910211053403.jpg";
long start=System.currentTimeMillis();
Copy(srcpath, outputh);
long end=System.currentTimeMillis();
System.out.println("复制过来所花时间" + (end - start));
}
public static void Copy(String srcpath,String outputh){
BufferedInputStream bufferedInputStream=null;
BufferedOutputStream bufferedOutputStream=null;
try {
bufferedInputStream = new BufferedInputStream(new FileInputStream(new File(srcpath)));
bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(new File(outputh)));
byte[] bytes=new byte[1024];
int len=0;
while ((len=bufferedInputStream.read(bytes))!=-1){
bufferedOutputStream.write(bytes,0,len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (bufferedOutputStream!=null) {
bufferedOutputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if (bufferedInputStream!=null){
bufferedInputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
5、 转换流的使用
@Test
public void test5() {
FileInputStream fr=null;
FileOutputStream fout=null;
InputStreamReader in = null;
OutputStreamWriter out=null;
try {
fr = new FileInputStream(new File("hello1.txt"));
fout=new FileOutputStream(new File("hello3.txt"));
in = new InputStreamReader(fr,"UTF-8");
out=new OutputStreamWriter(fout,"gbk");
char[] c=new char[1024];
int len=0;
while ((len = in.read(c)) != -1) {
out.write(c, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
}
try {
if(fr!=null) {
in.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if(fout!=null) {
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
6、 转换流和数据流的基本使用。
@Test
public void test6(){
DataOutputStream dos = null;
try {
dos = new DataOutputStream(new FileOutputStream("hello.txt"));
dos.writeUTF("我我我");
dos.flush();
dos.writeInt(1);
dos.flush();
dos.writeBoolean(false);
dos.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(dos!=null){
dos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Test
public void test7(){
DataInputStream dis=null;
try {
dis = new DataInputStream(new FileInputStream("hello.txt"));
String name = dis.readUTF();
int age = dis.readInt();
boolean isMan = dis.readBoolean();
//读取数据顺序要与写入时顺序一致
System.out.println("name="+name);
System.out.println("age="+age);
System.out.println("isMan="+isMan);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (dis!=null) {
dis.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
7、对象流的类或者接口有哪些,基本操作是什么
ObjectInputStream ObjeatOutputStream
作用:用于存储和读取基本数据类型或对象的处理流。
8、什么是序列化和反序列化。
需要让某个对象支持序列化机制
为了让某个类可序列化该类必须实现以下两个接口之一
Serializable
Externalizable
序列化:把内存中java对象转换成二进制 持久地保存在磁盘上 或通过网络进行传输
使用ObjectOutputStream
反序列化:将磁盘文件中的对象还原为内存中的一个java对象
使用ObjectInputStream



