0

Hy, I am currently learning Java, and I ve read a lot about file handling in Java but I did not understand yet.


import java.math.BigInteger;
import java.util.Scanner;

public class multiply {

    public static void main(String []args) {
            Scanner cin = new Scanner(System.in);  // Create a Scanner object

            String firstNumber = "";
            int n = cin.nextInt();
            while (n != 0) {
                int number = cin.nextInt();
                firstNumber = firstNumber + number;
                n--;
            }
            BigInteger number1 = new BigInteger(firstNumber);
            
            String firstNumber2 = "";
            int n2 = cin.nextInt();
            while (n2 != 0) {
                int number2 = cin.nextInt();
                firstNumber2 = firstNumber2 + number2;
                n2--;
            }
            BigInteger number2 = new BigInteger(firstNumber2);
            //BigInteger res = new BigInteger(number1.multiply(number2));
            
            System.out.println(number1.multiply(number2));
        
    }

}

I ve came up with this program that multiplies big integer but, I dont know what should I add such that it reads from a file named "multiply.in" and it shows the result in a file "multiply.out".

In c++, I know that using ifstream, ofstream solves the problem.

If you could help me with the code it would be very appreciated.

  • 1
    Please find the resources which might help. https://www.baeldung.com/java-write-to-file https://www.baeldung.com/reading-file-in-java – Vignesh_A Jun 21 '20 at 12:58
  • Does this answer your question? [Give standard input to program through files and take standard output to a file](https://stackoverflow.com/questions/20867009/give-standard-input-to-program-through-files-and-take-standard-output-to-a-file) – Nowhere Man Jun 21 '20 at 13:09
  • Does this answer your question? [Java read from one file and write into another file using methods](https://stackoverflow.com/questions/28290991/java-read-from-one-file-and-write-into-another-file-using-methods) – Anakhand Jun 21 '20 at 16:57

1 Answers1

0

This code reads a file and takes only integer values from first line. and does the operations like add and multiply and writes to a file.

            public static void main(String args[]) throws IOException {
            Scanner cin = new Scanner(System.in);  // Create a Scanner object
    
            File file = new File("/home/Downloads/multiplyin.txt");
            File fileOut = new File("/home/Downloads/multiplyOut.txt");
    
            BufferedReader reader = new BufferedReader(new FileReader(file));
            String firstNumber = reader.readLine();
            System.out.println("input 1");
            int n = cin.nextInt();
            System.out.println("input 2");
            int n2 = cin.nextInt();
    
            while (n != 0) {
                System.out.println("sub inputs" + n);
                int number = cin.nextInt();
                firstNumber = firstNumber + number;
                n--;
            }
            BigInteger number1 = new BigInteger(firstNumber);
    
    
            while (n2 != 0) {
                System.out.println("sub inputs" + n);
                int number2 = cin.nextInt();
                firstNumber = firstNumber + number2;
                n2--;
            }
            BigInteger number2 = new BigInteger(firstNumber);
    
            BufferedWriter writer = new BufferedWriter(new FileWriter(fileOut));
            writer.write(String.valueOf(number1.multiply(number2)));
            writer.close();
            reader.close();
        }
    }
Vignesh_A
  • 508
  • 1
  • 7
  • 19