Reputation: 4853
I have been asked in a interview that how to display a entire source program as output when the same program is in execution
File-handling
concept for this , is there any other way to achieve this task Any suggestion for this ?
Updated Solution : As i researched something i got a solution , we can do it by Quine
String array
..as @Basile commented belowUpvotes: 4
Views: 1097
Reputation: 54672
You can code like this to print the source. Is it what you are looking for?
class A{
public static void main(String args[]) throws Exception {
FileReader f = new FileReader(filePathOfThisClass);
BufferedReader b = new BufferedReader(f);
String s= null;
while ((str = b.readLine()) != null)
System.out.println(s);
}
}
UPDATE
according to Basile's comment I was curious. and hence i found several options. check this page for example programes from quine page
http://www.nyx.net/~gthompso/self_java.txt
Upvotes: 0
Reputation: 1315
This is a program that when run will display itself.The secret is to create an array of Strings that contain the linesof the program, except for the Strings in the array itself.So you have a first loop to display the lines of code before thearray of Strings.Then you have a loop to display the array Strings, and then youhave a loop to display the array Strings that represent the remaininglines of code.
Note that the ASCII value for a space character is 32 andthe ASCII value for a double quote mark is 34.The reason for creating 'quote' and 'sixSpaces' below is so that whendisplaying them, is to avoidhaving to use the escape char \, whentrying to display quotes.
public class ProgramThatDisplaysItself {
public static void main(String[] args) {
char space = (char)32;
char quote = (char)34;
String emptyStr = new String();
String spaceStr = String.valueOf(space);
String sixSpaces =
emptyStr.concat(spaceStr).concat(spaceStr).concat(spaceStr)
.concat(spaceStr).concat(spaceStr).concat(spaceStr);
String linesOfCode[] = {
"package programthatdisplaysitself;",
"",
"public class ProgramThatDisplaysItself {",
"",
" public static void main(String[] args) {",
" char space = (char)32;",
" char quote = (char)34;",
" String emptyStr = new String();",
" String spaceStr = String.valueOf(space);",
" String sixSpaces = ",
" emptyStr.concat(spaceStr).concat(spaceStr).concat(spaceStr)",
" .concat(spaceStr).concat(spaceStr).concat(spaceStr);",
"",
" String linesOfCode[] = {",
// Note: here's where the String array itself is skipped
" };",
"",
" for ( int i = 0; i < 14; i++ ) {",
" System.out.println( linesOfCode[i] );",
" } // end for i",
"",
" for ( int j = 0; j < linesOfCode.length; j++ ) {",
" System.out.println( sixSpaces + quote + linesOfCode[j] + quote + ',' );",
" } // end for j",
"",
" for ( int k = 14; k < linesOfCode.length; k++ ) {",
" System.out.println( linesOfCode[k] );",
" } // end for k",
"",
" } // end main()",
"",
"} // end class ProgramThatDisplaysItself",
}; // end linesOfCode[]
//display the lines until the String array elements
for ( int i = 0; i < 14; i++ ) {
System.out.println( linesOfCode[i] );
} // end for i
//display the String array elements
for ( int j = 0; j < linesOfCode.length; j++ ) {
System.out.println( sixSpaces + quote + linesOfCode[j] + quote + ',' );
} // end for j
//display the lines after the String array elements
for ( int k = 14; k < linesOfCode.length; k++ ) {
System.out.println( linesOfCode[k] );
} // end for k
} // end main()
} // end class ProgramThatDisplaysItself
Upvotes: 1
Reputation: 619
Once it's in bytecode I don't think you can get the source code from it. The only way I could see to achieve this is if the program held a string copy of it's code or load the same source from a file used to compile and print it out.
Edit: After checking Quine computer, the example was to use a string inside the program with was a copy of the code.
Upvotes: 0