Results 1 to 3 of 3
  1. #1
    AniMaind is offline Member
    Join Date
    Dec 2010
    Posts
    10
    Rep Power
    0

    Default reading from a file

    hey every one
    i need some help with reading from a file
    i'm trying to read from a file and put the input in an array
    for example this is what's in the file (Adam,ross,sarah,susan) i want to read till " , " and put every name in a column of the array
    thanx

  2. #2
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Wink

    Here is what you need to do,

    Java Code:
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileReader;
    import java.io.IOException;
    
    public class Split {
    	
    	public static void main(String args[]) {
    		File file = new File("test.txt");
    		try {
    			FileReader fr = new FileReader(file);
    			BufferedReader reader = new BufferedReader(fr);
    			String read = "";
    			while ((read = reader.readLine()) != null) {
    				String[] tokens = read.split(",");
    				for (int i = 0; i < tokens.length; i++) {
    					System.out.println("Array element[" + i + "]: " + tokens[i]);
    				}
    			}
    			reader.close();
    		} catch (IOException io) {
    			io.printStackTrace();
    		}
    	}
    }
    Here, make sure that the "test.txt" file is in the root folder and contains the same pattern mentioned by you.

    The result is,

    Java Code:
    Array element[0]: Adam
    Array element[1]: ross
    Array element[2]: sarah
    Array element[3]: susan
    What we have used is the String.split(String regex) method. It returns the array of Strings containing the data separated by the regex pattern provided by you. In our case it is "," which is separating the four name values in the file.

    Later, you can iterate over the array and display the contents as you want.

    Hope that helps,

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  3. #3
    AniMaind is offline Member
    Join Date
    Dec 2010
    Posts
    10
    Rep Power
    0

    Default thanx

    thanx
    that was quick and helpful:) ,however it didn't give me the result that i was looking for ,you see the file that i'm trying to read from is written like this
    (List of cars to use: ("MA06 XVU", 8999.0, new
    CarSpec("Saloon","Ford","Mondeo", (float)1.6,"Diesel", (int)12,"Blue")); ("MA06
    UXW", 12900.0, new CarSpec("Estate","Ford","Mondeo", (float)1.8,"Petrol",
    (int)6,"Red")); ) i know it's my bad i'm sorry i didn't give you all the info but i thought i can work it out by my self :o
    so can you help me out please :D

Similar Threads

  1. reading a file and writing to a file....help!!!!
    By java_prgr in forum New To Java
    Replies: 3
    Last Post: 07-26-2010, 06:53 PM
  2. Reading a file
    By Ranu in forum New To Java
    Replies: 1
    Last Post: 07-06-2010, 07:49 AM
  3. Replies: 9
    Last Post: 10-20-2009, 10:52 AM
  4. Replies: 3
    Last Post: 05-09-2009, 11:31 PM
  5. Reading a file
    By mew in forum New To Java
    Replies: 2
    Last Post: 12-30-2007, 12:23 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
  •