Results 1 to 4 of 4
  1. #1
    Bread is offline Member
    Join Date
    Nov 2010
    Posts
    4
    Rep Power
    0

    Default Creating Objects from txt file?

    Hey, I've been reading a little about Object I/O Stream, and I'm not sure if I'm understanding it. Can you create new objects from scratch without writing any code?

    Here's an example of what I mean

    Let's say I want to create an instance of an Enemy from a txt file like this:
    //Object Name x location y location width height
    Enemy1 16 16 16 16


    Would I be able to do
    Enemy enemy1 = new Enemy1(16,16,16,16);
    Without actually typing it in the actual code by using a file?


    Regards,
    -Will

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    You can't use a text file for standard serialization/deserialization, but I've heard that you can for xml serialization, but I may be wrong as I know nothing about this. What you want to do though is to read in the text file, parse the line and use the information that you've gained from parsing the line to create an appropriate object. You have to have "code" to do this, no two ways about it, but the code can be flexible and respond to the text entered.

    For instance, I'd read in the file line by line, and each line I'd split with the String#split method using whitespace as my split delimiter. Then the first item in the array would be the String representation of the object I want to create (here Enemy1), and the next numbers the parameters I'd want to pass into this object's constructor.

  3. #3
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    Sure, you can create objects on the fly by reading information from a text file. You can even create new classes on the fly. You can interpret javascript on the fly in java if you want.

    It would help to understand a little more about what you're trying to do before making suggestions of how to approach it.

  4. #4
    lovelesh is offline Senior Member
    Join Date
    Nov 2010
    Location
    Delhi
    Posts
    135
    Blog Entries
    1
    Rep Power
    0

    Smile

    I think this is what Will wants to do:

    Java Code:
    public class Dynamic {
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    
    		String object = "java.lang.String"; // consider this to be coming from a file
    		
    		try {
    			
    			Class classToBeInstantiated = Class.forName(object);
    			Object TargetObject = classToBeInstantiated.newInstance();
    			
    			if(TargetObject instanceof Integer) {
    				System.out.println(((Integer) TargetObject).intValue());
    			}
    			
    			if(TargetObject instanceof String) {
    				System.out.println(((String) TargetObject));
    			}
    			
    		} catch (ClassNotFoundException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		} catch (InstantiationException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		} catch (IllegalAccessException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    		
    	}
    
    }
    use getConstructors() api of Class to have better control

Similar Threads

  1. Creating an Array of Objects
    By int80 in forum New To Java
    Replies: 4
    Last Post: 08-09-2011, 01:40 PM
  2. creating public objects
    By TaxpayersMoney in forum New To Java
    Replies: 2
    Last Post: 05-19-2010, 07:50 PM
  3. Creating Array of Objects
    By chathurajeewaka in forum New To Java
    Replies: 4
    Last Post: 12-03-2009, 04:23 PM
  4. Creating an array of objects
    By geowizard in forum New To Java
    Replies: 5
    Last Post: 11-16-2009, 02:25 AM
  5. Replies: 1
    Last Post: 01-22-2009, 05:25 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
  •