Results 1 to 4 of 4
Thread: Creating Objects from txt file?
- 03-10-2011, 10:06 PM #1
Member
- Join Date
- Nov 2010
- Posts
- 4
- Rep Power
- 0
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
-
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.
- 03-11-2011, 12:22 AM #3
Senior Member
- Join Date
- Jan 2009
- Posts
- 671
- Rep Power
- 5
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.
- 03-11-2011, 07:40 PM #4
Senior Member
- Join Date
- Nov 2010
- Location
- Delhi
- Posts
- 135
- Blog Entries
- 1
- Rep Power
- 0
I think this is what Will wants to do:
use getConstructors() api of Class to have better controlJava 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(); } } }
Similar Threads
-
Creating an Array of Objects
By int80 in forum New To JavaReplies: 4Last Post: 08-09-2011, 12:40 PM -
creating public objects
By TaxpayersMoney in forum New To JavaReplies: 2Last Post: 05-19-2010, 06:50 PM -
Creating Array of Objects
By chathurajeewaka in forum New To JavaReplies: 4Last Post: 12-03-2009, 03:23 PM -
Creating an array of objects
By geowizard in forum New To JavaReplies: 5Last Post: 11-16-2009, 01:25 AM -
read txt file,with some records, create objects and store objects in tables of a db.
By stamv in forum JDBCReplies: 1Last Post: 01-22-2009, 04:25 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks