java - 将文本文件写入 Android 存储以供稍后查看

标签 java android storage adt nexus-5

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

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\下的文本文件,我只是一个有着简单愿望的简单人。为什么这必须如此复杂。

最佳答案

你试过这个吗?

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

FileOutputStream f = new FileOutputStream(file);

关于java - 将文本文件写入 Android 存储以供稍后查看,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25798565/

相关文章:

java - 使用 Java JAI 缩放图像

java - 您可以在 if 子句中存储变量吗?

android - 无法从 Activity 中调用 getSupportFragmentManager()

java - 保存类对象而不进行序列化

android - 可以获得总存储容量吗?

sql-server - 估计生产 SQL Server 系统的 IOPS 要求

java - 一个 protected 变量是可访问的,而另一个则不可访问。为什么?

android - 在没有 NDK 的情况下开发 native C 应用程序 android?

android - 有什么方法可以检查 GcmNetworkManager 是否已经安排了定期任务

Java 小程序 + MySQL