How to write a class to a text file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • momotaro
    Contributor
    • Sep 2006
    • 357

    How to write a class to a text file

    Hi all,

    Let's say that I have a class as an object somewhere in my program and I want to write this object down into a text file but not as an object but as the real text representation in this file like:

    public class MyClass {
    private type attribute1
    ...
    .
    }
  • dlite922
    Recognized Expert Top Contributor
    • Dec 2007
    • 1586

    #2
    You have to get every piece of information yourself. See the java docs on how to get the data members, methods, etc.

    Not sure if you can get the code inside the methods though. Don't think that's possible.

    Why do you want to do this? Can't you just save the .java file as a text file somewhere else not compiled with your program?

    This seems kind of like a very strange thing to do.


    Dan

    Comment

    • Dheeraj Joshi
      Recognized Expert Top Contributor
      • Jul 2009
      • 1129

      #3
      I guess this is not possible. Object is a real time entity, you can not make it to represent the class declaration.

      Anyway what are you trying to achieve? If you share what are you trying to do, we can suggest a alternate solution, if there is one.

      Regards,
      Dheeraj Joshi

      Comment

      • Markus
        Recognized Expert Expert
        • Jun 2007
        • 6092

        #4
        Reflection may help you .

        Comment

        • Dheeraj Joshi
          Recognized Expert Top Contributor
          • Jul 2009
          • 1129

          #5
          Reflection may help you .
          .
          Mark. I don't think reflection is of much help in this case :)

          Regards
          Dheeraj Joshi

          Comment

          • Markus
            Recognized Expert Expert
            • Jun 2007
            • 6092

            #6
            Why not? Isn't this really what reflection is for? The OP wants information about an object, reflection gives you information about an object. At least that's the way I understand it (in languages other than Java).

            Comment

            • Dheeraj Joshi
              Recognized Expert Top Contributor
              • Jul 2009
              • 1129

              #7
              He want to write the object as the .java file.
              object down into a text file but not as an object but as the real text representation in this file like:

              public class MyClass {
              private type attribute1
              ...
              .
              }
              Using reflection you can not write the object as its .java file. You can take methods, fields, or instantiate a new object. Or you can serialize. But not what OP is asking.

              Regards
              Dheeraj Joshi

              Comment

              • momotaro
                Contributor
                • Sep 2006
                • 357

                #8
                Hi everyone thank you for your answers and sorry for being late to respond.
                what am trying to do is quite simple,I have an XML file representing a data base, I managed to automatically generate instances of classes based on the XML file now I want these instance of classes to be printed down in a file.java.

                thank's in advance.

                Comment

                • Markus
                  Recognized Expert Expert
                  • Jun 2007
                  • 6092

                  #9
                  Dheeraj, if you have reflection data, then surely you can write it to a file with Java.

                  Code:
                  var reflection-data = object.get-reflection-data
                  var file-handle = file-path.open-for-writing
                  
                  file-handle.write: 
                    reflection-data.class-access class reflection-data.class-name {
                      // etc
                    }
                  I wrote it in pseudo-code because I don't do Java :P

                  Comment

                  • Dheeraj Joshi
                    Recognized Expert Top Contributor
                    • Jul 2009
                    • 1129

                    #10
                    OK Mark, Then how you will differentiate the private members, access specifiers etc?And methods Signatures? Return types? What if there is a anonymous inner classes?

                    @momotaro: Are you asking about serialization?

                    Regards
                    Dheeraj Joshi
                    Last edited by Dheeraj Joshi; Jul 18 '11, 12:28 PM.

                    Comment

                    • Markus
                      Recognized Expert Expert
                      • Jun 2007
                      • 6092

                      #11
                      I thought reflection provided that information.

                      Private members: http://tutorials.jenkov.com/java-ref...d-methods.html
                      Shows return types, etc: http://java.sun.com/developer/techni...LT/Reflection/

                      Comment

                      • momotaro
                        Contributor
                        • Sep 2006
                        • 357

                        #12
                        Hi again,
                        It's ok I managed to write them down now based on that I have again to work on ORMs...
                        I'll keep you posted.
                        thx.

                        Comment

                        Working...