栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java - I/O 流

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java - I/O 流

概述
  • 按照流向可以分为:输入流、输出流。
  • 按照数据处理单位可以分为:字节流、字符流。字节占1个byte即8位,字符占2个byte即16位。对于文本文件(txt、xml、java)应该使用字符流,字符涉及编码格式,对于二进制文件(jpg、doc、mp3)应该使用字节流,字节不涉及编码格式。

InputStream

字节输入流

OutputStream

字节输入流

Reader

字符输入流

Writer

字符输出流

缓存流BufferedInputStreamBufferedOutputStreamBufferedReaderBufferedWriter
文件流FileInputStreamFileOutputStreamFileReaderFileWriter
数组流ByteInputStreamByteOutputStreamCharReaderCharWriter
字符串流--StringReaderStringWriter
管道流PipedInputStreamPipedOutputStreamPipedReaderPipedWriter
对象流ObjectInputStreamObjectOutputStream--
打印流-PrintOutputStream-PrintWriter
转换流--InputStreamReaderOutputStreamWriter
推回输入流PushbackInputStream-PushbackReader-
FilterInputStreamFilterOutputStreamFilterReaderFilterWriter
DataInputStreamDataOutputStream
Java 7 特性

实现了 AutoClose 接口的对象,能自动关闭流资源。格式:try(流对象){读写代码}。

字符流抽象基类 Writer、Reader

Writer:字符写入流的抽象类。

close()

​abstract public void close() throws IOException

刷新后关闭流并释放资源,流不能再被使用。

flush()

abstract public void flush() throws IOException

刷新该流的缓冲,流可以继续使用。

write()

public void write(int c) throws IOException

写入单个字符(0~65535)。

public void write(char cbuf[]) throws IOException
abstract public void write(char cbuf[], int off, int len) throws IOException

写入字符数组。

public void write(String str) throws IOException
public void write(String str, int off, int len) throws IOException

写入字符串。

append()

public Writer append(char c) throws IOException

续写字符。

public Writer append(CharSequence csq) throws IOException
public Writer append(CharSequence csq, int start, int end) throws IOException

续写字符序列。

Reader:字符读取流的抽象类。

close()

abstract public void close() throws IOException

关闭流并释放资源,流不能再使用。

read()

public int read() throws IOException

读取单个字符,返回的是字符在内存中对应的数字(0~65535),该方法再次调用会自动读取下一位,没有了则返回 -1。

public int read(char cbuf[]) throws IOException

abstract public int read(char cbuf[], int off, int len) throws IOException

将读取的字符存入数组,返回的是读到的个数,没有了则返回 -1。

字节流抽象基类 OutputStream、InputStream

OutputStream:字节输出流的抽象类。

close()

public void close() throws IOException

刷新后关闭流并释放资源,流不能再被使用。

flush()

public void flush() throws IOException

刷新该流的缓冲,流可以继续使用。

write()

public abstract void write(int b) throws IOException
写入单个字节,写入有效位int的最低8位。

public void write(byte b[]) throws IOException
public void write(byte b[], int off, int len) throws IOException

写入字节数组。

InputStream:字节输入流的抽象类。

close()

public void close() throws IOException

关闭流并释放资源,流不能再使用。

available()

public int available() throws IOException

返回的是读到的字节个数,r 和 n 各是一位。

read()

public abstract int read() throws IOException

读取单个字节,读取有效位int的最低8位,该方法再次调用会自动读取下一位,没有了则返回 -1。

public int read(byte b[]) throws IOException
public int read(byte b[], int off, int len) throws IOException

将读取的字节存入数组,返回的是读到的个数,没有了则返回 -1。

文件流

文件流可以直接操作文件的读写,

FileWriter:字符文件写入流。用来写字符文件的便捷类。如果有同名文件会覆盖(因此续写模式从构造入手而不是write()方法入手)。

FileWriter()

public FileWriter(File file) throws IOException

根据给定的文件构造,从开头写。
public FileWriter(File file, boolean append) throws IOException

append续写模式,true从结尾处写,false从开头写。

public FileWriter(String fileName) throws IOException

根据给定的文件名构造(文件名前不带路径则为当前目录),从开头写。
public FileWriter(String fileName, boolean append) throws IOException

