I/O输入输出处理(下)

目录

Reader类的常用方法

FileReader类

BufferedReader类

使用BufferedReader读取文本文件

Writer类的常用方法

FileWriter类

BufferedWriter类

使用BufferedWriter写文件

读写二进制文件 

序列化和反序列化


Reader类的常用方法

方法说明
int read()从输入流中读取单个字符,返回读取的字符数据
int read(byte[] c)从输入流中最多读取c.length个字符,保存到字符数组c中,返回实际读取的字符数
int read(char[] c,int off,int len)从输入流中读取最多len个字符,保存到字符数组c中,保存的位置从off开始,返回实际的字符数
void close()关闭流

FileReader类

构造方法:FileReader(File file)、FileReader(String name)

FileReader读取文件:

  • 引入资源
  • 创建FileReader对象
  • 调用FileReader()读取文件
  • 关闭字符流

FileReader类是Reader的孙子类,该类只能按照本地平台的字符编码来读取数据,用户不能指定其它的字符编码类型(System.getProperty("file.encoding")可获取本地平台的字符编码类型)

 使用字符流读取文件产生中文乱码的原因:

  • 文件编码格式和程序环境的编码格式不一致

解决方案:

  • 方式一:手工调整文件编码格式和程序环境编码格式一致
  • 方式二:使用Reader子类InputStreamReader 
  • InputStreamReader(InputStream in)
  • InputStreamReader(InputStream in,String charsetName)
public class InputStreamReaderStu {
    public static void main(String[] args) {
        Reader fr=null;
        try {
           FileInputStream fis= new FileInputStream("D:/iofile2.text");
           fr=new InputStreamReader(fis,"UTF-8");
//通过InputStreamReader(InputStream in,String charsetName)方法来转换编码格式

            StringBuffer s = new StringBuffer();
            char[] ch = new char[1024];
            int len=-10;
            while((len=fr.read(ch))!=-1){
                s.append(ch);
            }
            System.out.println(ch);

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

BufferedReader类

BufferedReader类是Reader类的常用子类,带有缓冲区

常用构造方法:BufferedReader(Reader in)

特有方法:readLine()按行读

使用BufferedReader读取文本文件

Writer类的常用方法

方法说明
void write(String str)将str字符串里包含的字符输出到指定的输出流中
void write(String str,int off,int len)        将str字符串里从off位置开始,长度为len的多个字符输出到输出流中
void close()关闭输出流
void flush()刷新输出流

 

 

 

 

  

 

FileWriter类

FileWriter类是Writer的孙子类

  •   FileWriter(File file)
  •   FileWriter(String name)
  •   带boolean类型的参数构造方法

 该类只能按照本地平台的字符编码来写数据,用户不能指定其他的字符编码类型

使用FileWriter写入文件中文乱码问题如何解决?

使用writer子类OutputStreamWriter 

  •   InputStreamWriter(OutputStream out)
  •   InputStreamWriter(OutputStream out,String charsetName)

BufferedWriter类

BufferedReader类是Reader类的常用子类,带有缓冲区

BufferedWriterBufferedReader的流方向正好相反,BufferedWriter是把一批数据写到缓冲区,当缓冲区满的时候,再把缓冲区的数据写到字符输出流中。这可以避免每次都执行物理写操作,从而提高io操作的效率。

常用构造方法:BufferedWriter(Writer out)

使用BufferedWriter写文件

字符流总结:

  • 字符输入流
  • Reader-InputStreamReader-FileReader-BufferedReader
  • readLine()
  • 字符输出流
  • Writer-OutputStreamWriter-FileWriter-BufferedWriter
  • write()
  • newLine()

读写二进制文件 

  • DateInputStream类
  • FileInputStream的子类
  • 与FileInput Stream类结合使用读取二进制文件

(1)引入相关类。

        import java.io.FileInputStream;

        import java.io.DateInputStream;

(2)构造一个数据输入流对象。

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

        DateInputStream dis=new DateInputStream(fis)

(3)利用数据输入流类的方法读取二进制文件中的数据。

        dis.readInt();

        dis.readByte();

(4)关闭数据输入流。

        dis.close();

  • DataOutputStream类
  • FileOutStream的子类
  • 与FileOutputSteam类结合使用写二进制文件

(1)引入相关类。

        import java.io.FileInputStream;

        import java.io.DateInputStream;

(2)构造一个数据输出流对象。

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

        DateOutputStream dos=new DateOutputStream(fos)

(3)利用数据输出流类的方法将数据写入二进制文件中。

        dos.write();

(4)关闭数据输出流。

        dos.close();

以上可利用来实现copy图片:

public class CopyImage {
    public static void main(String[] args) {
        DataInputStream dis=null;
        FileInputStream fis=null;

        DataOutputStream dos=null;
        FileOutputStream fos=null;

        try {
            fis=new FileInputStream("resource/aaa.jpg");
            dis=new DataInputStream(fis);

            fos=new FileOutputStream("aaacopy3.jpg");
            dos=new DataOutputStream(fos);

            int temp;
            while ((temp=dis.read())!=-1){
                dos.write(temp);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                dos.close();
                fos.close();
                fis.close();
                dis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

序列化和反序列化

序列化是将对象的状态写入到特定的流中的过程;反序列化则是从特定的流中获取数据重新构建对象的过程。

  • 对象输出流ObjectOutputStream
  • 结合FileOutputStream使用,实现对象的序列化
  • writeObject(Object)
  • 对象输入流ObjectInputStream
  • 结合FileInputStream使用,实现对象的反序列化
  • Object readObject()
序列化的步骤反序列化的步骤

使用集合保存对象,可以将集合中的所有对象序列化。

  

 

 

 

 

 

 

 

 

 

 

 

 

 

public class SeriaStu {
    public static void main(String[] args) {
        Student student = new Student("张三", "male", 21);
        FileOutputStream fos=null;
        ObjectOutputStream oos=null;
        try {
            fos = new FileOutputStream("D:/stuseria.txt");
            oos = new ObjectOutputStream(fos);
            oos.writeObject(student);

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            try {
                oos.close();
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        FileInputStream fis=null;
        ObjectInputStream ois=null;
        try {
            fis=new FileInputStream("d:/stuseria.txt");
            ois=new ObjectInputStream(fis);
            Student stu= (Student)ois.readObject();
            System.out.println(stu.getAge());
            System.out.println(stu.getName());

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }finally {
            try {
                fis.close();
                ois.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


    }
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值