1

I am getting errorjava.io.FileNotFoundException: removed/contact/contact.txt: open failed: ENOENT (No such file or directory) whenever i try to create the file. can some one let me know if i am giving the path for the file correct. Also i have giving the required permission in the manifest too

MainActivity

public class MainActivity extends Activity {
    List<String> phone = new ArrayList<String>();
    Context context =this;
    List<String> firstname = new ArrayList<String>();
    List<String> lastname= new ArrayList<String>();
    int data_block =100;
    List<contact> rowItems;
    ListView mylistview;
    public String path = Environment.getExternalStorageState()+"/contact";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        File f = new File(path);
        f.mkdirs();


        mylistview=(ListView) findViewById(R.id.contactList);
        ContactAdpater adapter =new ContactAdpater (this,getContact());
        mylistview.setAdapter(adapter);


    }
    private ArrayList<contact> getContact(){
        try {
            File fi= new File(path+"/contact.txt");
            Scanner s = null;
            s  = new Scanner(fi);
            while(s. hasNextLine()){
                System.out.println(s.nextLine());
                String firstName = s.next();
                String lastName = s.next();
                String phone = s.next();
                Toast.makeText(getBaseContext(),"Message2:"+firstName,Toast.LENGTH_SHORT).show();

            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }


        ArrayList<contact> alarms= new ArrayList<contact>();

        for (int i =0; i<firstname.size();i++){
            alarms.add(new contact (firstname.get(i),lastname.get(i),phone.get(i)));
        }
        return alarms;
    }
    @Override
    protected void onResume (){
        //run this method on the finish of second activity and update the database
        super.onResume();


        mylistview=(ListView) findViewById(R.id.contactList);
        ContactAdpater adapter =new ContactAdpater (this,getContact());
        mylistview.setAdapter(adapter);

    }
    public void add(View v){
        Intent getNameIntent = new Intent(MainActivity.this, AddContact.class);
        startActivity(getNameIntent);

    }
    public void remove(View v){
        Intent getNameIntent = new Intent(MainActivity.this, DeleteContact.class);
        startActivity(getNameIntent);
    }





}

AddContact

    public class AddContact extends Activity {
    EditText textFirstName,textLastName,textPhone;
    public String path = Environment.getExternalStorageState()+"/contact";
    String first,last,phone;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_contact);
        textFirstName = (EditText) findViewById(R.id.editText);
        textLastName = (EditText) findViewById(R.id.editText2);
        textPhone = (EditText) findViewById(R.id.editText3);
    }

    public void save(View v){

        first= textFirstName.getText().toString();
        last= textLastName.getText().toString();
        phone= textPhone.getText().toString();
        try{

            File fou= new File(path+"/contact.txt");
            FileWriter fWriter = new FileWriter (fou);
            PrintWriter pWriter = new PrintWriter (fWriter);
            pWriter.println(first+" "+last+" "+phone);
            pWriter.close();



        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        finish();
    }



    public void cancel (View v){
        finish();

    }
}

logcast

    12-07 11:48:45.627 17718-17744/com.example.lab6 W/EGL_emulation: eglSurfaceAttrib not implemented
