Cohagen
Cohagen

Reputation: 87

Program output to file in Java (and reading it again)

I have a large main method in an IO class which creates objects from four different classes in my program (which all use one another to some extent).

My main method takes all info in using a scanner, from the console window, and uses this info to call the constructors and methods in the other classes, through a menu system constructed using switch/case statements.

As this is my first full program in Java I have been focussed on making the main method work via the console, without properly considering file input and output.

I cannot see an easy way of making that work now. Ideally what I require is some way of writing everything I input to the console while running the main method to a file, in a format that can be read again and inputed back through the main method?

I have refrained from posting the main method as it is 250+ lines long, but will post any relevant parts of it if required.

Any help appreciated

Upvotes: 0

Views: 257

Answers (2)

matus
matus

Reputation: 1582

you can use Properties file to store or withdraw certain values. Look e.g. into official java tutorial

here is a part of it you can reuse to read properties:

FileInputStream propFile =
            new FileInputStream( "myProperties.txt");
        Properties p =
            new Properties(System.getProperties());
        p.load(propFile);

Upvotes: 0

Bohemian
Bohemian

Reputation: 425198

This is a basic design problem. You need to separate acquiring content from processing it, probably into two classes.

Once you have done that, you can easily pass the processing code either input from console or from a file (or from test code)

Upvotes: 1

Related Questions