Jump to content
funbinod

file exist error

Recommended Posts

hello all!

i'm new to java and android studio.

i was trying to check and create new file and do something in it.

                    if (!file1.exists()) {
                        try {
                            file1.createNewFile();
                            Toast.makeText(StartActivity.this, "key file created!" + directory, Toast.LENGTH_LONG).show();
                            Intent intent = new Intent(StartActivity.this, KeyActivity.class);
                            startActivity(intent);
                            StartActivity.this.finish();
                        } catch (IOException e) {
                            e.printStackTrace();
                            Toast.makeText(StartActivity.this, "key file creation failed!" + directory + "--" + e, Toast.LENGTH_LONG).show();
                        }
                    } else {
                        StringBuilder keyData = new StringBuilder("");
                        try {
                            FileInputStream kfile = openFileInput(keyfile);
                            InputStreamReader kSr = new InputStreamReader(kfile);
                            BufferedReader bufferedReader1 = new BufferedReader((kSr));
                            String readKey = bufferedReader1.readLine();
                            if (readKey != null) {
                                keyData.append(readKey);
                                final String key = keyData.toString();
                                Toast.makeText(StartActivity.this, "found login id and key on " + directory + "! Checking the credentials!", Toast.LENGTH_LONG).show();
                                new AsyncLogin1().execute(loginId, key);
                            } else {
                                Intent intent = new Intent(StartActivity.this, KeyActivity.class);
                                intent.putExtra("loginID", loginId);
                                startActivity(intent);
                                StartActivity.this.finish();
                            }
                            kSr.close();
                        } catch (IOException ioe1) {
                            ioe1.printStackTrace();
                            Toast.makeText(StartActivity.this, "error: " + ioe1, Toast.LENGTH_LONG).show();  // this is the part that is displayed as error
                        }

i tried all these and got unexpected error!

while running this, it goes inside the file existence block and returns the IOException error saying "No such file or directory". Please someone guide me where i made mistake.

EDIT:

this is the file path:

File thisdirectory = this.getFilesDir();
File directory = new File(thisdirectory + "/online/is_cool_staffs/");
if (!directory.exists()) {
  directory.mkdirs();
}
File file1 = new File(directory + keyfile);

 

thank you in advance...

Edited by funbinod

Share this post


Link to post
Share on other sites

If it's saying there file doesn't exist then I would trust it, print out the path to verify.  I don't know if keyfile is what you're opening but I don't see where that variable gets defined.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...