Java输入及输出处理(I/O流)

 IO流的分类:

按流向分类:输入流、输出流
按操作对象分类: 字节流、字符流
按功能分类: 节点流、处理流

Input 输入
Output 输出
Java.io.File类,文件输入输出类

  • 字节流和字符流:

1,字节流
InputStream(读),OutputStream(写)。
2,字符流:
Reader(读),Writer(写)。
基本的读写操作方式:
因为数据通常都以文件形式存在。
所以就要找到 IO 体系中可以用于操作文件的流对象。
通过名称可以更容易获取该对象。
因为 IO 体系中的子类名后缀绝大部分是父类名称。而前缀都是体现子类功能的名字。

字节流和字符流的区别

1,字节流读取的时候,读到一个字节就返回一个字节。
字符流使用了字节流读到一个或多个字节(中文对应的字节数是两个,在 UTF-8
码表中是 3 个字节)时。先去查指定的编码表,将查到的字符返回。
2,字节流可以处理所有类型数据,如图片,mp3,avi。
而字符流只能处理字符数据。
结论: 只要是处理纯文本数据,就要优先考虑使用字符流。除此之外都用字节流。

字节流

  • 字节输入流(读) InputStream(抽象类)
  • 字节输出流(写) OutputStream (抽象类)

InputStream类常用方法:

  • Int read():

一个字节一个字节的读,返回的是该字节的整数表示形式
Int reda(byte[] b)
从输入流读取若干字节,把这些字节保存到数组b中,返回的是读取到的字节数,如果到了输入流末尾,返回-1
Int read(byte[] b,int off,int len)
从输入流读取若干字节,把这些字节保存到数组b中,off指的是字节数组中开始保存数据的起始下表,len指读取的字节数目,返回的是实际读取到的字节数,如果到了输入流末尾,返回-1

  • Close()

关闭流,每次运行完必须关闭流

  • Availble()

可以从输入流中读取的字节数目

  • 子类FileInputStream:

New FileInputStream(File file)
New FileInputStream(String path)