append续写模式,true从结尾处写,false从开头写。

FileReader:字符文件读取流。用来读字符文件的便捷类。

FileReader()

public FileReader(File file) throws FileNotFoundException

根据给定的文件构造。

public FileReader(String fileName) throws FileNotFoundException

根据给定的文件名构造(文件名前不带路径则为当前目录)。

try (FileReader fr = new FileReader("demo.txt")) {
    int num = 0;
//  每次读取单个字符效率低下不推荐
//  while ((num = fr.read()) != -1) {
//      System.out.print((char)num);
//  }
    char[] buffer = new char[1024]; //定义一个数组用于存储读到的字符(建议为1024的整数倍)
    while ((num = fr.read(buffer)) != -1) { //直到返回-1读不到数据了便结束
        System.out.print(new String(buffer, 0, num));   //最后一次读取,不一定装满1024所以是num不是buffer.length,否则超过num后会把上一次存入的打印出来
    }
} catch (IOException e) {
    e.printStackTrace();
}

FileOutputStream:字节文件输出流。

FileInputStream:字节文件输入流。

缓存流

BufferedWrite:字符写入的缓存流。将文本写入字符输出流,缓存各个字符,从而提供高效写入。

BufferedWrite()

public BufferedWriter(Writer out)

缓冲区的出现是为了提高流的效率,所以创建缓冲区需要提供一个流对象。

newLine()

public void newLine() throws IOException

写入一个换行符(回车换行效果)。

try (BufferedWriter bw = new BufferedWriter(new FileWriter("demo.txt"))) {
    for (int i = 1; i < 5; i++) {
        bw.write("Hello" + i);
        bw.newLine();   //换行
        bw.flush(); //每次刷新避免数据丢失,用到缓存流就要记得刷新
    }
} catch (IOException e) {
    e.printStackTrace();
}

BufferedReader:字符读取的缓存流。从字符输入流中读取文本,缓存各个字符,从而提供高效读取。

BufferedReader()

public BufferedReader(Reader in)

缓冲区的出现是为了提高流的效率,所以创建缓冲区需要提供一个流对象。

readLine()

public String readLine() throws IOException

读取一行文本(不包含换行符)。

try (BufferedReader br = new BufferedReader(new FileReader("demo.txt"))) {
    String line = null;
    while ((line = br.readLine()) != null) {    //一行一行读更方便
        System.out.println(line);
    }
} catch (IOException e) {
    e.printStackTrace();
}

通过字符缓存流复制文本文件

try (BufferedWriter bw = new BufferedWriter(new FileWriter("D:\original\demo.txt"));
     BufferedReader br = new BufferedReader(new FileReader("E:\destination\demo_copy.txt"))
) {
    String line = null;
    while ((line = br.readLine()) != null) {
        bw.write(line);
        bw.newLine();
        bw.flush();
    }
} catch (IOException e) {
    e.printStackTrace();
}

LineNumberReader:带行号的字符读取缓存流。是BufferedReader的子类,可以给每一行加上行号。

try(LineNumberReader lnr = new LineNumberReader(new FileReader("demo.txt"))){
    String line = null;
    lnr.setLineNumber(100); //设置行号从100开始
    while((line = lnr.readLine())!=null){
        System.out.println(lnr.getLineNumber()+":"+line);   //带行号打印,第一行为101
    }
}catch(IOException e){
    e.printStackTrace();
}

BufferedOutputStream:字节输出缓存流。

BufferedInputStream:字节输入缓存流。

PrintStream(PrintWeiter)

打印流,可以将各种数据原样打印。

SequenceInputStream

序列流,将多个流合并为一个流。

ObjectInputStream、ObjectOutputStream

对象流,对象的持久化存储(将对象存储到文件中)。

PipedInputStream、PipedOutputStream

管道流,输入流和输出流直接进行连接,结合线程使用。

RandomAccessFile

随机访问文件,该类直接继承自 Object,但通过内部封装字节输入流和输出流具备了读写功能,所以是 I/O 包中的成员。

ByteArrayInputStream、ByteArrayOutputStream

字节数组流,因为两个流对象操作的是数组,没有使用系统资源所以不会出现 IOException。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/985062.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号