Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Reading files

I am trying to read a file which has multiple lines of text.

I can open the file and read it but i would like to be able to put it into a string or something so i can use it again in other objects.

I need to create an object (which i have done) which displays the contents of the file and i also need to create an object which counts the words (which i have done also).

I need to be able to use a variable which holds the entire string of what's in the file so i can reuse it but am unsure how I would do it.

Here is the code that i have which reads the file in and displays it but is there a way i can read the file once and reuse what is read by other objects?

String textline, getFile;

//Constructor
public Document(String fileName)
{
getFile = fileName;
}

//Method for reading in the file text
public void readFile() throws IOException
{
//Open the requested file for reading
FileReader file = new FileReader(getFile);
BufferedReader MyFile = new BufferedReader(file);
while((textline = MyFile.readLine()) !=null)
{
System.out.println(textline);
}
}
[1216 byte] By [AdRock] at [2007-11-11 10:08:19]
# 1 Re: Reading files
FileReader file = new FileReader(getFile);
BufferedReader MyFile = new BufferedReader(file);
while((textline = MyFile.readLine()) !=null)
{
System.out.println(textline);
}

just create another variable, lets call it, String source; and then instead of s.o.p'ing the contents of the file just go

source += textline;
anubis at 2007-11-11 22:31:59 >
# 2 Re: Reading files
Thanks...words fine
AdRock at 2007-11-11 22:32:54 >