0

My problem is in the following code.

The problem is that when I call the alreadyUser(String username) if the file doesn't exist on the system already, it gives the FileNotFoundException. I want to get over this error and I can not figure it out.

So at startup of the app the system asks for uname and pass. Then the alreadyUser method is called and it gives the error if the file is not already created hardly (I create it manually for example). And the next time I start the program if the file is already there it must not be switched with new, because the old data will be gone ofc :)

public final class TinyBase {

    final static String FILENAME = "KEYVALUES.txt";
    static FileOutputStream fos = null;
    static FileInputStream fis = null;

    protected static void createUser(String username, String password)

    protected static boolean loadUser(String username, String password)

    protected static boolean alreadyUser(String username) {
        String encode = new String(username);
        String compare = null;
        boolean flag = false; // true - ok no such user ; false - fail username
                                // already in use
        try {
            /* ERROR IS HERE */
            fis = new FileInputStream(FILENAME);
            /* ERROR IS HERE */
            byte[] buffer = new byte[fis.available()];

            while (fis.read(buffer) != -1) {
                compare = new String(buffer);
                if (compare.contains(encode)) {
                    flag = true;
                    break;
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                fis.close();
                return flag;
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return false;
    }
}
HansUp
  • 95,961
  • 11
  • 77
  • 135
Stuci
  • 571
  • 1
  • 5
  • 13

2 Answers2

0

Check if the file exists using -

File f = new File(filePathString);
if(f.exists()) { /* do something */ }
ajc
  • 1,685
  • 14
  • 34
  • I have tried to make this, but i assume that there is a problem. Because i code it and nothing happens i mean it does not create any file at the path. – Stuci Oct 30 '13 at 09:57
0

I guess, what you need according to your snapshot, is to handle FileNotFoundCase properly :

// ....
} catch (FileNotFoundException e) {
        Log.d("no settings for the user - assuming new user");
        flag = true;
}

BTW, you need to fix your finally block, in case of prior exception your fis may be null, so to avoid NullPointerException you may need an extra check :

if (fis != null) {
    fis.close();
}

UPDATE

Here is sketch of what you may need based on my understanding of your problem :

    // ... somewhere at startup after username/pass are given

    // check if file exists
    if (new File(FILENAME).isFile() == false) { // does not exist
        fos = new FileOutputStream(FILENAME); // will create 
                                              // file for you (not subfolders!)
        // write content to your file
        fos.close();
    }

    // by this point file exists for sure and filled with correct user data
    alreadyUser(userName);
kiruwka
  • 9,250
  • 4
  • 30
  • 41
  • Thank you for the fix in fis, but i tried: `code` fos = new FileOutputStream(FILENAME, true); if (fos != null) { fos.close(); alreadyUser(username); } And does not help me i can not understand why.... – Stuci Oct 30 '13 at 10:09
  • @user If you want to execute `alreadyUser(userName)` only if FILENAME exists, then you need to use the code posted by ajc. (it will not create file for you, it will only return true if file exists : `if (f.exists()) { alreadyUser(userName); }` Otherwise, update your question and state what exactly you want to do. – kiruwka Oct 30 '13 at 10:29