1

所以我试图基本上将一些日志写入文本文件,以便稍后查看。我在实体手机上运行它,而不是模拟器。我尝试了很多不同的变体,我得到的最多的是它写入数据/数据和存储/模拟,但我永远无法访问我的文件。任何帮助,将不胜感激。我最新的一些未删除的例子是:

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();
}

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);

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();
}

以下两者都将其放在 /storage/emulated/0/Android/data/com.example.simplelte/files/system/stuff/test.txt 和 /storage/emulated/0/Android/data/com.example.simplelte /files/storage/emulated/0/stuff/test.txt 分别

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();
}

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();
}

以及更多。我已经遵循了我能想到的每一个例子。我真的只想查看 Computer\Nexus 5\Internal storage\ 下的文本文件,我只是一个有着简单欲望的简单人。为什么这必须如此复杂。

4

1 回答 1

0

你试过这个吗?

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

FileOutputStream f = new FileOutputStream(file);
于 2014-09-11T23:58:09.567 回答