JavaIO操作之 字节 输入输出流

来源:互联网 发布:阿里云备案订单号查询 编辑:程序博客网 时间:2024/05/21 11:44

在Java中IO流主要分为两大部分:字节流和字符流,这里要说的就是第一种字节流

字节流也分为两大部分:输入流输出流

首先是输入流:

输入流包含以下3种简单的输入数据的方法:

  • read(); //从输入流中读取数据的下一个字节,返回一个int型字节,如果因为已经到达流末尾而没有可用的字节,则返回值 -1。
  • read(byte[] b); //从输入流中读取一定数量的字节,并将其存入缓冲数组b中,并以整数的形式返回实际读取的字节数,如果因为流位于文件末尾而没有可用的字节,则返回值 -1。
  • read(byte[] b, int off, int len); //将输入流中最多 len 个数据字节读入 byte 数组,将读取的第一个字节存储在元素 b[off] 中,下一个存储在 b[off+1] 中,依次类推。在任何情况下,b[0] 到 b[off] 的元素以及 b[off+len] 到 b[b.length-1] 的元素都不会受到影响。返回读入缓冲区的的总字节数,同样,如果因为流位于文件末尾而没有可用的字节,则返回值 -1。

下面是一个使用字节输入流显示文件的例子:

import jdk.internal.org.objectweb.asm.tree.analysis.SourceInterpreter;import jdk.internal.util.xml.impl.Input;import java.io.*;/** * Created by zhuxinquan on 16-1-15. */public class InputStreamDemo {    //一次性读取所有的字节    public static void read1() throws IOException {        File f = new File("test.txt");        InputStream in = new FileInputStream(f);        //根据文件大小构造字符数组        byte[] bytes = new byte[(int)f.length()];       //通过使用f.length获取到该文件总共有多少字节        int len = in.read(bytes);        System.out.println(new String(bytes));        in.close();    }    //每次读取一个字节    public static void read2() throws IOException {        InputStream in = new FileInputStream("./test.txt");        int b = -1;        //in.read的返回值为int类型        while((b = in.read()) != -1){            System.out.print((char)b);        }        in.close();    }    //每次获取指定长度个字节    public static void read3() throws IOException {        InputStream in = new FileInputStream("./test.txt");        byte[] bytes = new byte[10];        int len = -1;   //存储每次读取的实际长度        StringBuilder sb = new StringBuilder();        while((len = in.read(bytes)) != -1){            System.out.println(new String(bytes, 0, len) + " "  + "len = " + len);            sb.append(new String(bytes, 0, len));        }        System.out.println(sb);        in.close();    }    public static void main(String[] args) throws IOException {        read3();    }}

接下来是输出流

输出流包含也包含以下3种简单的将数据输出到文件的方法:

  • write(byte[] b); //将b.length个字节从指定byte数组写入文件输出流中
  • write(byte[] b, int off, int len); //将指定 byte 数组中从偏移量 off 开始的 len 个字节写入此文件输出流。
  • write(int b); //将指定字节写入此文件输出流。该方法参数中的int b为字符的ascii码值,输出时将对应的字符写入文件输出流中。
    下面是一个使用输出流方法将数据写入文件的例子:
import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.OutputStream;/** * Created by zhuxinquan on 16-1-15. */public class OutputStresmDemo {    public static void write1(){        try{            OutputStream out = new FileOutputStream("./test.txt");            String info = "hello IO";            byte[] bytes = info.getBytes();            for(int i = 0; i < info.length(); i++){                out.write(bytes[i]);        //一个一个将字节写入字节输出流并写入文件            }            out.write(bytes);       //将一个字节数组一次性的写入输出流并写入文件当中            out.write(97);          //将一个字节数(ascii值)转换为字符输出到文件输出流并写入文件中            out.close();        }catch (FileNotFoundException e){            e.printStackTrace();        }catch (IOException e){            e.printStackTrace();        }    }    public static void main(String[] args) {        write1();    }}

这里是一些字节输入输出流的方法,每次文件流的读写操作都是基于字节进行的,接下来就是字符输入输出流了……

!!出错之处请指正~~~

0 0
原创粉丝点击