Skip to content

Java IOExeption reading file android

I’m trying to get an Opengl es app working and I’m currently working on the shaders. To do that I need to open tow text files situated in src/shaders/ Here is my code:

private static int LoadShader(String filepath, int type){
      File file = new File(filepath);
      StringBuilder codebuilder = new StringBuilder();
        BufferedReader reader = new BufferedReader(new FileReader(file));
        String line;
        while((line = reader.readLine())!=null){
      }catch(IOException e){
        System.err.println("could not read" + filepath);
//And some other code

The inputs are String a = "src/shaders/vertexshader.txt", int GLES20.GL_VERTEX_SHADER and String b = "src/shaders/fragmentshader.txt", int GLES20.GL_FRAGMENT_SHADER.

The app builds correctly, but it Instantly crashes because of the System.exit(-1); (When I remove it it doesn’t crash anymore).

The file path isn’t the problem, I checked it multiple times. The worse is that I don’t have access to any form of backdoor or log communication with my program, because of the only compiler that I can use (java N-IDE for android). Please help, this project is driving me crazy.


I am new to java and programing in general and I come more from a c++ background, so try catch is relatively new to me. So I don’t fully understand the exception handling, which was copied from a tutorial on opengl.

The System.err.println("could not read" + filepath); And e.printStackTrace(); Actually aren’t working for me and I just didn’t bother to remove them.

Also here’s the implementation of all this:

//Constructor of abstract class
//Loadshader is in the same class

public ShaderBase(String vertexfile, String fragmentfile){
      vertexshaderID = LoadShader(vertexfile,GLES20.GL_VERTEX_SHADER);
      fragmentshaderID = LoadShader(fragmentfile,GLES20.GL_FRAGMENT_SHADER);

//And other code
//Implementation of abstract class

    private static final String VERT_FILE = "src/shaders/vertexshader.txt";
    private static final String FRAG_FILE = "src/shaders/fragmentshader.txt";
    public StaticShader(){
//And other code

Also, here’s my file system: My file system

The code is mainly a mismatch of tutorials on opengl (, opengl es ( and some working examples I found online ( So if you have some good tutorial suggestions, please tell me.



I have found the answer: the file system before compiling and after compiling is not the same. By doing MainActivity.getAssets().open("something.txt");, I was able to open files situated in the apps/apps/src/main/assets folder.

User contributions licensed under: CC BY-SA
9 People found this is helpful