6

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

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

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

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
user1841492
  • 161
  • 2
  • 2
  • 11

5 Answers5

14

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 {

    }

Or add a try-catch block

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

After all these thing make sure that file is exist.

Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115
  • I got an error because it was expecting my function to return something and I fixed it by replacing `e.printStackTrace();` with `throw new RuntimeException(e);`. (I also had to `import java.lang.RuntimeException`) – Henry Dec 03 '21 at 13:04
0

All you have to do is add try-catch blocks for the unhandled exceptions. This happens because FileInputStream throws FileNotFoundException which is a checked exception You can read here more)

The same issue happens here with

String strLine = br.readLine()
Community
  • 1
  • 1
BobTheBuilder
  • 18,858
  • 6
  • 40
  • 61
0

Please add throws IOException after main method's signature

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

}
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
Sunny Gupta
  • 6,929
  • 15
  • 52
  • 80
  • 1. Always catch the most specific error possible. 2. In the code you gave, there's no reason to both catch Exception and throw IOException. 3. If you're going to give code as an example of what to do, it should also be closing the streams properly. – Eric Stein Aug 18 '13 at 12:08
  • Please don't use DataInputStream for text. – Peter Lawrey Sep 25 '13 at 14:26
0

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

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

Jobin
  • 5,610
  • 5
  • 38
  • 53
user1841492
  • 161
  • 2
  • 2
  • 11
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();
    }
}
sonichy
  • 1,370
  • 2
  • 14
  • 17