-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpython.java
More file actions
40 lines (35 loc) · 1.48 KB
/
python.java
File metadata and controls
40 lines (35 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
public class python {
private static String inpString;
public python (String input){
inpString = input;
}
public static void Huffman_tree_visualisation() {
try {
String[] pythonCommand = {"python", "Huffman_tree_visualisation.py"};
ProcessBuilder pb = new ProcessBuilder(pythonCommand);
Process process = pb.start();
// Provide input to the Python script
OutputStreamWriter writer = new OutputStreamWriter(process.getOutputStream());
writer.write(inpString);
writer.flush();
writer.close(); // Close the writer when done writing input
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
BufferedReader errorReader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
while ((line = errorReader.readLine()) != null) {
System.out.println(line);
}
int exitCode = process.waitFor();
System.out.println("Python script execution completed with exit code: " + exitCode);
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
}
}