Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Java Problem

Options
  • 13-11-2001 11:45am
    #1
    Closed Accounts Posts: 22


    Hi,
    Have this to do and cant seem to figure it out. Ive been told its relatively easy to do.
    Given this code , we have to modify the program. At the minute it takes in input as a text file name from the command line, and also takes in an output file name from the command line. I have to modify it to take the input name from the command line( as it already does) and output back to the same file, overwriting it (eg test.txt as input is overwritten with the output)
    A temporary file was suggested, but i dont know how to do this. Any help greatly appreciated :)

    import java.io.*;

    class Convert {

    public void convert(InputStream in,OutputStream out) throws Exception
    {
    int b = in.read();
    while (b != -1)
    {
    if (b == 0x0A)
    {
    out.write(0x0A);
    out.write(0x0D);
    }
    else
    {
    out.write(b);
    }
    b = in.read();
    }
    }

    public static void main(String[] args)
    {
    if (args.length != 2)
    {
    System.out.println("Usage: java Convert <input file> <output file>");
    }
    else
    {
    try {

    Convert object = new Convert();
    FileInputStream input = new FileInputStream(args[0]);
    FileOutputStream output = new FileOutputStream(args[1]);
    object.convert(input,output);

    } catch (Exception exp) {
    exp.printStackTrace();
    }
    }
    }
    };

    :)


Comments

  • Registered Users Posts: 16,413 ✭✭✭✭Trojan


    Basically it's a design question that you're asking (also we won't give you code, against policy). There's a couple of different ways you can do this.

    You've got most of the idea there already with the temp file suggestion: you need to open source, open a temp file (check libararies to find out how to get a temporary file name if you want to store to disk), have a while loop copy off the source stream out to the temp file, then delete either a) the contents of source, or b) close and delete entire source file. Then you write it back out. That's one possible way of doing it. There's more.

    I don't think we can give you any more info, but let us know if you have any particular errors with the code, there's a fair few java guys in here (I aint one of them). GL.

    Al.


Advertisement