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.

[3665 byte] By [dglnza] at [2007-11-27 4:05:18]
# 1
Why don't you just put all the code for the file into a separate method?What exactly are you trying to delegate to the method? The complete opening, reading and closing?
nogoodatcodinga at 2007-7-12 9:10:16 > top of Java-index,Java Essentials,Java Programming...
# 2

Possible implementation:

import java.io.BufferedReader;

import java.io.FileReader;

public class Test {

public static BufferedReader OpenFile(String fileName) throws Exception {

return new BufferedReader(new FileReader(fileName));

}

public static void processLine(String line) {

// do something with string line

}

public static void closeReader(BufferedReader in) {

if (in != null)

try {

in.close();

} catch (Exception e) {

// TODO: handle exception

}

}

public static void main(String[] args) {

BufferedReader in = null;

try {

in = OpenFile("fileName");

String line;

while ((line = in.readLine()) != null) {

processLine(line);

}

} catch (Exception e) {

// TODO: handle exception

} finally {

closeReader(in);

}

}

}

java_2006a at 2007-7-12 9:10:16 > top of Java-index,Java Essentials,Java Programming...
# 3

To nogoodatcoding,

Yes i am after a solution of do just that.

Found a web page that showed me some along the lines of what java_2006 has posted.

This then helps me break up my main into a controlling loop with calls made to methods or procedures (if you like) to handle the processsing.

what i wasn't sure of but thought of after looking posting was what if...

i decleared the filestream as i did but pass it and the bufferedstream to methods as required.

thank you both for the time spent.

java_2006 your sample code is very much like what i'm after :)

I hope i can get further with my little project.

dave.

dglnza at 2007-7-12 9:10:16 > top of Java-index,Java Essentials,Java Programming...