javaandroidexceptionioexception

IOException, must be caught or declared to be thrown


This code is from the below link. I tried to run it but got the following error:

Thelink

D:\Android_Dosyalar\Proje\TextToArray\app\src\main\java\chessactivetexttoarray\com\texttoarray\MainActivity.java Error:(40, 21) error: unreported exception IOException; must be caught or declared to be thrown Error:(41, 22) error: unreported exception IOException; must be caught or declared to be thrown Error:(42, 21) error: unreported exception IOException; must be caught or declared to be thrown Error:Execution failed for task ':app:compileDebugJavaWithJavac'.

Compilation failed; see the compiler error output for details.

What possibly is wrong. The following part is underlined in red as well. New to android. Novice level. Thank you all in advance. Regards

            br.close();
            isr.close();
            is.close();

Read and split text file into an array - Android

import android.content.res.AssetManager;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        AssetManager manager;
        String line = null;
        List<String[]> xyzList = new ArrayList<String[]>();
        String[][] xyz;
        InputStream is = null;
        InputStreamReader isr = null;
        BufferedReader br = null;
        try {
            manager = getAssets();
            is = manager.open("C:\\Users\\serhat\\Copy\\satranc\\Akdag_Reportaj\\dan_akdag.pgn");
            isr = new InputStreamReader(is);
            br = new BufferedReader(isr);
               while ((line = br.readLine()) != null) {
                xyzList.add(line.split(" "));
            }
            xyz = (String[][]) xyzList.toArray();
        } catch (IOException e1) {
             Toast.makeText(getBaseContext(), "Problem!",     Toast.LENGTH_SHORT).show();
        } finally {

            br.close();
            isr.close();
            is.close();
        }




    }


}

Solution

  • The close methods can throw an exception. So they need to catch them.

    } finally {
        try {
            br.close();
            isr.close();
            is.close();
        } catch(IOException e) {
            e.printStackTrace();
        }
    }
    

    You also have one other issue in your code that will cause it to fail at run time. You are trying to use the AssetManager to open a file on your windows computer and not the phone. Move the file into your your projects' assets folder and then change

    is = manager.open("C:\\Users\\serhat\\Copy\\satranc\\Akdag_Reportaj\\dan_akdag.pgn");
    

    to

    is = manager.open("dan_akdag.pgn");