5

我在 java 中读取文件时遇到一些问题:我的文件例如:

3,4
2
6
4
1
7
3
8
9

其中第一行 3 和 4 是数组 A 和 B 的长度,然后是每个数组的元素。我做的

import java.io.*;
import java.util.Arrays;

public class Progetto  {

    public static void main(String args[])
      {
// Open the file that is the first 
// command line parameter

            FileInputStream fstream = new FileInputStream("prova.txt");
            BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
            String strLine = br.readLine(); // step 1

            if (strLine != null) {
              String[] delims = strLine.split(","); // step 2

              // step 3
              int[] a = new int[Integer.parseInt(delims[0])];
              int[] b = new int[Integer.parseInt(delims[1])];

              // step 4
              for (int i=0; i < a.length; i++)
                a[i] = Integer.parseInt(br.readLine());

              // step 5
              for (int i=0; i < b.length; i++)
                b[i] = Integer.parseInt(br.readLine());

              br.close(); // step 6

              // step 7
              System.out.println(Arrays.toString(a));
              System.out.println(Arrays.toString(b));
            }
        }
      }

但它给了我错误:-未处理的异常类型 FileNotFoundException(第 11 行)-未处理的异常类型 IOException(第 15 26 30 32 行)但我不知道为什么。有人可以帮助我。提前致谢

4

5 回答 5

12

更改 main 方法的抛出方式IOException。由于这些操作可能会导致要么FileNotFoundException要么IOException

    public static void main(String[] args) throws FileNotFoundException {

    }

或者添加一个try-catch

   try {
        FileInputStream fstream = new FileInputStream("prova.txt");
        String strLine = br.readLine();
    } catch (IOException e) {
        e.printStackTrace(); 
        // handle exception correctly.
    }

毕竟这些事情确保该文件存在。

于 2013-08-18T09:39:51.260 回答
0

您所要做的就是为未处理的异常添加 try-catch 块。发生这种情况是因为FileInputStreamthrowsFileNotFoundException这是一个已检查的异常,您可以在此处阅读更多信息)

同样的问题发生在这里

String strLine = br.readLine()
于 2013-08-18T09:37:52.600 回答
0

谢谢你,Ruchira,我做了这个

import java.io.*;
import java.util.Arrays;

public class Progetto  {

    public static void main(String args[]) throws IOException {
    }
      {
          try {
// Open the file that is the first 
// command line parameter

            FileInputStream fstream = new FileInputStream("prova.txt");
            BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
            String strLine = br.readLine(); // step 1


            if (strLine != null) {
              String[] delims = strLine.split(","); // step 2

              // step 3
              int[] a = new int[Integer.parseInt(delims[0])];
              int[] b = new int[Integer.parseInt(delims[1])];

              // step 4
              for (int i=0; i < a.length; i++)
                a[i] = Integer.parseInt(br.readLine());

              // step 5
              for (int i=0; i < b.length; i++)
                b[i] = Integer.parseInt(br.readLine());

              br.close(); }// step 6
          }catch (Exception e){//Catch exception if any
                System.err.println("Error: " + e.getMessage());
                }
              // step 7
              System.out.println(Arrays.toString(a));
              System.out.println(Arrays.toString(b));
            }

        }

但现在最后我有这个错误:

a 无法解析为变量

和 b 一样。但是我import java.util.Arrays;

于 2013-08-18T09:54:07.920 回答
0
void addRule(String content){
    FileOutputStream outStream = null;
    try {
        outStream = this.openFileOutput("BlockList", Context.MODE_APPEND);
        outStream.write(("\n"+content).getBytes());
        outStream.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }catch (IOException e) {
        e.printStackTrace();
    }
}
于 2017-08-31T03:35:40.987 回答
-1

throws IOException在main方法的签名后添加

public static void main(String[] args) throws IOException

对于您的下一个问题:

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;

public class Progetto {

    public static void main(String args[]) throws IOException {
        int a[] = null;
        int b[] = null;
        try {
            // Open the file that is the first
            // command line parameter

            FileInputStream fstream = new FileInputStream("prova.txt");
            BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
            String strLine = br.readLine(); // step 1

            if (strLine != null) {
                String[] delims = strLine.split(","); // step 2

                // step 3
                a = new int[Integer.parseInt(delims[0])];
                b = new int[Integer.parseInt(delims[1])];

                // step 4
                for (int i = 0; i < a.length; i++)
                    a[i] = Integer.parseInt(br.readLine());

                // step 5
                for (int i = 0; i < b.length; i++)
                    b[i] = Integer.parseInt(br.readLine());

                br.close();
            }// step 6
        } catch (Exception e) {// Catch exception if any
            System.err.println("Error: " + e.getMessage());
        }
        // step 7
        System.out.println(Arrays.toString(a));
        System.out.println(Arrays.toString(b));
    }

}
于 2013-08-18T09:53:32.400 回答