-1

I have already created a file and I want to open it. After creating the file with some common opening text, I call the method, writeValuestoFile(String fname) where fname is the File Name. In the method, I would like to open the file I already created through another method and just update it. But, I am InvalidArgumentException. I am doubting if my path I am specifying is correct or not. Input will be highly appreciated. This is my method call.

public void writeValuestoFile(String fname)  {
String rec_x_string = Float.valueOf((x_updated)).toString();
        String rec_y_string = Float.valueOf((y_updated)).toString();
        String rec_z_string = Float.valueOf((z_updated)).toString();
        try {
            FileOutputStream fout = openFileOutput("sdcard/Accelorometer Readings/"+file_name, Context.MODE_APPEND);
            OutputStreamWriter write = new OutputStreamWriter(fout);
            String newline = System.getProperty("line.seperator");
            try {
                write.append(newline);
                write.append(newline);
                write.write(rec_x_string + "        " + rec_y_string + "        " + rec_z_string);
                write.flush();
                write.close();
                fout.close();

            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }



        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return;

    }

This is my Logcat for the exception:

02-08 05:57:49.869: E/AndroidRuntime(24818): FATAL EXCEPTION: main
02-08 05:57:49.869: E/AndroidRuntime(24818): java.lang.IllegalArgumentException: File sdcard/Accelorometer Readings/null contains a path separator
02-08 05:57:49.869: E/AndroidRuntime(24818):    at android.app.ContextImpl.makeFilename(ContextImpl.java:1674)
02-08 05:57:49.869: E/AndroidRuntime(24818):    at android.app.ContextImpl.openFileOutput(ContextImpl.java:420)
02-08 05:57:49.869: E/AndroidRuntime(24818):    at android.content.ContextWrapper.openFileOutput(ContextWrapper.java:158)
02-08 05:57:49.869: E/AndroidRuntime(24818):    at com.example.hw02_devarajan_q1.MainActivity.writeValuestoFile(MainActivity.java:241)
02-08 05:57:49.869: E/AndroidRuntime(24818):    at com.example.hw02_devarajan_q1.MainActivity$2.run(MainActivity.java:91)
02-08 05:57:49.869: E/AndroidRuntime(24818):    at android.os.Handler.handleCallback(Handler.java:587)
02-08 05:57:49.869: E/AndroidRuntime(24818):    at android.os.Handler.dispatchMessage(Handler.java:92)
02-08 05:57:49.869: E/AndroidRuntime(24818):    at android.os.Looper.loop(Looper.java:130)
02-08 05:57:49.869: E/AndroidRuntime(24818):    at android.app.ActivityThread.main(ActivityThread.java:3687)
02-08 05:57:49.869: E/AndroidRuntime(24818):    at java.lang.reflect.Method.invokeNative(Native Method)
02-08 05:57:49.869: E/AndroidRuntime(24818):    at java.lang.reflect.Method.invoke(Method.java:507)
02-08 05:57:49.869: E/AndroidRuntime(24818):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:878)
02-08 05:57:49.869: E/AndroidRuntime(24818):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:636)
02-08 05:57:49.869: E/AndroidRuntime(24818):    at dalvik.system.NativeStart.main(Native Method)

1 Answers1

0

Do not use hardcoded sdcard/ - this is WRONG approach for long time not to mention it may simply not work on all devices. You should be using Environment class' methods like getExternalStorageDirectory() to get the right directory as documented here. You should read this chapter of documentation to see how this should be done the right way. Do not forget about proper permissions as well.

Also: fname argument is not used in your writeValuestoFile() function at all.

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
  • Thanks for pointing it out. I know how to use it to create a new file. But how do you use it to open an existing file. I am having the filename passed as input to the method. – Shrinivas Dev Feb 08 '15 at 00:44
  • I tried this but still the same problem openFileOutput(Environment.getExternalStorageDirectory()+"/Accelorometer Readings/"+fname – Shrinivas Dev Feb 08 '15 at 00:47
  • you are not using function argument at all – Marcin Orlowski Feb 08 '15 at 09:00
  • I don't get you. Can you explain what you are trying to say? I am new to I/O in Java, so I am sorry if I am asking something elementary. – Shrinivas Dev Feb 08 '15 at 14:10
  • Okay, I understand that the openFileOutput cannot have path as an input. But if I cannot use it, can anyone tell me how to open an existing file provided I have the filename with me? – Shrinivas Dev Feb 08 '15 at 15:31