0

I am trying to declare the main method as a string to print the output code to a text file. I tried something like:

String textoutput1 = ((main)); 

and

String textoutput1 = (main())

But it does not work. If you know a way to declare a main method as a string or any other Java data type please help.

Pshemo
  • 122,468
  • 25
  • 185
  • 269
Pro Gamer
  • 13
  • 5
  • there is no such possibility – Iłya Bursov Oct 09 '14 at 00:47
  • Not in Java. Try researching a more functional driven language. – mrres1 Oct 09 '14 at 00:49
  • The above statements don't really make sense, `main` aside. – Hot Licks Oct 09 '14 at 00:49
  • For clarification, do you intend to print the source code of the `main()` method to a file? Java is a compiled language, so the source code is lost on its own. – Ghostkeeper Oct 09 '14 at 00:50
  • `main` method's return type is `void` so it doesn't return any value (not even String). Maybe consider creating separate process in which you would invoke command like `java ClassWithYourMain` and just read output from that process (you can store it as String if you want). If you want to redirect output from current main method to file you can also use `System.setOut` method and pass `PrintStream` instance which will handle writing to file. – Pshemo Oct 09 '14 at 00:53
  • Yes I do intend to print the main source code output to a text file in a folder on my C:/ drive for clarification. – Pro Gamer Oct 09 '14 at 00:55
  • My answer is 'Not possible' :D – Sai Ye Yan Naing Aye Oct 09 '14 at 00:59
  • @SaiYeYanNaingAye I wouldn't be so sure :). There are few tricks which OP could use here depending on what (s)he really wants to achieve. – Pshemo Oct 09 '14 at 01:01

1 Answers1

0

That is not how you print output to a text file. You have two (basic) options: you can output to stdin using functions like System.out.println() and pipe that output to a file (java yourFile > foo.txt), or you can use file operations to write to the file directly from Java. I would recommend reading the Basic IO Tutorial.

asthasr
  • 9,125
  • 1
  • 29
  • 43