是否有类似于的 java 库/包?
我断断续续地研究 Java 已经有大约 14 年了,在过去 6 年左右的时间里几乎没有做其他事情。
我真的很讨厌 java.io 包——它的大量子类和适配器。我确实喜欢异常,而不是总是轮询“errno”之类的东西,但我肯定可以在没有声明异常的情况下生活。
是否有任何功能类似于 C 语言中的 Unix/ANSI stdio.h 例程?
我知道,在 java 本身退役之前,我们永远不会摆脱 java.io 及其约定,因为它们已经转移到了 java 所积累的许多框架中。也就是说,我想要像这样工作的东西(让我们称之为包 javax.stdio):
有一个主要的实用程序类,也许是 FileStar,它可以顺序读取和写入文件(或管道),无论是文本还是二进制或随机访问,具有模仿 fopen() 和 popen() 的构造函数。
这个类应该有很多有用的方法,可以执行 fread()、fwrite()、fgets()、fputs()、fseek() 以及其他任何操作(fprintf()?)。与开放/构造模式不兼容的方法只会抛出(就像某些集合类/方法在受到限制时所做的那样)。
然后,有一堆接口建议您在创建流后打算如何使用该流:顺序、随机访问、只读、只写、文本、二进制,以及这些有意义的组合。也许甚至有方法返回适当的类型转换(接口),如果您要求不兼容的东西,就会抛出错误。
为了获得额外的风味,请跳过声明的异常 - 例如 - javax.stdio.IOException 扩展 RuntimeException。
有这样的开源项目吗?
I have been doing Java on and off for about 14 years, and almost nothing else the last 6 years or so.
I really hate the java.io package -- its legion of subclasses and adapters. I do like exceptions, rather than having to always poll "errno" and the like, but I could surely live without declared exceptions.
Is there anything that functions like the Unix/ANSI stdio.h routines in C?
I know we will never be rid of java.io and its conventions until java itself is retired, as they have metastasized throughout the many frameworks that have accreted to java. That said, I would like something that works kind of like this (let's call it package javax.stdio):
Have a main utility class, perhaps FileStar, that can read and write files (or pipes), either text or binary, either sequentially or random access, with constructors that mimic fopen() and popen().
This class should have a load of useful methods that do things like fread(), fwrite(), fgets(), fputs(), fseek(), and whatever else (fprintf()?). Methods that are incompatible with the open/construct mode simply throw up (just like some of the collections classes/methods do when restricted).
Then, have a bunch of interfaces that suggest how you intend to use the stream once you have created it: Sequential, RandomAccess, ReadOnly, WriteOnly, Text, Binary, plus combinations of these that make sense. Perhaps even have methods to return the appropriate type-cast (interface), throwing up if you have asked for something incompatible.
For extra flavor, skip the declared exceptions -- e.g. - javax.stdio.IOException extends RuntimeException.
Is there an open source project like this floating around?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Java IO 库不是最好的,但您基本上应该接受它。 C 中的代码类似于 C。Java 中的代码类似于 Java。 Java IO 类即使不是有点冗长,也是相当简单的。
您也可以使用 Java NIO 包,但它们往往有更专门的用例。
The Java IO library isn't the best but you should basically live with it. Code like C in C. Code like Java in Java. The Java IO classes are fairly straightforward if not a little verbose.
You could alternatively use the Java NIO packages but they tend to have more specialized use cases.
您研究过 java.nio 和 java.nio.file (在 Java 7 中)包吗?
Have you investigated the java.nio and java.nio.file (in Java 7) packages?
据我所知,没有什么比 Java 的 stdio 更直接的了。我不认为它可以用纯 Java 实现。
实际上,我认为您会发现大多数具有 C 背景的经验丰富的 Java 开发人员认为 Java I/O 是对 C stdio 的巨大改进。特别是现在 Java 已经有了 printf 和 scanf 的类似方法。
As far as I know, there is nothing directly like stdio for Java. And I don't think that it would be implementable in pure Java.
Actually, I think you would find that most experienced Java developers with a C background consider Java I/O to be a vast improvement over C stdio. Especially now that Java has analogs for printf and scanf.