【问题标题】:Writing a text file to android storage for viewing later将文本文件写入 android 存储以供稍后查看
【发布时间】:2014-09-11 23:38:39
【问题描述】:

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

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

【问题讨论】:

    标签: java android storage adt nexus-5


    【解决方案1】:

    你试过了吗?

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

    【讨论】:

    • 刚刚试了一下。无法在资源管理器中查看文件。创建 /storage/emulated/0/text.txt
    猜你喜欢
    • 2012-06-26
    • 1970-01-01
    • 2020-11-03
    • 1970-01-01
    • 2016-10-14
    • 1970-01-01
    • 1970-01-01
    • 2014-04-30
    • 2015-04-18
    相关资源
    最近更新 更多