簡體   English   中英

將文本文件寫入android存儲以供以后查看

[英]Writing a text file to android storage for viewing later

因此,我試圖將一些日志基本上寫入文本文件,以便以后查看。 我在物理電話上而不是仿真器上運行它。 我已經嘗試了許多不同的變體,但我得到的最多是它寫入數據/數據和存儲/模擬的圖像,但是我永遠無法訪問我的文件。 任何幫助,將不勝感激。 我最近未刪除的一些示例是:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM