Reading files


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Reading files

  1. #1
    Join Date
    Jan 2007
    Posts
    60

    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?

    Code:
    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);
    		}		
    	}

  2. #2
    Join Date
    Dec 2005
    Posts
    97
    Code:
    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
    Code:
    source += textline;

  3. #3
    Join Date
    Jan 2007
    Posts
    60
    Thanks....words fine
    Last edited by AdRock; 01-08-2007 at 02:49 PM.

Similar Threads

  1. Reading XML Files
    By skuppusamy in forum VB Classic
    Replies: 0
    Last Post: 05-10-2006, 12:22 PM
  2. Reading in text files
    By cupanTae in forum Java
    Replies: 1
    Last Post: 01-08-2006, 04:28 PM
  3. Replies: 2
    Last Post: 02-28-2002, 11:53 AM
  4. reading multiple text files
    By simon in forum XML
    Replies: 6
    Last Post: 07-27-2001, 01:58 PM
  5. Reading operating system files
    By Venky in forum XML
    Replies: 4
    Last Post: 09-27-2000, 06:09 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center