open a file within a class and be able to access it?
hi,
would like to reduce the code sitting in my main within a class.
My problem is not full understanding how i can do this!
I've been thinking along the lines of declaring the fileinputstream & Bufferstream variables as Private class variables and having the code that opens the files for reading in a method. But this fails.
what i want to be able to do is to ....
have methods for opening, closing, reading in a line etc
so alls i have in my main is a loop that tests for EOF and if encountered exits.
all the donkey work is passed onto methods which will allow the breakup of the work.
this program is a consoleno GUI stuff envolved as I'm newish to java but not programming concepts, OOP does defeat me sometimes due to not being able to put understanding of principles into action.
here is a sample of what i thought would work
import java.io.*
import java.util.
publicclass readcsv
{
//the Brace's were one of many attempts to try and get it working.
{
// declare variables so i can use them in Methods
FileReader fr =null;
BufferReader br =null;
// Verifies the file is open no more !
Boolean FileOk;
}
publicstaticvoid main( String [] args )
{
FileOk = OpenFile();// my method for opening the file i want read.
further code would call a readline
another method to process the data etc etc
}
// Method to open the data file and load br with the 1st line of data!
Boolean FileOK()
{
returnfalse;
try
{
FileReader fr =new filereader("mydata.txt");
BufferedReader br =new BufferedReader(fr);
// Test here for the file being opened & if it is return true
}
catch (Exceptionerrors e) /knowthis isn't right but more to show
{
System.err.println("failed to open the file!");
}
}
Know the above isn't correct but i hope it give you an idea of my aim.
haven't tried passing the variables as parameters tho but since thought of it i will give this a try.
if someone could point me to a good worked tutorial or pass on a sample bit of code it would be appreciated.
eg
program will take a file name display data to the console screen then exit.
now lets look at where and what is going on type details.
also i have gone trolling google but from what i've seen it's a bit general overview stuff not like ....
any thanks.
dave.

