0

I'm calling java code through a channel in flutter. It works, but when I try to read I file it does not find the file and throws me an exception. I've tried with all kind of routes but it simply does not find the file, here's my file structure: enter image description here

enter image description here

And the code that I'm using for fetching the file is:

public class MainActivity extends FlutterActivity {
    private static final String CHANNEL = "prueba/cien";
    @Override
    public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {
        GeneratedPluginRegistrant.registerWith(flutterEngine);
        new MethodChannel(flutterEngine.getDartExecutor().getBinaryMessenger(), CHANNEL)
                .setMethodCallHandler(
                        (call, result) -> {
                            if(call.method.equals("main")){
                                try {
                                    ArrayList<String> sintomas = call.argument("sintomas");
                                    ArrayList<String> signos = call.argument("signos");
                                    ArrayList<ArrayList<String>>[] resultado = main(sintomas,signos);
                                    result.success(resultado);
                                } catch (IOException e) {
                                    e.printStackTrace();
                                }
                            }
                        }
                );
    }
    private ArrayList<ArrayList<String>>[] main(ArrayList<String> sintomas,ArrayList<String> signos) throws IOException {
        dataReader dr = new dataReader("texts/enfermedadPaciente.txt");
        dataReader dr2 = new dataReader("texts/enfermedadPaciente.txt");
    }

}

The reader class constructor is:

public class dataReader{

public dataReader(String path){
        this.file = new File(path);
        try{
            this.br = new BufferedReader(new FileReader(file));
        }
        catch (FileNotFoundException ex){
            System.out.println("File not found in "+path);
        }
    }
}
  • The reason in that you are accessing file as it were on file system (in runtime). From the code, file is part of your code actually. You should try to look here for more info: https://stackoverflow.com/questions/676250/different-ways-of-loading-a-file-as-an-inputstream – Alex Radzishevsky Nov 15 '20 at 17:52
  • Hey thanks, Idk if didn't understand the code, but none of these solutions worked for me. Also i tried to print the path where the project is located (when running) but the answer is just a / – nivalderramas Nov 15 '20 at 21:22
  • Allright, yet another option, which is even better for Android case: https://www.tutorialspoint.com/how-to-read-a-simple-text-file-in-android-app – Alex Radzishevsky Nov 16 '20 at 09:06

0 Answers0