## JAVA笔记14 I/O输入输出流

复习
HashMap数据结构是哈希表(JDK1.7 数组+链表)
                     (JDK1.8数组+链表+红黑树)
I/O流
I/O :intputstream/outputstream
它是处理设备之间的一个数据传输
我们是站在内存角度来看流的流向
输入流:硬盘---内存
输出流:内存---硬盘
安装流的读取文件的类型分字节流和字符流
字节流可读写任意类型文件
字符流只能读写文本文件
字节流的继承体系:
FileInputstream    ByteArrayInputstream  objectInputstream
字节输出流:
FileOutputstream   ByteArrayOutputstream        objectOutputstream
字节流
文件输出流:
构造一: File file = new File"a.txt");
        FileOutPutstream  out = new Fileoutstream(file);
构造二: FileOutPutstream  out = new Fileoutstream("a.txt");
输出流
 FileOutPutstream  out = new Fileoutstream("a.txt");
  out.write(97);
  out.write(98);
  out.write(99);
  out.write(100);
  out.write(300); //超过字节范围,就会丢弃多余字节
  
  byte[] bytes = {100,101,102,103};
  out.write(bytes); //一次写入一个字节数组
  out.write(bytes,2,2);//写入字节数组一部分
  
  String  str = "好好学习,天天向上";
  out.write(str.getbytes);   //字符串---字节数组

   FileOutPutstream  out = new Fileoutstream("a.txt");
   out.write("你".getbytes());
   out.write("\r\n".getbytes()); //写入一个换行符

   //windows:\r\n
   //LInux:\nI/
   //mac:\r
   //流使用完之后,必须释放资源
   out.close();
   //close 的用处:童子系统释放关于管理a.txt文件的资源
                  //让I/O流对象变成垃圾,等待垃圾回收器对其回收、

  FileOutPutstream  out = new Fileoutstream("a.txt"true);
  //true代表追加写入,false不追加
 
输入流
文件输入流:
构造一: File file = new File"a.txt");
        FileinPutstream  in = new Fileinputstream(file);
        //输入流所关联的文件,如果不存在,则报错
构造二: FileinPutstream  in = new Fileinputstream("a.txt");

     int len = in.read(); //一次读取一个字节
     System.out.println(len); //如果读取不到,则返回-1
       
      //创建一个字节数组,充当缓冲区
     byte[] bytes = new byte[1024];
     int len = in.read(bytes); //一次读1024,放缓冲区
     System.out.println(len); //len实际读取长度
                                               
     String s = new string();
     System.out.println(len); //字节数组-----字符串   
                                               
对文件进行复制
 FileOutPutstream  out = new Fileoutputstream("aa.txt");
 FileInputPutstream  in = new FileInputstream("a.txt");
//循环读写
//定义变量
int len = 0while((len=in.read())!=-1){
      out.write(len);
      out.flush(); //刷新
}
  in.close();
  out.close();
//由于是一个一个字节循环,影响效率,不建议使用


FileOutPutstream  out = new Fileoutputstream("aa.txt");
 FileInputPutstream  in = new FileInputstream("a.txt");
//循环读写
//定义变量
byte[] bytes= new byte[1024*8]; //一次读写一个字节数组
int len = 0while((len=in.read(bytes))!=-1){
      out.write(bytes);
      out.flush(); //刷新
}
  in.close();
  out.close();

异常处理
public class Demo2 {
    public static void main(String[] args)  {
        FileInputStream in = null;
        FileOutputStream f = null;
        try {
            in = new FileInputStream("a.txt");
            f = new FileOutputStream("a.txt");
            byte[] bytes = new byte[1024];
            int len = 0;
            while ((len = in.read(bytes)) != -1) {
                f.write(bytes, 0, len);
                f.flush();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (f != null) {
                    f.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

高效的字节输入输出流
BufferedInputstream  bfr = new BufferedInputstream(new FileInputstream("a.txt"))
BufferedOutputstream  bfw = new BufferedOutputstream(new FileOutputstream("aa.txt"))    
字符流
Reader输入流: InputstreamReader
        BufferedReader
writer输出流: outPutstreamWriter
         BufferedWriter
         
    //编码:把字符串转化成字节数组
    //解码:把字节数组转化成字符串
    
    String str = "天天向上"byte[] bytes = str.getbytes("gbk");

    String str1 = new String(bytes,"gbk");
    System.out.println(str1);
  // 编码与解码的字典需一致,否则会乱码

字符流
 OutputStreamWriter
 OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream("aa.txt"));
 //输出流,所关联的文件,如果不存在,会自动创建
 out.writer();
 InputStreamReader 
 out.read()  //读取不到返回-1
 InputStreamReader in = new InputStreamReader(new FileInputStream("a.txt"));
 char[] chars = new char[1024];
 int len = in.read(chars);
 System.out.println(len);

 字符数组--------字符串
 String trim = new Sring(chars);
 System.out.println(trim);

 String s = String.valueof(chars);
  System.out.println(s);
采用字符流复制文本文件
public class Demo3 {
    public static void main(String[] args) {
        InputStreamReader in = null;
        OutputStreamWriter out = null;
            try { 
            in = new InputStreamReader(new FileInputStream("a.txt"));
            out = new OutputStreamWriter(new FileOutputStream("aa.txt"));
            char[] ch = new char[2000];
            int len = 0;
            while ((len = in.read(ch)) != -1) {
                out.write(ch);
                out.flush();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (out != null) {
                    out.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值