未处理的异常:FileNotFoundException [英] Unhandled exception: FileNotFoundException

查看:177
本文介绍了未处理的异常:FileNotFoundException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

3,4
2
6
4
1
7
3
8
9

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

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

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

但它给了我错误:
-Unhandled exception type FileNotFoundException(第11行)
-Unhandled exception type IOException(lines 15 26 30 32)
但我不知道为什么。有人可以帮助我。
提前致谢

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

推荐答案

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

Change the way your main method throws IOException. Since these operations may cause either FileNotFoundException or 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(); 
    }

在所有这些之后确保文件存在。

After all these thing make sure that file is exist.

这篇关于未处理的异常:FileNotFoundException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