Reading and Writing Files

How do I read and write files that are not sequential-access or
random-access? I am currently looping through a array of values that
represent various devices we are monitoring. The data is currently being
written into these files by a Perl program, but we now have a new Java /
Corba API process that is much faster at retrieving the data. The
problem I am running into is that I want to keep these files looking the
same as they do now to help expedite the new process. (Later I am going
to use a database because that is much better, but the boss wants this
new process up an running quickly.) Are there JAVA equivalents for the
following Perl commands?

open (INPUT, "$infile")

open (OUTPUT, ">$outfile")

Please note that the output has the > in it so that it can be written to.

Thanks...

Paul

Comments

  • : How do I read and write files that are not sequential-access or
    : random-access? I am currently looping through a array of values that
    : represent various devices we are monitoring. The data is currently being
    : written into these files by a Perl program, but we now have a new Java /
    : Corba API process that is much faster at retrieving the data. The
    : problem I am running into is that I want to keep these files looking the
    : same as they do now to help expedite the new process. (Later I am going
    : to use a database because that is much better, but the boss wants this
    : new process up an running quickly.) Are there JAVA equivalents for the
    : following Perl commands?
    :
    : open (INPUT, "$infile")
    :
    : open (OUTPUT, ">$outfile")
    :
    : Please note that the output has the > in it so that it can be written to.
    :
    : Thanks...
    :
    : Paul
    :
    :

    In Java you use FileInputStream and FileOutputStream to open and write files.
    --=][tReShR][=--

  • Hi Paul,

    I'm not sure I did unterstand you right, but let's have a try:
    I don't think there is an equivalent in Java to your Perl instructions

    The following code opens a file, reads out the content and writes it back... this should cover all the functionality you are looking for, please tell me if I missed the point:

    [code]
    java.io.File f = new java.io.File("/monitors/stream.a");
    try {
    java.io.FileInputStream fis = new java.io.FileInputStream(f);
    long len = f.length();

    if (len > Integer.MAX_VALUE) {
    //just for the future ;)
    throw new java.lang.Exception("File too large!");
    }

    //content buffer for reading...
    byte[] content = new byte[(int)len];

    //read the file
    fis.read(content);
    //don't leave it out...
    fis.close();

    //just read or do something with the content...
    java.io.FileOutputStream fos = new java.io.FileOutputStream(f);
    fos.write(content);
    fos.close();

    } catch (java.io.FileNotFoundException fnf) {
    System.err.println("File not found");
    } catch (java.io.IOException io) {
    System.err.println("I/O error at reading/writing");
    } catch (java.lang.Exception e) {
    System.err.println(e.getMessage());
    }
    [/code]

    greetings moo
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion