Jump to content

file exist error


funbinod

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
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...