1

So I'm trying to basically write some logs to a text file so i can view it later. I'm running this on a physical phone, not the emulator. I've tried so many different variations, and the most i got was it writing to data/data and storage/emulated but i can never access my file. Any help would be appreciated. Some of my latest undeleted examples have been:

String filename = "myfile.txt";
try {
    File path = new File(context.getFilesDir(), "myfolder");
    if (!path.exists())
        path.mkdir();
    File output = new File(path, filename);
    BufferedWriter buff = new BufferedWriter(new FileWriter(output));
    buff.append("hi");
    Log.d("Success",
            "Successfully wrote a file " + context.getFilesDir());
} catch (Exception e) {
    e.printStackTrace();
}

and

final String appPath = String.format("%s/Datafiles",
        context.getFilesDir());
File path = new File(appPath);
if (!path.exists()) {
    path.mkdir();
}
String fileName = String.format("%s/filedemo.txt", appPath);

and

try {
    String filename = "myfile.txt";
    File path = new File(Environment.getRootDirectory(), "myfolder");
    if (!path.exists())
        path.mkdir();
    File output = new File(path, filename);
    BufferedWriter buff = new BufferedWriter(new FileWriter(output));
    buff.append("hi");
    Log.d("Success",
            "Successfully wrote a file " + context.getFilesDir());
} catch (Exception e) {
    e.printStackTrace();
}

Both of the following put it under /storage/emulated/0/Android/data/com.example.simplelte/files/system/stuff/test.txt and /storage/emulated/0/Android/data/com.example.simplelte/files/storage/emulated/0/stuff/test.txt respectively

try {
    File file = new File(context.getExternalFilesDir(Environment
            .getRootDirectory().getCanonicalPath()), "stuff");
    if (!file.mkdirs()) {
        Log.e(LOG_TAG, "Directory not created");
    }
    File output = new File(file, "test.txt");
    BufferedWriter buff;
    buff = new BufferedWriter(new FileWriter(output));
    buff.append("hi");
    buff.close();
    Log.d("Success", output.getCanonicalPath());
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

and

try {
    File file = new File(context.getExternalFilesDir(Environment
            .getExternalStorageDirectory().getCanonicalPath()),
            "stuff");
    if (!file.mkdirs()) {
        Log.e(LOG_TAG, "Directory not created");
    }
    File output = new File(file, "test.txt");
    BufferedWriter buff;
    buff = new BufferedWriter(new FileWriter(output));
    buff.append("hi");
    buff.close();
    Log.d("Success", output.getCanonicalPath());
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

and much more. I've followed every example I can think of. I literally just want to view the text file under Computer\Nexus 5\Internal storage\ I'm just a simple man with simple desires. Why does this have to be so complicated.

Ion
  • 334
  • 3
  • 15

1 Answers1

0

Have you tried this ?

File sdCard = Environment.getExternalStorageDirectory();
File dir = new File (sdCard.getAbsolutePath() + "/");
File file = new File(dir, "text.txt");

FileOutputStream f = new FileOutputStream(file);
reidisaki
  • 1,525
  • 16
  • 29
  • just tried it. No way to view the file in explorer. Creates /storage/emulated/0/text.txt – Ion Sep 12 '14 at 00:03