DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
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 01:49 PM.

Similar Threads

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

Bookmarks

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


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links