• IO操作


    作用:读写设备上数据、硬盘文件、内存、键盘、网络等。

    分类:

      数据走向:输入流、输出流

      数据类型:字符流(文本数据Reader或者Writer结尾)  

           字节流(所有类型Stream结尾)

      1个字节 = 8位二进制  字节是存储信息单位,计量单位

      字符:抽象的一个符号

      字符集:编码,ANSI编码标准

      字节流->字符

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

      byte inpyt[] = new byte[21];

      fis.read(input);

      String inputString = new String(input,"UTF-8");

      fis.close();

      字符->字节流

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

      String outString = "";

      byte output[] = outString.getBytes("UTF-8");

      fos.write(output);

      fos.close();

      文件拷贝:

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

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

      byte input[] = new byte[50];

      while(fis.read(input)!=-1){fos.write(input);}

      fis.close();

      fos.close();

      缓冲区加速度:

      FileInputStream fis = new FileInputStream("");

      BufferedInputStream bis = new BufferedInputStream(fis);

      FileOutputStream fos = new FileOutputStream("");

      BufferedOutputStream bos = new BufferedOutputStream(fos);

      byte input[] = new byte[100];

      while(bis.read(input)!=-1){bos.write(input);}

      bis.close();

      fis.close();

      bos.close();

      fos.close();

      系统时间返回的是1970年到现在的毫秒数。

      优化速度:1、缓冲区大小。2、字节数组的大小。

      

      使用字符流:

      FileInputStream fis = new FileInputStream("");

      InputStreamReader isr = new InputStreamReader(fis,"UTF-8");

      FileOutputStream fos = new FileOutputStream("");

      OutputStreamWriter osw=new OutputStreamWriter(fos);

      char input[] = new char[100];

      while(isr.read(input)!=-1){int l=0;while((l=isr.read(input))!=-1){osw.write(input,0,l)}}

      osw.close();

      fos.close();

      isr.close();

      fis.close();

      字符流缓冲:

      FileInputStream fis = new FileInputStream("");

      InputStreamReader isr = new InputStreamReader(fis,"UTF-8");

      BufferedReader br = new BufferedReader(isr);

      FileOutputStream fos = new FileOutputStream("");

      OutputStreamWriter osw=new OutputStreamWriter(fos);  //PrintWriter换行输出

      BufferedWriter bw = new BufferedWriter(osw);

      char input[] = new char[100];

      while(input = br.readLine!=null){br.write(input);}

      br.close();

      bw.flush();

      bw.close();

      osw.close();

      fos.close();

      isr.close();

      fis.close();

      文件的读写(纯文本):

      FileReader fr = new FileReader("");

      BufferedReader br = new BufferedReader(fr);

      RandomAccessFile:

      RandomAccessFile raf = new RandomAccessFile(file,"rw");

      raf.seek((block-1)*l);->指针

      raf.write();

     

      FileUtiles对file的操作特别方便:

      File fie = new File("");

      String input  = FileUtiles.readFileToString(file,"");

      FileUtiles.copyFile(file1,file2); 

      

  • 相关阅读:
    haproxy path_end不能忽略
    haproxy hdr和path
    logstash 分析nginx 错误日志
    logstash 处理nginx 访问日志
    rsyslog 同时发生nginx 访问日志和错误日志
    rsyslog 同时发生nginx 访问日志和错误日志
    【codevs1048】石子归并
    【codevs1576】最长严格上升子序列
    【BAT】中文数字to阿拉伯数字转换
    Windows批处理BAT脚本
  • 原文地址:https://www.cnblogs.com/hzwcoming/p/4655376.html
Copyright © 2020-2023  润新知