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

Inputting files into Java

Options
  • 25-10-2001 9:42pm
    #1
    Registered Users Posts: 68,317 ✭✭✭✭


    OK, I need to input a file into my prog in order to be able to store some output. My lecturer insists that in projects we hand up the output of our code, obviously, but up till now, I've been using the execution log :p in jbuilder and just copy and paste. This prog takes a command from the user through the BufferedReader and gives an appropriate response. I just need to know how to get my prog to read a file containg a list of these commands. I'm assuming it's something to do with the InputStreamReader, but havent a clue what to do. When creating a new BufferedReader, I use the line,
    BufferedReader stdin = [b]new[/b] BufferedReader([b]new[/b] InputStreamReader(System.in));
    

    as always. Do I make changes here?

    And then at the command prompt, what do I type?(god I sound like a noob):rolleyes:

    I was typing: java MyClass.class < input.txt > output.txt
    but this is obviously wrong.

    Thanks all.


Comments

  • Closed Accounts Posts: 1,322 ✭✭✭phobos


    Howya doin Seamus.

    Right file IO in Java is a cinch, so this link below from Sun, should sort out what ales ya! ;)

    http://java.sun.com/docs/books/tutorial/essential/io/filestreams.html

    Post further Q's below...

    ;-phobos-)


  • Registered Users Posts: 1,481 ✭✭✭satchmo


    Yeah it's very simple. Change that line to
    BufferedReader fileIn = [b]new[/b] BufferedReader([b]new[/b] FileReader("input.txt"));
    
    then you can use fileIn.readLine() to read a line from the file (returns a String). It will return null if you've reached the end of the file.

    For output, use
    PrintStream fileOut = [b]new[/b] PrintStream([b]new[/b] FileOutputStream("output.txt",true));
    
    and then you can use fileOut.println("blah blah"); to write a line. The true at the end is whether you want to append to the file if it already exists. If you want to replace the file instead of appending, use false, or just leave out that parameter altogether.

    There are tons of different ways to input & output files in Java, I like these cause they have lots of handy methods built-in.


  • Registered Users Posts: 68,317 ✭✭✭✭seamus


    Cheers Jazz - that's the quick fix I needed, and cheers too phobos, I'll give that a gloss over while I'm bored, after all, I really should know how to do it in 2nd year :). I think though I'll just use the standard DOS parameter '> output.txt' for the output. Whole thing is that we have to write a utility class for stacks, and the lecturer gave us the prog that we have to use to test our class, and we 'aren't allowed alter it', so if I fúck around too much with his test prog he'll know.:p


Advertisement