//读取文件
public class FileInputStreamTest {
    public static void main(String[] args) {
        FileInputStream fis = null;
        try {
            //输入流FileInputStream
            fis = new FileInputStream("D:/test/test.txt");
            //可以读取到的字节数
            //System.out.println(fis.available());
            //借助输入流方法读取文件
            int data;
//            while((data=fis.read())!=-1){
//                System.out.print((char)data);
//            }
            byte [] b = new byte[fis.available()];
            while((data = fis.read(b))!=-1){
                for(int i=0;i < b.length;i++){
                    System.out.print((char)b[i]);
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            try {
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

OutputStream常用方法:

  • Write():

一个字节一个字节往外写
Write(byte[] buf)
Write(byte[] b,int off,ine len)

  • Close:

关闭流

  • Flush:

强制将缓冲区清空
FileOutputStream:
New FileOutputStream(File file)
New FileOutputStream(String path)
New FileOutputStream(String path,boolean append):
可以指定覆盖或追加文件内容,false为参数是为覆盖,true为追加,不写boolean参数是默认覆盖

//给文件输出内容
public class FileOutputStreamTest {
    public static void main(String[] args) {
        FileOutputStream fos = null;
        try {
             fos = new FileOutputStream("D:/test/test.txt");
             String str = "好好学习,天天向上";
             //将字符串打散为一个字节数组
             byte[] b= str.getBytes();
             fos.write(b,0,b.length);
             System.out.println("文件已更新");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            try {
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
  • 字符流:

Reader类常用方法:

  • read()
  • read(char[] c)
  • read(char[]c,int off,int len)
  • close()

子类InputStreamReader常用的构造方法
可以指定字符编码格式
InputStreamReader(InputStream in)
InputStreamReader(InputStream in,String charsetName
FileReader类是InputStreamReader的子类
FileReader(File file)
FileReader(String name)
该类只能按照本地平台的字符编码来读取数据,用户不能指定其他的字符编码类型
System.out.println(System.getProperty(“file.encoding”)); 获得本地平台的字符编码类型
BufferedReader(缓冲流)常用的构造方法
BufferedReader(Reader in)
子类BufferedReader特有的方法
readLine()

//读取文件中字符
public class FileRaderTest {
    public static void main(String[] args){
        //创建字符输入流对象
        FileReader fr = null;
        
        try {
            //声明读取文件的地址
            fr = new FileReader("D:/test/test.txt");
            StringBuffer sb = new StringBuffer();
            char[] ch = new char[1024];
            int b ;
            while((b=fr.read(ch))!=-1){
                sb.append(ch);
            }
            System.out.println(sb);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            try {
                fr.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

Writer类常用方法

  • write(String str)
  • write(String str,int off,int len)
  • void close()
  • void flush():清空缓存

子类OutputStreamWriter常用的构造方法
OutputStreamWriter(OutputStream out)
OutputStreamWriter(OutputStream out,String charsetName)
FileWriter类是OutputStreamWriter的子类
以下两种方法都可以重载,指定一个boolean类型变量,指定是覆盖还是替换。false为参数是为覆盖,true为追加,不写boolean参数是默认覆盖
FileWriter (File file)
FileWriter (String name)
该类只能按照本地平台的字符编码来写数据,用户不能指定其他的字符编码类
BufferedWriter类是Writer类的子类
BufferedWriter类带有缓冲区
BufferedWriter常用的构造方法
BufferedWriter(Writer out)

//往文件中输出字符
public class FileWriterTest {
    public static void main(String[] args) {
        Writer wr = null;
        
        try {
            wr = new FileWriter("D:/test/test.txt",false);
            String words = "hello 梦";
            wr.write(words);
            wr.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            try {
                wr.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        
    }
}

二进制文件读取

音频视频文件就是二进制文件

  • DataInputStream类

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

  • DataOutputStream类

FileOutputStream的子类
与FileOutputStream类结合使用写二进制文件

public class DataTest {
    public static void main(String[] args) {
        //声明二进制输入流对象
        DataInputStream  dis = null;
        FileInputStream fis = null;
        
        //声明二进制输入流对象
        DataOutputStream dos = null;
        FileOutputStream fos = null;
        try {
            //声明需要读取的图片的地址
            fis = new FileInputStream("D:\\test\\1558667107843.jpg");
            dis = new DataInputStream(fis);
            //声明复制的图片保存的地址
            fos = new FileOutputStream("D:\\test2\\new1558667107843.jpg");
            dos = new DataOutputStream(fos);
            int temp;
            while((temp=dis.read())!=-1){
                dos.write(temp);
            }
            System.out.println("复制成功");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            try {
                fis.close();
                dis.close();
                dos.close();
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        
    }
}

序列化和反序列化

  • 二进制的字节序列

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

  • 常见异常:

  NotSerializableException(无法序列化),没有实现Serializable接口

  • **序列化步骤:
  1. 实现Serializable接口
  2. 创建对象输出流
  3. ObjectOutputStream(序列化)
  4. 调用writeObject()方法将对象写入文件
  5. 关闭对象输出流
  6. 使用集合保存对象,可以将集合中的所有对象序列化
  • 反序列化步骤
  1. 实现Serializable接口
  2. 创建对象输出流
  3. ObjectInputStream(序列化)
  4. 调用readObject()方法将对象写入文件
  5. 关闭对象输出流
  6. 如果向文件中使用序列化机制写入多个对象,那么反序列化恢复对象时,必须按照写入的顺序读取**

transient关键字: 该属性不被序列化 屏蔽某些敏感字段的序列化

//创建学生类对象,实现Serializable接口
public class Student implements Serializable{
    private String name;
    private int age;
    
    private transient String password;
    
    
    public Student() {
        super();
    }
    
    public Student(String name, int age) {
        super();
        this.name = name;
        this.age = age;
    }
    
    public Student(String name, int age, String password) {
        super();
        this.name = name;
        this.age = age;
        this.password = password;
    }

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String getPassword() {
    return password;
}

public void setPassword(String password) {
    this.password = password;
}
}

//序列化与反序列化学生对象
public class SeriaStuTest {
    public static void main(String[] args) {
        Student stu = new Student("小明",17);
        //声明序列化对象及对象输出流对象
        ObjectOutputStream oos = null;
        FileOutputStream fos = null;
        
        //声明反序列化对象及对象输入流对象
        ObjectInputStream ois = null;
        FileInputStream fis = null;
        try {
            //序列化
            fos = new FileOutputStream("D:\\test\\student.txt");
            oos= new ObjectOutputStream(fos);
            oos.writeObject(stu);
            
            //反序列化
            fis = new FileInputStream("D:\\test\\student.txt");
            ois = new ObjectInputStream(fis);
            Student stus =(Student)ois.readObject();
            System.out.println("反序列化后:"+stus.getName()+stus.getAge()+stus.getPassword());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }finally{
            try {
                fos.close();
                oos.close();
                fis.close();
                ois.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            
        }
    }
}

猜你喜欢

转载自www.cnblogs.com/big-data-sky/p/11031470.html