import java.io.*;
public class MyProg
{
public static void main(String[] args)
throws IOException, InterruptedException
{
String fname = "Prog1.java";
BufferedWriter bw;
try{
bw = new BufferedWriter(
new FileWriter(fname)
);
}
catch (IOException e) {
System.out.println("Cannot open " + fname + "!");
return;
}
final String NL = System.getProperty("line.separator");
bw.write("public class Prog1{" + NL
+ "\tpublic static void main(String[] args) {" + NL
+ "\t\tSystem.out.println(\"hello world\");" + NL
+ "\t}" + NL
+ "}" + NL
);
bw.close();
BufferedReader br;
try {
br = new BufferedReader(
new FileReader(fname)
);
}
catch (FileNotFoundException e) {
System.out.println(fname + " not found!");
return;
}
String line;
while( (line = br.readLine()) != null )
{
System.out.println(line);
}
Process p1 = new ProcessBuilder(
"javac", "Prog1.java"
).start();
int error = p1.waitFor();
System.out.println("p1: " + error);
/*
Process p2 = new ProcessBuilder(
"java", "-cp . Prog1"
).start();
error = p2.waitFor();
System.out.println("p2: " + error);
*/
FileInputStream fin;
try {
fin = new FileInputStream(fname);
}
catch (FileNotFoundException e) {
System.out.println(fname + " not found!");
return;
}
int abyte;
while((abyte = fin.read()) != -1)
{
System.out.println(abyte);
}
fin.close();
}
}
--output:--
public class Prog1{
public static void main(String[] args) {
System.out.println("hello world");
}
}
p1: 0
112
117
98
108
105
99
...
...
125
10