Java 未处理的异常:FileNotFoundException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18297732/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 00:14:44  来源:igfitidea点击:

Unhandled exception: FileNotFoundException

javabufferedreader

提问by user1841492

I have some problems reading file in java: my file is for example:

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

3,4
2
6
4
1
7
3
8
9

where first line 3 and 4 are the lenght of array A and B and then the element of each array. I made this

其中第一行 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));
            }
        }
      }

But it gives me error: -Unhandled exception type FileNotFoundException (line 11) -Unhandled exception type IOException (lines 15 26 30 32) but i don't know why. Someone can help me. Thanks in advance

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

回答by BobTheBuilder

All you have to do is add try-catch blocks for the unhandled exceptions. This happens because FileInputStreamthrows FileNotFoundExceptionwhich is a checked exception You can readhere more)

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

The same issue happens here with

同样的问题在这里发生

String strLine = br.readLine()

回答by Ruchira Gayan Ranaweera

Change the way your main method throws IOException. Since these operations may cause either FileNotFoundExceptionor IOException.

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

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

    }

Or add a try-catchblock

或者添加一个try-catch

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

After all these thing make sure that file is exist.

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

回答by Sunny Gupta

Please add throws IOExceptionafter main method's signature

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

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

For your Next Question :

对于您的下一个问题:

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

}

回答by user1841492

Thank you Ruchira, i made this

谢谢鲁奇拉,我做了这个

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

        }

But now at the end i have this error:

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

a cannot be resolved to a variable

a 不能解析为变量

and the same for b. But i import java.util.Arrays;

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

回答by sonichy

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