Code example for File

Methods: createNewFile, exists

0
    private static final String UDID_PREF_KEY = "pushtalk_udid";
    private static String getSavedUuid() {
        String udid = MyPreferenceManager.getString(UDID_PREF_KEY, null);
        if (null != udid) return udid;
         
        File file = new File(UDID_PATH);
        if (file.exists()) {
            try { 
                ArrayList<String> content = FileUtil.readLines(new FileInputStream(file));
                if (content.size() > 0) {
                    udid = content.get(0);
                    MyPreferenceManager.commitString(UDID_PREF_KEY, udid);
                    Logger.i(TAG, "Got sdcard file saved udid - " + udid);
                    return udid;
                } 
            } catch (FileNotFoundException e) {
            } 
        } 
         
        String name = System.currentTimeMillis() + "";
        udid = UUID.nameUUIDFromBytes(name.getBytes()).toString();
        udid = StringUtils.toMD5(udid);
         
        MyPreferenceManager.commitString(UDID_PREF_KEY, udid);
         
        try { 
            file.createNewFile();
        } catch (IOException e) {
            Logger.e(TAG, "Create file in sdcard error", e);
            return udid;
        }