12-07 11:48:45.627 17718-17744/com.example.lab6 W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xabdbe840, error=EGL_SUCCESS
12-07 11:48:45.705 17718-17744/com.example.lab6 E/Surface: getSlotFromBufferLocked: unknown buffer: 0xab7548b0
12-07 11:48:45.708 17718-17744/com.example.lab6 D/OpenGLRenderer: endAllStagingAnimators on 0xab66e180 (RippleDrawable) with handle 0xabdbf760
12-07 11:48:59.957 17718-17718/com.example.lab6 W/System.err: java.io.FileNotFoundException: removed/contact/contact.txt: open failed: ENOENT (No such file or directory)
12-07 11:48:59.957 17718-17718/com.example.lab6 W/System.err:     at libcore.io.IoBridge.open(IoBridge.java:452)
12-07 11:48:59.957 17718-17718/com.example.lab6 W/System.err:     at java.io.FileOutputStream.<init>(FileOutputStream.java:87)
12-07 11:48:59.957 17718-17718/com.example.lab6 W/System.err:     at java.io.FileOutputStream.<init>(FileOutputStream.java:72)
12-07 11:48:59.957 17718-17718/com.example.lab6 W/System.err:     at java.io.FileWriter.<init>(FileWriter.java:42)
12-07 11:48:59.957 17718-17718/com.example.lab6 W/System.err:     at com.example.lab6.AddContact.save(AddContact.java:41)
12-07 11:48:59.957 17718-17718/com.example.lab6 W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
12-07 11:48:59.957 17718-17718/com.example.lab6 W/System.err:     at android.view.View$DeclaredOnClickListener.onClick(View.java:4447)
12-07 11:48:59.957 17718-17718/com.example.lab6 W/System.err:     at android.view.View.performClick(View.java:5198)
12-07 11:48:59.957 17718-17718/com.example.lab6 W/System.err:     at android.view.View$PerformClick.run(View.java:21147)
12-07 11:48:59.957 17718-17718/com.example.lab6 W/System.err:     at android.os.Handler.handleCallback(Handler.java:739)
12-07 11:48:59.957 17718-17718/com.example.lab6 W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:95)
12-07 11:48:59.957 17718-17718/com.example.lab6 W/System.err:     at android.os.Looper.loop(Looper.java:148)
12-07 11:48:59.957 17718-17718/com.example.lab6 W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5417)
12-07 11:48:59.957 17718-17718/com.example.lab6 W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
12-07 11:48:59.957 17718-17718/com.example.lab6 W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
12-07 11:48:59.957 17718-17718/com.example.lab6 W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
12-07 11:48:59.957 17718-17718/com.example.lab6 W/System.err: Caused by: android.system.ErrnoException: open failed: ENOENT (No such file or directory)
12-07 11:48:59.957 17718-17718/com.example.lab6 W/System.err:     at libcore.io.Posix.open(Native Method)
12-07 11:48:59.958 17718-17718/com.example.lab6 W/System.err:     at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
12-07 11:48:59.958 17718-17718/com.example.lab6 W/System.err:     at libcore.io.IoBridge.open(IoBridge.java:438)
12-07 11:48:59.958 17718-17718/com.example.lab6 W/System.err:   ... 15 more
12-07 11:48:59.979 17718-17718/com.example.lab6 W/System.err: java.io.FileNotFoundException: removed/contact/contact.txt: open failed: ENOENT (No such file or directory)
12-07 11:48:59.979 17718-17718/com.example.lab6 W/System.err:     at libcore.io.IoBridge.open(IoBridge.java:452)
12-07 11:48:59.979 17718-17718/com.example.lab6 W/System.err:     at java.io.FileInputStream.<init>(FileInputStream.java:76)
12-07 11:48:59.979 17718-17718/com.example.lab6 W/System.err:     at java.util.Scanner.<init>(Scanner.java:158)
12-07 11:48:59.979 17718-17718/com.example.lab6 W/System.err:     at java.util.Scanner.<init>(Scanner.java:138)
12-07 11:48:59.979 17718-17718/com.example.lab6 W/System.err:     at com.example.lab6.MainActivity.getContact(MainActivity.java:52)
12-07 11:48:59.979 17718-17718/com.example.lab6 W/System.err:     at com.example.lab6.MainActivity.onResume(MainActivity.java:80)
12-07 11:48:59.979 17718-17718/com.example.lab6 W/System.err:     at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1258)
12-07 11:48:59.979 17718-17718/com.example.lab6 W/System.err:     at android.app.Activity.performResume(Activity.java:6312)
12-07 11:48:59.980 17718-17718/com.example.lab6 W/System.err:     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3092)
12-07 11:48:59.980 17718-17718/com.example.lab6 W/System.err:     at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3134)
12-07 11:48:59.980 17718-17718/com.example.lab6 W/System.err:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1388)
12-07 11:48:59.980 17718-17718/com.example.lab6 W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:102)
12-07 11:48:59.980 17718-17718/com.example.lab6 W/System.err:     at android.os.Looper.loop(Looper.java:148)
12-07 11:48:59.980 17718-17718/com.example.lab6 W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5417)
12-07 11:48:59.980 17718-17718/com.example.lab6 W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
12-07 11:48:59.980 17718-17718/com.example.lab6 W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
12-07 11:48:59.980 17718-17718/com.example.lab6 W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
12-07 11:48:59.980 17718-17718/com.example.lab6 W/System.err: Caused by: android.system.ErrnoException: open failed: ENOENT (No such file or directory)
12-07 11:48:59.980 17718-17718/com.example.lab6 W/System.err:     at libcore.io.Posix.open(Native Method)
12-07 11:48:59.980 17718-17718/com.example.lab6 W/System.err:     at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
12-07 11:48:59.980 17718-17718/com.example.lab6 W/System.err:     at libcore.io.IoBridge.open(IoBridge.java:438)
12-07 11:48:59.980 17718-17718/com.example.lab6 W/System.err:   ... 16 more
12-07 11:49:00.009 17718-17744/com.example.lab6 W/EGL_emulation: eglSurfaceAttrib not implemented
12-07 11:49:00.009 17718-17744/com.example.lab6 W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xad720560, error=EGL_SUCCESS
12-07 11:49:00.098 17718-17744/com.example.lab6 E/Surface: getSlotFromBufferLocked: unknown buffer: 0xab7582e0
12-07 11:49:00.101 17718-17744/com.example.lab6 D/OpenGLRenderer: endAllStagingAnimators on 0xaf7fb780 (RippleDrawable) with handle 0xad71a400

