473,465 Members | 1,399 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How to write a class to a text file

momotaro
357 Contributor
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
...
.
}
Jul 14 '11 #1
11 2665
dlite922
1,584 Recognized Expert Top Contributor
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
Jul 14 '11 #2
Dheeraj Joshi
1,123 Recognized Expert Top Contributor
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
Jul 15 '11 #3
Markus
6,050 Recognized Expert Expert
Reflection may help you .
Jul 18 '11 #4
Dheeraj Joshi
1,123 Recognized Expert Top Contributor
Reflection may help you .
.
Mark. I don't think reflection is of much help in this case :)

Regards
Dheeraj Joshi
Jul 18 '11 #5
Markus
6,050 Recognized Expert Expert
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).
Jul 18 '11 #6
Dheeraj Joshi
1,123 Recognized Expert Top Contributor
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
Jul 18 '11 #7
momotaro
357 Contributor
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.
Jul 18 '11 #8
Markus
6,050 Recognized Expert Expert
Dheeraj, if you have reflection data, then surely you can write it to a file with Java.

Expand|Select|Wrap|Line Numbers
  1. var reflection-data = object.get-reflection-data
  2. var file-handle = file-path.open-for-writing
  3.  
  4. file-handle.write: 
  5.   reflection-data.class-access class reflection-data.class-name {
  6.     // etc
  7.   }
  8.  
I wrote it in pseudo-code because I don't do Java :P
Jul 18 '11 #9
Dheeraj Joshi
1,123 Recognized Expert Top Contributor
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
Jul 18 '11 #10
Markus
6,050 Recognized Expert Expert
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/
Jul 18 '11 #11
momotaro
357 Contributor
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.
Jul 19 '11 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: chuck clark | last post by:
Hi, I understand that for security and other reasons that javascript does not support writing/reading to text files. I am looking for a way that will however. This is what i am trying to do. I...
1
by: Mike John | last post by:
I want to write to text file, the follow text " My Name Is mike" Now, I have used the following sysntax: dim strFilePath as string = "C:\" dim strFileName as string = "MyFile.txt" ...
1
by: Erich | last post by:
Hi, everybody! I would like my WebApp (ASP.NET) writes a text-file in my Web Host, so I could download it and import it to another application. Does anybody know how can I write a text file...
4
by: Erich | last post by:
Hi, everybody! I would like that my WebApp generates a text-file in my ISP, so I could download it and import it to another application. Does anybody know how can I write a text file running...
1
by: Mike | last post by:
I need to search a server to see if files exist on that server or not. If the file does not exsit I need to write the filename to a text file, excel file, etc. what file acutally I have a list...
10
by: Mike | last post by:
I have a text file that i need to write multiple lines of data to but one at a time. how can i write more then one line of data to the file? example: i do a search for a file on a server if the...
1
by: Saravanan | last post by:
I need to read in a text file as strings and then write some of these strings into another text file. The output file is in the working directory while the input text file is not. how can I...
3
by: broli85 | last post by:
I am writing an extension for Firefox 1.5. I would like to know if there is a way to write and read data to and from a file which is in the location of the extension (or somewhere in the user's...
2
by: Wanjun Yu | last post by:
What is the class name for open/write/read a text file? I can't find it in the reference book. Thanks! WJ
11
by: vivekgrs | last post by:
hi my requirement is read the data from the text file through vb. then calculate the data and write the result on the same text file where the data has retrived thru vb 6.0. how 2 implement...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.