views:

85

answers:

3

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?

+4  A: 

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.

cletus
A: 

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.

Stephen C
A: 

Have you investigated the java.nio and java.nio.file (in Java 7) packages?

Steve Emmerson
From what I have seen of NIO, it seems to be oriented towards more complex stuff like polling multiple channels and selecting the one with current activity for a thread to act on. But, I could be missing something.
Roboprog