JavaSE-IO

    技术2022-07-11  69

    字节流与字符流常见的16个流:文件专属:FileInputStream使用:FileOutputStreamFileReaderFileWriter 缓冲流BufferedReader其他三个缓冲流代码步骤类似 转换(字节流--->字符流)InputStreamReader 数据流(很少用)标准输入输出流对象专属流ObjectOutputStream(序列化)ObjectInputStream(反序列化)

    字节流与字符流

    InputStream:字节输入流 OutputStream:字节输出流 Reader:字符输入流 Writer:字符输出流

    常见的16个流:

    文件专属:

    FileInputStreamFileOutputStreamFileReaderFileWriter

    FileInputStream使用:

    1、先获取文件输入流对象,可能抛出FileNotFoundException异常

    FileInputStream fileInputStream=new FileInputStream("D:\\CHENGXUYUAN\\IO\\text\\temp.txt");

    2、从输入流对象中读取字节,可能抛出IOException异常

    //read()读一个字节,读到尾返回-1 int read = fileInputStream.read(); System.out.println(read); //读取数组长度的字节,返回成功读取的个数 byte[] bytes=new byte[4]; int read1 = fileInputStream.read(bytes); System.out.println(new String(bytes,0,read1));

    3、关闭流

    fileInputStream.close();

    FileOutputStream

    1、获取文件输出流对象

    FileOutputStream fileOutputStream=new FileOutputStream("myFile");

    2、调用write()方法写入信息

    byte[] bytes={97,98,99,100}; fileOutputStream.write(bytes);

    3、清空管道

    fileOutputStream.flush();

    4、关闭流

    FileReader

    FileReader fr=null; try { fr=new FileReader("myFile"); char[] chars=new char[4]; int read=fr.read(chars); System.out.println(new String(chars,0,read)); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if(fr!=null){ try { fr.close(); } catch (IOException e) { e.printStackTrace(); } } }

    FileWriter

    具体代码写法步骤和上述差不多,自己根据需要查文档

    缓冲流

    BufferedReaderBufferedWriterBufferedInputStreamBufferedOutputStream

    缓冲流与上述普通文件流的区别是,缓冲流使用的时候使用内存作为缓冲, 不用自己定义byte[]数组和char[]数组来存储读到的内容,直接可以转成字符串,字符等。

    BufferedReader

    1、获取普通字符输入流作为节点流

    Reader rd=new FileReader("myFile");

    2、获取缓冲字符输入流作为包装流

    BufferedReader br=new BufferedReader(rd);

    3、读取内容

    String str=null; while((str=br.readLine())!=null){ System.out.println(str); }

    4、关闭流

    //包装流关闭节点流会自动关闭 br.close();

    其他三个缓冲流代码步骤类似

    转换(字节流—>字符流)

    InputStreamReaderOutputStreamWriter

    InputStreamReader

    1、获取字节输入流

    FileInputStream fis=new FileInputStream("d://desktop//test.txt");

    2、将自己流包装成字符流

    nputStreamReader isr=new InputStreamReader(fis,"utf8");

    3、读取文件信息

    int i; while((i=isr.read()) != -1){ System.out.println((char)i); }

    4、关闭流

    isr.close();

    数据流(很少用)

    DataInputStreamDataOutputStream

    标准输入输出流

    PrintWriterPrintStream

    对象专属流

    ObjectInputStream(反序列化)ObjectOutputStream(序列化)

    ObjectOutputStream(序列化)

    序列化 :将Java内存中的对象写到硬盘中

    要序列化的对象必须实现Serializable接口,这个接口没有方法只是一个标识性的接口,JVM看到这个接口就知道这个对象需要序列化,会自动生成一个序列化版本号。

    序列化版本号:序列化对象的唯一标识,在编译时生成,反序列化时根据版本号找到对应的类。 自动生成序列化版本号的缺点:每次修改或重新编译时,JVM会认为这是一个全新的类,会生成不同的版本号,那么以前序列化的对象要反序列化就会失败,因此会在类中设置一个固定的唯一的版本号。 transient:用这个关键字声明的成员变量会被JVM忽略不参加序列化 实现步骤: 1、定义参加序列化的类

    public class FileTest implements Serializable { private static final long serialVersionUID = -3083673595387013015L; private int id; private transient String name;//设置name不参加序列化 }

    2、

    FileTest test=new FileTest(); ObjectOutputStream os=new ObjectOutputStream(new FileOutputStream("object")); os.writeObject(test); os.flush(); os.close();

    ObjectInputStream(反序列化)

    反序列化:将硬盘中序列化的文件读到内存中成为对象

    ObjectInputStream ois=new ObjectInputStream(new FileInputStream("object")); Object obj=ois.readObject(); System.out.println(obj); ois.close();
    Processed: 0.010, SQL: 9