Java 如何创建一个文件来写入两个二维数组的乘法结果?

Java 如何创建一个文件来写入两个二维数组的乘法结果?,java,arrays,io,console,try-catch,Java,Arrays,Io,Console,Try Catch,我读到一个问题,要求编写一个程序,该程序执行以下操作: 从两个文件中读取两个不同的矩阵。一个是3x4矩阵,另一个是4x3矩阵。 将这两个矩阵相乘的结果写入文件。 我初始化了所需的数组,如下所示: int [][] m1 = new int [3][4]; int [][] m2 = new int [4][3]; int [][] m3 = new int [3][3]; 我编写了打开两个输入文件所需的代码 Scanner input1 = null; Scanner input2 = nul

我读到一个问题,要求编写一个程序,该程序执行以下操作:
  • 从两个文件中读取两个不同的矩阵。一个是3x4矩阵,另一个是4x3矩阵。
  • 将这两个矩阵相乘的结果写入文件。
    我初始化了所需的数组,如下所示:

    int [][] m1 = new int [3][4];
    int [][] m2 = new int [4][3];
    int [][] m3 = new int [3][3];
    
    我编写了打开两个输入文件所需的代码

    Scanner input1 = null;
    Scanner input2 = null;
    
    PrinWriter output = null;
    
    // first file
    try {
        Scanner input1 = new Scanner(new FileInputStream("file1.txt"));
    } catch (Exception e) {
        System.out.println("The file cannot be open");
    }
    
    // second file
    try {
        Scanner input2 = new Scanner (new FileInputStream("file2.txt"));
    } catch (Exception e){
        System.out.println("The File Cannot be open");
    }
    

    但是我无法处理try&catch,以及如何读取两个文件的内容,这两个文件的内容是我需要处理的,以便将它们写入结果文件中。您需要在try/catch中执行文件IO,但您可能缺少的是,当您从文件中读取值时,可以填充应该定义的数组超出try/catch的范围

    int[][] array1 = ...;
    int[][] array2 = ...;
    
     try{
         // read file 1 and populate array 1
     catch(IOException e){
         // log failure here, possibly exit application
     }
     try{
         // read file 2 and populate array 2
     catch(IOException e){
         // log failure here, possibly exit application
     }
    
      // do array arithmetic
    
     try{
         // write output file file
     catch(IOException e){
         // log failure here, possibly exit application
     }
    

    你说你不能处理“试抓”是什么意思?你哪里有困难?异常是IO的一个非常抽象的异常,你需要IOException。如果出现异常但不知道详细信息,请尝试打印堆栈跟踪:e.printStackTrace();