Question

I have a program Main.java:

public class Main {
  public static void main() throws FileNotFoundException 
      {       
      BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
      System.out.println("Enter no: \t");
      int sq=0;
      try {
        sq=Integer.parseInt(br.readLine());
    } catch (IOException e) {           
        e.printStackTrace();
    }         
    System.out.println(sq*sq);
  }
}

I am not supposed to edit the above code(Main.java) and I should execute this program from another java program. So, I figured out the following code:

public class CAR {
public static void main(String[] args) {
    try {               
        Class class1 = Class.forName("executor.Main"); // executor is the directory in which the files Main.java and CAR.java are placed
        Object object = class1.newInstance();
        Method method = class1.getMethod("main", null);
        method.invoke(object, null);
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
}

By running CAR.java, the following is the output:

Enter no:   
2                  // this is the number I entered through the console
square is:   4

This works fine.But now, I need to input value to "sq" (variable in Main.java) not from the console but from a text file using the program CAR.java without editing Main.java. And I couldn't figure out how to do this with out editing the Main.java.

For example, If chech.txt has content as: 10 100. Then, by running CAR.java I should read the value 10 and give it to the waiting console to be asigned to the values of "sq" and compare the output printed on the console with 100. And print the output of the CAR.java as "Test passed".

Please suggest a solution for this.

The following code snippet could be added to CAR.java to read values from a file:

File f = new File("check.txt");
BufferedReader bf = new BufferedReader(new FileReader(f));
String r = bf.readLine();
String[] r1 = r.split(" ");
System.out.println("Input= " + r1[0] + "    Output=  " + r1[1]);
Was it helpful?

Solution

System.setIn() did the magic...
It specifies the jvm, to change the way of taking inputs from "System.in". Example:

System.setIn(new FileInputStream("chech.txt"));

This takes the input from "check.txt" instead of waiting for input from the console. Example program:

public class systemSetInExample {

public static void main(String[] args) {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

        try {
            System.out.println("Enter input:  ");
            String st=br.readLine();                 // takes input from console
            System.out.println("Entered:  "+st);    

            System.setIn(new FileInputStream("test.txt"));
            br=new BufferedReader(new InputStreamReader(System.in));
            st=br.readLine();                       // takes input from file- "test.txt" 
            System.out.println("Read from file:  "+st); 

    } catch (Exception e) {         
        e.printStackTrace();
    }
}

}

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top