1

In the first method I created a file in the applicatin directory, and the I try to read it with a BufferedReader, but for some reason, the app keeps throwing the FileNotFound exception, and I have no idea why.

Writer output = null;
    File file = new File(getApplicationContext().getFilesDir(), "past");
    output = new BufferedWriter(new FileWriter(file, true));
    output.write(format_1 + "Overall positivity was " + sprog);
    output.close();

and here's the reader code

public void past(View v) throws IOException {
    try(BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("past"),StandardCharsets.UTF_8))){

        String line;

        while ((line=br.readLine()) != null){
            System.out.println(line);
        }
    }
}

this is what the debug throws

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.mojefajne.test, PID: 15167
java.lang.IllegalStateException: Could not execute method for android:onClick
    at android.view.View$DeclaredOnClickListener.onClick(View.java:5634)
    at android.view.View.performClick(View.java:6597)
    at android.view.View.performClickInternal(View.java:6574)
    at android.view.View.access$3100(View.java:778)
    at android.view.View$PerformClick.run(View.java:25885)
    at android.os.Handler.handleCallback(Handler.java:873)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:193)
    at android.app.ActivityThread.main(ActivityThread.java:6669)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
 Caused by: java.lang.reflect.InvocationTargetException
    at java.lang.reflect.Method.invoke(Native Method)
    at android.view.View$DeclaredOnClickListener.onClick(View.java:5629)
    at android.view.View.performClick(View.java:6597) 
    at android.view.View.performClickInternal(View.java:6574) 
    at android.view.View.access$3100(View.java:778) 
    at android.view.View$PerformClick.run(View.java:25885) 
    at android.os.Handler.handleCallback(Handler.java:873) 
    at android.os.Handler.dispatchMessage(Handler.java:99) 
    at android.os.Looper.loop(Looper.java:193) 
    at android.app.ActivityThread.main(ActivityThread.java:6669) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) 
 Caused by: java.io.FileNotFoundException: past (No such file or directory)
    at java.io.FileInputStream.open0(Native Method)
    at java.io.FileInputStream.open(FileInputStream.java:231)
    at java.io.FileInputStream.<init>(FileInputStream.java:165)
    at java.io.FileInputStream.<init>(FileInputStream.java:112)
    at com.example.mojefajne.test.MainActivity.past(MainActivity.java:179)
    at java.lang.reflect.Method.invoke(Native Method) 
    at android.view.View$DeclaredOnClickListener.onClick(View.java:5629) 
    at android.view.View.performClick(View.java:6597) 
    at android.view.View.performClickInternal(View.java:6574) 
    at android.view.View.access$3100(View.java:778) 
    at android.view.View$PerformClick.run(View.java:25885) 
    at android.os.Handler.handleCallback(Handler.java:873) 
    at android.os.Handler.dispatchMessage(Handler.java:99) 
    at android.os.Looper.loop(Looper.java:193) 
    at android.app.ActivityThread.main(ActivityThread.java:6669) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) 
  • 1
    You may want to try `new FileInputStream(file)` or `new FileInputStream(new File(getApplicationContext().getFilesDir(), "past"))` instead of just `new FileInputStream("past")` . – Arnaud Dec 26 '18 at 14:29
  • 1
    That actually worked :0 Thank you so much. – Kamil Barczak Dec 26 '18 at 14:53
  • This question will also give you an explanation about the default path of a new file : https://stackoverflow.com/questions/5154529/in-java-what-is-the-default-location-for-newly-created-files – Arnaud Dec 26 '18 at 15:00

0 Answers0