gpt4 book ai didi

java - Java 中的自动文件/输入流处理

转载 作者:行者123 更新时间:2023-12-02 07:48:01 26 4
gpt4 key购买 nike

序言:我了解 Apache IO,并且希望获得 Apache IO 之外的评论。

<小时/>

这可能是一个有点愚蠢的问题,但我刚刚意识到:

当我实现一个必须从输入流读取的 API 时,我最终实现了许多重载变体,例如:

public void foo ( InputStream input, int somearg, float someotherarg) { ...

重载变体:

public void foo ( File inputFile, int somearg, float someotherarg) { ...
public void foo ( String filename, int somearg, float someotherarg) { ...

--或--

我可以实现

public void foo ( InputStream input, int somearg, float someotherarg) { ...

并实现处理文件名、文件并将它们转换为输入流的辅助实用程序函数。

我的问题是我不断重新实现这个 - 是否有人编写了一个库,或者是否已在设计模式中对其进行了处理。

谢谢。

最佳答案

Guava有一个叫做InputSupplier的东西。也许你应该看一下。

除此之外,还有一个AutoCloseable界面和一个名为 try-with-resources 的功能在 Java 7 中,这可能会简化您的情况(即您只能创建需要 InputStream 的 API 方法,并且程序员可以使用 try-with-resources 来处理那些具有字符串/文件参数的输入流)。

关于java - Java 中的自动文件/输入流处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10573759/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com