Java_IO流

IO流概念

在编程语言的I/O类库中常使用流这个抽象概念.它代表有能力产出数据的数据源对象或者是与能力接受数据的接受端对象."流"屏蔽了实际的I/O设备中处理数据的细节.
  • 一个流可以理解为一个数据的序列,输入流表示从一个源读取数据,输入流表示向一个目标写数据.

IO流的分类

Java_IO流_第1张图片
2.PNG

字符流 (特点:处理纯文本的效率比较高)

字符输入流

public static void main(String[] args) throws IOException {
      //读取(一次读一个)
       FileReader fileReader = new FileReader("index.tex");
//     int num = fileReader.read();//只能读一个
//     System.out.println((char)num);
       int num = -1;//默认没有值
       String str = "";
       while ((num=fileReader.read()) != -1) {
        str = str+(char)num;
    }
       System.out.println(str);
       fileReader.close();//关闭
}
 public static void main(String[] args) throws Exception{
     
         FileReader fileReader = new FileReader("index.tex");
         char[] buff = new char[1024];
         int length = 0;//读取的长度
         while ((length =fileReader.read(buff)) != -1) {
            String str = new String(buff, 0, length);//1.数组 2.偏移量  3.长度
            System.out.println(str);
        }
         fileReader.close();//关闭  一定关
//       fileReader.read(buff);
//       System.out.println(buff);
}

这是一次读一组.

public static void main(String[] args) throws Exception{
        FileReader fileReader = new FileReader("index.tex");
        BufferedReader reader = new BufferedReader(fileReader);
//      String line = reader.readLine();//一次地一行
//      System.out.println(line);
        String temp = "";
        while ((temp = reader.readLine()) != null) {
            System.out.println(temp);
        }
    }

这是有缓冲区的输入

字符输出流

public static void main(String[] args) throws Exception{
         FileWriter fileWriter = new FileWriter("index1.tex");
         //fileWriter.write("啦啦啦啊啊啦啦啦");  写一个字符串
         //fileWriter.write(cbuf, off, len);  写一个字符数组
         
         fileWriter.close();
         
    }
public static void main(String[] args) throws Exception{
        FileWriter fileWriter = new FileWriter("writer.tex");
        BufferedWriter writer = new BufferedWriter(fileWriter);
        writer.write("235321");
        writer.flush();//刷新缓冲区
        writer.write("啦啦啦啦阿拉");
        writer.close();
        fileWriter.close();
    }

这是有缓冲区的输出

字节流

字节输入流(特点:处理所有类型的媒体文件,本质上是处理二进制流)

public static void main(String[] args) throws Exception{
        FileInputStream fileInputStream = new FileInputStream("in.tex");
        byte[] by = new byte[5];//gbk一个汉字两个字节   utf-8  三个字节
        int num =0;
        while ((num = fileInputStream.read(by)) != -1) {
            System.out.println(new String(by));
        }
        //System.out.println((char)num);
        fileInputStream.close();
        
    }
public static void main(String[] args) throws Exception{
       FileInputStream  fileInputStream = new FileInputStream("in.tex");
       BufferedInputStream bufferedInput = new BufferedInputStream(fileInputStream);
       
      byte[] by = new byte[1024];
       int length = -1;
       while ((length = bufferedInput.read(by)) != -1)
       {
        System.out.println(by);
    }
       bufferedInput.close();
      fileInputStream.close();
    
}

字节输出流

public static void main(String[] args)throws Exception {
    FileOutputStream fileOutputStream = new FileOutputStream("output.tex");
    fileOutputStream.write("我是江景".getBytes());
    fileOutputStream.close();
    
}
 public static void main(String[] args) throws Exception{
        FileOutputStream fileout = new FileOutputStream("out.tex");
    BufferedOutputStream bufferedut = new BufferedOutputStream(fileout);
    bufferedut.write("我是连回家过年".getBytes());
    //bufferedut.flush();
    
    bufferedut.write("在来一行".getBytes());
    bufferedut.close();
    fileout.close();
    
}

*以MP3格式歌曲为例写一个复制

public static void main(String[] args) throws Exception{
    //复制mp3文件\
       FileInputStream filein = new FileInputStream("1.mp3");
       BufferedInputStream bufferedin = new BufferedInputStream(filein);
       
       FileOutputStream fileout = new FileOutputStream("a.mp3");
       BufferedOutputStream bufferedOut = new BufferedOutputStream(fileout);
       byte[] by = new byte[1024];//临时缓冲区
       int length = -1;//每次读的长度
       while ((length = bufferedin.read(by))!= -1) {
           bufferedOut.write(by,0,length);
    }
       bufferedin.close();
       bufferedOut.close();
       filein.close();
       fileout.close();
       }

对象序列化

  • java平台允许我们在内存中创建可复用的java对象,但一般情况下,只有当jvm(虚拟机)处于运行时,这些对象才可能存在,即,这些对象的生命周期不会比jvm的生命周期更长.但在现实应用中,就可以要求在jvm停止运行之后能够保存指定对象,并在将来读取被保存的对象.所以就要使用对象序列化.
  • 特点
    • 使用java对象序列化,在保存对象时,会把其状态保存为一组字节,在未来,在将这些字节组装成对象.必须注意的是:对象序列化保存的是对象的"状态",即它的成员变量.由此可知,对象序列化不会关注类中的静态变量.
      写入:
public class Test1 {
    public static void main(String[] args) throws Exception{
        FileOutputStream fileout = new FileOutputStream("out.txt");
        ObjectOutputStream stream = new ObjectOutputStream(fileout);
        Person per = new Person();
        per.age = 12;
        per.name = "赖宁";
        stream.writeObject(per);
        stream.close();
        fileout.close();
        
          }

}
//必须实现Serializable接口
class Person implements Serializable{
    int age;
    String name;
    
}

写出:

public class Test2 {
    public static void main(String[] args) throws Exception{
        FileInputStream filein = new FileInputStream("out.txt");
        ObjectInputStream objout = new ObjectInputStream(filein);
        Object obj = objout.readObject();
        Person p =  (Person)obj;
        System.out.println("name:"+p.name);
        System.out.println("age:"+p.age);
    }

}

transient关键字

  • 不想被序列化就在属性前加 transient关键字

你可能感兴趣的:(Java_IO流)