未处理的异常:FileNotFoundException

时间:2013-08-18 09:36:12

标签: java bufferedreader

我在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行) 但我不知道为什么。有人可以帮助我。 提前致谢

5 个答案:

答案 0 :(得分:6)

更改主方法抛出IOException的方式。由于这些操作可能会导致FileNotFoundExceptionIOException

    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(); 
    }

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

答案 1 :(得分:0)

您所要做的就是为未处理的异常添加try-catch块。 发生这种情况是因为FileInputStream抛出FileNotFoundException这是一个经过检查的例外您可以read更多信息)

此处发生同样的问题

String strLine = br.readLine()

答案 2 :(得分: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;

答案 3 :(得分: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();
    }
}

答案 4 :(得分:-1)

请在主要方法签名后添加throws IOException

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));
    }

}