manifest

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.lab6">
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".AddContact" />
        <activity android:name=".DeleteContact"></activity>
    </application>

</manifest>
Android2841
  • 55
  • 10

2 Answers2

0

Just change

public String path = Environment.getExternalStorageState()+"/contact";

to

public String path = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "/contact";

From the Documentation

android.os.Environment public static java.lang.String getExternalStorageState() Returns the current state of the primary "external" storage device. Returns: one of MEDIA_UNKNOWN, MEDIA_REMOVED, MEDIA_UNMOUNTED, MEDIA_CHECKING, MEDIA_NOFS, MEDIA_MOUNTED, MEDIA_MOUNTED_READ_ONLY, MEDIA_SHARED, MEDIA_BAD_REMOVAL, or MEDIA_UNMOUNTABLE.

As you can see from

java.io.FileNotFoundException: removed/contact/contact.txt

your path is removed/contact/contact.txt because Environment.getExternalStorageState() returns removed.

** EDIT **

Also you have to make this changes on your getContact() and save() methods

getContact:

    private ArrayList<contact> getContact(){
        try {
            File fi= new File(path+"/contact.txt");
            Scanner s = null;
            s  = new Scanner(fi);
            while(s.hasNext()){
                String firstName = s.next();
                String lastName = s.next();
                String phone = s.next();
            Toast.makeText(getBaseContext(),"Message2:"+firstName,Toast.LENGTH_SHORT).show();

            }
            catch (FileNotFoundException e) {
            e.printStackTrace();
          }


          ArrayList<contact> alarms= new ArrayList<contact>();

          for (int i =0; i<firstname.size();i++){
              alarms.add(new contact (firstname.get(i),lastname.get(i),phone.get(i)));
          }
          return alarms;
     }

As you show I delete the System.out.println(s.nextLine()); beacuse it's eating all the line and the

String firstName = s.next();
String lastName = s.next();
String phone = s.next();

or throw a NoSuchElementException. Also I change the while(s.hasNextLine()) with while(s.hasNext()) beacuse it's throwing a NoSuchElementException too.

save:

  public void save(View v){

        first= textFirstName.getText().toString();
        last= textLastName.getText().toString();
        phone= textPhone.getText().toString();
        try{

            File fou = new File(path + File.separator +"contact.txt");
            if (!fou.exists()) {
                fou.createNewFile();
            }
            FileWriter fWriter = new FileWriter (fou);
            PrintWriter pWriter = new PrintWriter (fWriter);
            pWriter.println(first+" "+last+" "+phone);
            pWriter.close();



        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        finish();
    }

Here the only change I made is

   if (!fou.exists()) {
        fou.createNewFile();
    }

to create the file if doesn't exists.

Vasileios Pallas
  • 4,801
  • 4
  • 33
  • 50
0

I think that was an really similar issue on StackOverflow. Please check this out: How to create text file and insert data to that file on Android.

You're missing an important part of code which would tell an Android system where to save a file. You'd chosen wrong method, because state removed given by Environment.getExternalStorageState() means only that Android couldn't find your file and that method wouldn't never give you a path to file.

Community
  • 1
  • 1
piotrek1543
  • 19,130
  • 7
  • 81
  • 94