1

Due to some requirement I want to save text files files to Android's file system and read it anytime programmatically.

For each user who will be using the same phone there will be a different text file stored. Unfortunately, at that time when the user has not logged in the only unique information about the user I have is the email address (or is there anything else?).

So my question is can I use the email address as the filename for these .txt files such as "xyz_123@email.com.txt" since the email address can have multiple special characters which I'm not sure are allowed in filenames?

Max Worg
  • 2,932
  • 2
  • 20
  • 35
edeesan
  • 326
  • 3
  • 16
  • 2
    There is no problem. Just save using email but dont miss the .txt at the end! – Xenolion Oct 25 '17 at 03:03
  • 1
    I don't think it will work in all cases. There are characters allowed in email addresses that are not allowed in file names. You might be better computing a hash from the email and storing that as the file name. – jwitt98 Oct 25 '17 at 03:14
  • 1
    You should be ok (see https://stackoverflow.com/questions/2679699/what-characters-allowed-in-file-names-on-android) – Stefan Oct 25 '17 at 03:31

1 Answers1

0

Try this .

1.create

String email = "xyz_123@email.com";
File file = new File(email + ".txt");
if (!file.exists()) {
    try {
        file.createNewFile();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Log.e("FILE_NAME", file.getName());

2.Add permission

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

3.And you could use https://github.com/permissions-dispatcher/PermissionsDispatcher to request permission .

KeLiuyue
  • 8,149
  • 4
  • 25
  • 42