Closeable、Readable、Flushable和Appendable

Closeable:

package java.io;

import java.io.IOException;

public interface Closeable {
    /**
     * Closes this stream and releases any system resources associated
     * with it. If the stream is already closed then invoking this 
     * method has no effect. 
     */
    public void close() throws IOException;
}

Readable:

package java.lang;

import java.io.IOException;

public interface Readable {

    /**
     * Attempts to read characters into the specified character buffer.
     * The buffer is used as a repository of characters as-is: the only
     * changes made are the results of a put operation. No flipping or
     * rewinding of the buffer is performed.
     */
    public int read(java.nio.CharBuffer cb) throws IOException;
}
Flushable:

package java.io;

import java.io.IOException;

public interface Flushable {

    /**
     * Flushes this stream by writing any buffered output to the underlying stream.
     */
    void flush() throws IOException;
}

Appendable:

package java.lang;

import java.io.IOException;

public interface Appendable {

    /**
     * Appends the specified character sequence to this Appendable.
     * @return  A reference to this Appendable
     */
    Appendable append(CharSequence csq) throws IOException;

    /**
     * Appends a subsequence of the specified character sequence to this Appendable.
     * @return  A reference to this Appendable
     */
    Appendable append(CharSequence csq, int start, int end) throws IOException;

    /**
     * Appends the specified character to this Appendable.
     * @return  A reference to this Appendable
     */
    Appendable append(char c) throws IOException;
}




### `Closeable` 接口与 `emit` 方法的使用分析 Java 中的 `Closeable` 接口是定义在 `java.io` 包中的标准接口,主要用于确保资源能够被正确关闭以避免资源泄漏。该接口包含一个名为 `close` 的方法,其作用是在完成对资源的操作后释放底层资源[^1]。 然而,在标准 Java 库中,`Closeable` 接口本身并不包含名为 `emit` 的方法。如果某个类实现了 `Closeable` 并同时提供了 `emit` 方法,则该方法是自定义扩展的一部分,而非 `Closeable` 接口的标准功能。这种设计通常出现在需要处理流式数据或事件发射的场景中。 #### 自定义实现示例 以下是一个结合 `Closeable` 接口并添加了 `emit` 方法的简单示例,用于模拟数据发射并在完成后关闭资源: ```java import java.io.Closeable; import java.io.IOException; public class DataEmitter implements Closeable { private boolean closed = false; // 模拟发射数据的方法 public void emit(String data) throws IOException { if (closed) { throw new IOException("Emitter已经关闭"); } System.out.println("正在发射数据: " + data); } @Override public void close() throws IOException { closed = true; System.out.println("资源已关闭"); } } ``` 上述代码中,`emit` 方法用于将数据输出到目标位置(如网络连接或文件),而 `close` 方法则确保在操作结束后资源被释放。在实际应用中,可以在此类内部封装更复杂的逻辑,例如通过网络发送数据包或写入文件缓冲区。 #### 使用方式 可以通过如下方式使用 `DataEmitter` 类: ```java public class EmitterTest { public static void main(String[] args) { try (DataEmitter emitter = new DataEmitter()) { emitter.emit("Hello"); emitter.emit("World"); } catch (IOException e) { e.printStackTrace(); } } } ``` 在上述代码中,`try-with-resources` 语法确保在代码块执行完毕后自动调用 `close` 方法,从而释放相关资源。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值