JavaEEJavaEE 学习专题

Java_IO流

2017-09-19  本文已影响10人  Miss_差不多

IO流概念

在编程语言的I/O类库中常使用流这个抽象概念.它代表有能力产出数据的数据源对象或者是与能力接受数据的接受端对象."流"屏蔽了实际的I/O设备中处理数据的细节.

IO流的分类

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();
       }

对象序列化

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关键字

上一篇下一篇

猜你喜欢

热点阅读