当前位置: 移动技术网 > IT编程>开发语言>Java > Java中IO流 字节流实例详解

Java中IO流 字节流实例详解

2019年07月22日  | 移动技术网IT编程  | 我要评论

java中io流 字节流实例详解

io流(输入流、输出流),又分为字节流、字符流。

  流是磁盘或其它外围设备中存储的数据的源点或终点。

  输入流:程序从输入流读取数据源。数据源包括外界(键盘、文件、网络…),即是将数据源读入到程序的通信通道。

  输出流:程序向输出流写入数据。将程序中的数据输出到外界(显示器、打印机、文件、网络…)的通信通道。

字节流

  1.inputstream、outputstream

  inputstream抽象了应用程序读取数据的方式

  outputstream抽象了应用程序写出数据的方式

  2.读到文件结尾,称为eof = end,读到-1就读到结尾

  3.输入流基本方法

  int b = in.read();读取一个字节,无符号填充到int的低八位.-1是eof

  int.read(byte[] buf)读取数据填充到字节数组buf

  int.read(byte[] buf, int start, int size)读取数据填充到字节数组buf,从buf的start位置开始存储size长度的数据

  4.输出流基本方法

  out.write(int b);写出一个byte到流,b的低八位

  out.write(byte[] buf);将buf字节数组都写入到流

  out.write(byte[] buf, int start, int size);字节数组buf从start位置开始写size长度的字节到流

  5.fileinputstream是inputstream的子类,具体实现了在文件上读取数据

    6.fileoutputstream是outputstream的子类,实现了向文件中写出字节数据的方法

  fileinputstream的demo:

package com.test.io;

import java.io.bufferedinputstream;
import java.io.bufferedoutputstream;
import java.io.file;
import java.io.fileinputstream;
import java.io.fileoutputstream;
import java.io.ioexception;

public class ioutils {
  /**
   * 读取指定文件内容,按照十六进制输出到控制台
   * 并且每输出10个byte换行
   * @param filename
   * @throws ioexception
   */
  public static void printhex(string filename) throws ioexception {
    //把文件作为字节流进行读操作
    fileinputstream in = new fileinputstream(filename);
    int b;
    int i = 1;
    while ((b = in.read()) != -1) {
      if (b <= 0xf) {
        system.out.print("0");
      }
      system.out.print(integer.tohexstring(b) + " ");
      if (i % 10 == 0) {
        system.out.println("");
      }
      i++;
    }
    in.close();
  }

  public static void printhexbybytearray(string filename) throws ioexception {
    fileinputstream in = new fileinputstream(filename);
    byte[] buf = new byte[20*1024];
    
    //如果字节数组够大,可以一次性读完
    //从in中批量读取字节,放入到buf这个字节数组中,从第0个位置开始放,最多放buf.length个,返回的是读到的字节的个数
    /* int bytes = in.read(buf, 0, buf.length);
    int j = 1;
    for(int i = 0;i < bytes; i++) {
      if (buf[i] <= 0xf) {
        system.out.print("0");
      }
      system.out.print(integer.tohexstring(buf[i] & 0xff) + " ");
      if (j % 10 == 0) {
        system.out.println("");
      }
      j++;
    } */
    
    //如果字节数组不够大,不能一次性读完
    int bytes = 0;
    int j = 1;
    while ((bytes = in.read(buf, 0, buf.length)) != -1) {
      for (int i = 0; i <bytes; i++) {
        if (buf[i] <= 0xf) {
          system.out.print("0");
        }
        system.out.print(integer.tohexstring(buf[i] & 0xff) + " ");
        if (j % 10 == 0) {
          system.out.println("");
        }
        j++;
      }
    }
  }

}

  fileoutputstream的demo:

package com.test.io;

import java.io.fileoutputstream;
import java.io.ioexception;

public class fileoutputdemo {

  public static void main(string[] args) throws ioexception {
    //如果该文件不存在,则直接创建,如果存在,删除后创建。(如果第二个参数为 true,则将字节写入文件末尾处,而不是写入文件开始处。)
    fileoutputstream out = new fileoutputstream("f:\\javaio\\out.dat");
    out.write('a');//写入了‘a'的低八位(一次只写入一个字节)
    int a = 10;
    out.write(a >>> 24);
    out.write(a >>> 16);
    out.write(a >>> 8);
    out.write(a);
    
    byte[] b = "10".getbytes();
    out.write(b);
    
    out.close();
    
    ioutils.printhex("f:\\javaio\\out.dat");    
  }
}

  7.dataoutputstream和datainputstream,对流功能的扩展,可以更加方便的读取int,long,字符等类型数据。

package com.test.io;

import java.io.dataoutputstream;
import java.io.fileoutputstream;
import java.io.ioexception;

public class dataoutputdemo {

  public static void main(string[] args) throws ioexception {
    string file = "f:\\javaio\\b.txt";
    dataoutputstream dos = new dataoutputstream(new fileoutputstream(file));
    dos.writeint(10);
    dos.writeint(-10);
    dos.writelong(10l);
    dos.writedouble(10.5);
    dos.writeutf("你好");
    dos.writechars("中国");
    dos.close();
    ioutils.printhex(file);
  }
}

  运行结果:

