字节流与字符流

来源:互联网 发布:jquery ajax json data 编辑:程序博客网 时间:2024/05/08 21:38

字节流与字符流

使用File类只能实现文件本身的操作,但是与文件内容的操作无关,如果要想进行文件内容操作由以下两组流完成:

  • 字节流:InputStream、OutputStream
  • 字符流:Reader、Writer
    但是不管使用何种流,基本的操作流程是一样的,以文件操作为例:

    • 确定操作文件的路径;
    • 确定字节流或字符流的子类为字节流和字符流类对象实例化;
    • 进行输入、输出的操作;
    • 关闭流 流属于资源操作,资源操作完成一定要关闭。

字节输出流:OutputStream

java.io.OutputStream是可以进行字节数据(byte)的输出,这个类的定义解构如下:

public abstract class OutputStream extends Object implements Closeable, Flushable

首先可以发现在OutputStream类之中实现了两个接口:

  • Closeable 关闭资源

    public void close() throws IOException

  • Flushable

    public void flush() throws IOException

从实际的开发来讲,对于Closeable和Flushable两个接口是属于后来再次抽象的产物,本身存在的意义不大。因为在OutputStream类里面已经存在close()和flush()两个方法。
在OutputStream里面有三个write()方法。

输出单个字节:public abstract write(int b) throws IOException;
输出全部字节:public void write(byte[] b) throws IOException
输出部分字节:public void write(byte[] b, int off, int len) throws IOException

但是OutputStream只是一个抽象类,无法实例化,所以可以使用FileOutputStream子类。这个类定义了两个常用构造方法:

  • 构造方法:

    public FileOutputStream(File file) throws FileNotFoundException, 覆盖


public FileOutputStream(File file, boolean append) throws FileNotFoundException, 追加。范例:
package test;import java.io.File;import java.io.FileOutputStream;import java.io.OutputStream;public class Test {    public static void main(String[] args) throws Exception {        File file = new File("E:" + File.separator + "test.txt");        if(!file.getParentFile().exists()) {            file.getParentFile().mkdirs();        }        OutputStream out = new FileOutputStream(file);        String msg = "Hello World";        out.write(msg.getBytes());        out.close();    }}
循环单个字节
public class Test {    public static void main(String[] args) throws Exception {        File file = new File("E:" + File.separator + "test.txt");        if(!file.getParentFile().exists()) {            file.getParentFile().mkdirs();        }        OutputStream out = new FileOutputStream(file);        String msg = "Hello World";        for(int i = 0; i < msg.length(); i++) {            out.write(msg.charAt(i));        }    }}

字节输入流:InputStream

使用OutputStream可以完成程序向文件的输出,而现在要通过程序读取内容,则必须要使用InputStream。那么此类的定义如下:

public abstract class InputStream extends Object implements Closeable

读取单个字节

public abstract int read() throws IOException;
每次读取玩一个字节返回一个数据,读完则返回-1;

读取内容到字节数组

public int read(byte[] b) throws IOException;
返回读取的个数,如果读取完毕,则返回的是-1;

读取内容到部分字节数组

public int read(byte b, int off, int len) throws IOException.
将制定长度的内容读取到字节数组,返回读取的个数,读完则返回-1

但是InputStream类属于抽象类,抽象类如果要实例化则使用它的子类。那么可以使用FileInputStream类。

public class FileInputStream(File file) throws FileNotException

范例:
public class Test {    public static void main(String[] args) throws Exception {        File file = new File("E:" + File.separator + "test.txt");        if(file.exists()) {            InputStream in = new FileInputStream(file);            byte[] b = new byte[1034];            int len = in.read(b);            System.out.println(new String(b, 0, len));            in.close();        }    }}
读取单个字节
public class Test {    public static void main(String[] args) throws Exception {        File file = new File("E:" + File.separator + "test.txt");        if(file.exists()) {            InputStream in = new FileInputStream(file);            byte[] data = new byte[1024];            int temp = 0;            int foot = 0;            while((temp = in.read()) != -1) {                data[foot++] = (byte)temp;            }            System.out.println(new String(data, 0, foot));            in.close();        }    }}

字符输出流Writer

public abstract class Wrtier extends Object implements Appendable, Closeable, Flushable.

在Writer类里面提供有一个最为重要的操作方法:
  • 输出字符串:

public void write(String str) throws IOException.


  • 输出字符数组

public void write(char[] cbuf) throws IOException
但是Writer是抽象类,必须使用子类FileWrtier

范例:

public class Test {    public static void main(String[] args) throws Exception {        File file = new File("E:" + File.separator + "test.txt");        Writer out = new FileWriter(file);        String msg = "Hello, world!!!!";        out.write(msg);        out.close();    }}

字符输入流Reader

public abstract class Reader extends Object implements Readable, Closeable

范例:

public class Test {    public static void main(String[] args) throws Exception {        File file = new File("E:" + File.separator + "test.txt");        if(file.exists()) {            Reader in = new FileReader(file);            char[] data = new char[1024];            int temp = 0;            int foot = 0;            while((temp = in.read()) != -1) {                data[foot++] = (char)temp;            }            System.out.print(new String(data, 0, foot));            in.close();        }    }}

当处理中文时,使用字符流操作,但处理图像、音响等二进制文件时,使用字节流操作。

0 0
原创粉丝点击