The Artima Developer Community
Sponsored Link

Java Answers Forum
array problems!

6 replies on 1 page. Most recent reply: Aug 6, 2003 2:01 AM by Michael Ritchie

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 6 replies on 1 page
Michael Ritchie

Posts: 3
Nickname: ritchie
Registered: Aug, 2003

array problems! Posted: Aug 5, 2003 10:41 AM
Reply to this message Reply
Advertisement
Im trying to write part of a program where i have to read in data from a text file (all String) and then store it in an 'appropriate object'. I think the best way is to put it into an array but im not sure how to read straight from the file then store into the array can anyone help??


Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: array problems! Posted: Aug 5, 2003 10:54 AM
Reply to this message Reply
Here is a method which reads the contents of a text file into a String.

    public String readFile(File f){
        System.out.println("Reading file: " + f.getName());
        String text = "";
        try{
            String lineRead = "";
            BufferedReader br = new BufferedReader(new FileReader(f));
            while ((lineRead = br.readLine()) != null){
                text = text + lineRead + "\n";
            }
        }catch(FileNotFoundException fnfe){
            System.err.println("FileNotFoundException: " + fnfe.getMessage());
        }catch(IOException ioe){
            System.err.println("IOException: " + ioe.getMessage());			
        }
        return text;
    }
 

Michael Ritchie

Posts: 3
Nickname: ritchie
Registered: Aug, 2003

Re: array problems! Posted: Aug 5, 2003 11:23 AM
Reply to this message Reply
OK thanks, but how do i put that into an array? im new to java as you can tell!

Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: array problems! Posted: Aug 5, 2003 12:08 PM
Reply to this message Reply
    public String[] readFile(File f){
        System.out.println("Reading file: " + f.getName());
        String text = "";
        String[] lines = null;
        try{
            String lineRead = "";
            int n = 0;
            BufferedReader br = new BufferedReader(new FileReader(f));
            while ((lineRead = br.readLine()) != null){
                text = text + lineRead + "\n";
                n++;
            } 
            lines = new String[n];
            int i = 0;
            StringReader sr = new StringReader(text);
            br = new BufferedReader(sr);
            while ((lineRead = br.readLine()) != null){
                lines[i] = lineRead;
                i++;
            }                       
        }catch(FileNotFoundException fnfe){
            System.err.println("FileNotFoundException: " + fnfe.getMessage());
        }catch(IOException ioe){
            System.err.println("IOException: " + ioe.getMessage());
        }
        return lines;
    }
 
 

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: array problems! Posted: Aug 5, 2003 12:59 PM
Reply to this message Reply
Or use Jython:
linesArray = file( filename ).readlines()


;-0

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: array problems! Posted: Aug 5, 2003 1:04 PM
Reply to this message Reply
By the way, in the Java, wouldn't it be simpler and clearer to read into an ArrayList then, if necessary, convert that to an array?

Michael Ritchie

Posts: 3
Nickname: ritchie
Registered: Aug, 2003

Re: array problems! Posted: Aug 6, 2003 2:01 AM
Reply to this message Reply
OK thanks guys that helped a lot!

Flat View: This topic has 6 replies on 1 page
Topic: Native Class Previous Topic   Next Topic Topic: integrating java with crystal report

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use