00 00 00 0a ff ff ff f6 00 00 
00 00 00 00 00 0a 40 25 00 00 
00 00 00 00 00 06 e4 bd a0 e5 
a5 bd 4e 2d 56 fd 

  其中,00 06两个字节是“你好”这两个中文的字节个数。

package com.test.io;

import java.io.datainputstream;
import java.io.fileinputstream;
import java.io.ioexception;

public class datainputdemo {

  public static void main(string[] args) throws ioexception {
    string file = "f:\\javaio\\b.txt";
    datainputstream dis = new datainputstream(new fileinputstream(file));
    int i = dis.readint();
    system.out.println(i);
    i = dis.readint();
    system.out.println(i);
    long l = dis.readlong();
    system.out.println(l);
    double d = dis.readdouble();
    system.out.println(d);
    string s = dis.readutf();
    system.out.println(s);
    dis.close();
  }

}

  运行结果:

10
-10
10
10.5
你好

   8.bufferedinputstream&bufferedoutputstream,这两个流类为io提供了带缓冲区的操作,一般打开文件进行写入或读取操作时,都会加上缓冲,这种流模式提高了io的性能。

  文件的拷贝:

package com.test.io;

import java.io.bufferedinputstream;
import java.io.bufferedoutputstream;
import java.io.file;
import java.io.fileinputstream;
import java.io.fileoutputstream;
import java.io.ioexception;

public class ioutils {/**
   * 拷贝文件,字节批量读取
   * @param srcfile 源文件
   * @param destfile 目标文件
   * @throws ioexception
   */
  public static void copyfile(file srcfile, file destfile) throws ioexception {
    if (!srcfile.exists()) {
      throw new illegalargumentexception("文件" + srcfile + "不存在");
    }
    if (!srcfile.isfile()) {
      throw new illegalargumentexception(srcfile + "不是文件");
    }
    fileinputstream in = new fileinputstream(srcfile);
    fileoutputstream out = new fileoutputstream(destfile);
    byte[] buf = new byte[10*1024];
    int b;
    while ((b = in.read(buf, 0, buf.length)) != -1) {
      out.write(buf,0,b);
      out.flush();//最好加上,刷新此输出流并强制写出所有缓冲的输出字节。
    }
    in.close();
    out.close();
  }
  /**
   * 拷贝文件,利用带缓冲的字节流
   * @param srcfile
   * @param destfile
   * @throws ioexception
   */
  public static void copyfilebybuffer(file srcfile, file destfile) throws ioexception {
    if (!srcfile.exists()) {
      throw new illegalargumentexception("文件" + srcfile + "不存在");
    }
    if (!srcfile.isfile()) {
      throw new illegalargumentexception(srcfile + "不是文件");
    }
    fileinputstream in = new fileinputstream(srcfile);
    fileoutputstream out = new fileoutputstream(destfile);
    
    bufferedinputstream bis = new bufferedinputstream(in);
    bufferedoutputstream bos = new bufferedoutputstream(out);
    
    int c;
    while ((c = bis.read()) != -1) {
      bos.write(c);
      bos.flush();
    }
    
    bis.close();
    bos.close();
  }
  /**
   * 拷贝文件,通过单字节读取
   * @param srcfile
   * @param destfile
   * @throws ioexception
   */
  public static void copyfilebybyte(file srcfile, file destfile) throws ioexception {
    if (!srcfile.exists()) {
      throw new illegalargumentexception("文件" + srcfile + "不存在");
    }
    if (!srcfile.isfile()) {
      throw new illegalargumentexception(srcfile + "不是文件");
    }
    fileinputstream in = new fileinputstream(srcfile);
    fileoutputstream out = new fileoutputstream(destfile);
    
    int c;
    while ((c = in.read()) != -1) {
      out.write(c);
      out.flush();
    }
    
    in.close();
    out.close();
  }
}

  测试文件拷贝:

package com.test.io;

import java.io.file;
import java.io.ioexception;

public class ioutilstest {

  public static void main(string[] args) {
    //ioutils.printhex("d:\\javaprogram\\hello.java");
    try {
      long start = system.currenttimemillis();
      //ioutils.copyfile(new file("f:\\javaio\\1.mp3"), new file("f:\\javaio\\2.mp3"));//211ms
      //ioutils.copyfilebybuffer(new file("f:\\javaio\\1.mp3"), new file("f:\\javaio\\3.mp3"));//18583ms
      ioutils.copyfilebybyte(new file("f:\\javaio\\1.mp3"), new file("f:\\javaio\\4.mp3"));//37822ms
      long end = system.currenttimemillis();
      system.out.println(end - start);
    } catch (ioexception e) {
      e.printstacktrace();
    }
  }
}

  根据以上测试看出,文件拷贝,最快的方式是通过字节的批量读取。

        感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网