【问题标题】:I am unable to locate my saved text file in phone memory我无法在手机内存中找到我保存的文本文件
【发布时间】:2015-09-24 11:33:16
【问题描述】:

我正在使用此代码存储一个文本文件,其中包含按下应用程序中的“开始”按钮并进一步阅读它的时间名称。我正在尝试将文件保存在用户安装应用程序的内存中(即 SD 卡或手机内存)。我正在存储的文件可以很好地读取并显示在 logcat 中,但我找不到它保存在内存中的位置(电话或 SD)。

public void write_data(String s, Queue<SeismicDataPoint> t) throws IOException {

    String path = getBaseContext().getFilesDir().getAbsolutePath();
    File dir = new File(path);
    if (!dir.exists())
        dir.mkdirs();
    FileOutputStream fOut = getBaseContext().openFileOutput(s + ".txt", MODE_APPEND);

    OutputStreamWriter osw = new OutputStreamWriter(fOut);
    SeismicDataPoint temp ;
    for(int i = 0; i < bufferSize; i++)

    {
        temp=t.poll();
        osw.append(temp.timestamp+" "+temp.x+" "+temp.y+" "+temp.z+"\n");

    }



    osw.flush();
    osw.close();


}
public void read_data(String s) throws IOException {



    BufferedReader b = new BufferedReader(new FileReader(s+".txt"));
    while(true){
    String osw = b.readLine();

        if(osw==null)break;




}

这里也是清单文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mycompany.falldetect" >
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

【问题讨论】:

    标签: android file memory text


    【解决方案1】:

    getFilesDir 指向内部存储目录。如果你想在外部存储一些东西,那么你应该使用 path as

    String path = Environment.getExternalStorageDirectory().toString();
    

    也请阅读http://www.grokkingandroid.com/how-to-correctly-store-app-specific-files-in-android/

    更新:检查此代码

    private void saveFile() {
    
        String root = Environment.getExternalStorageDirectory().toString();
        File myDir = new File(root + "/files");
        myDir.mkdirs();
        File file = new File (myDir, "sample.txt");
        if (file.exists ()) file.delete ();
        try {
            FileOutputStream out = new FileOutputStream(file);
            PrintWriter pw = new PrintWriter(out);
            pw.println("my sample txt");
            pw.flush();
            pw.close();
            out.flush();
            out.close();
    
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    

    【讨论】:

    • 我按照你的建议做了还是看不到存储的文件
    • 查看示例代码,您可以在 SDcard 和该 sample.txt 中看到一个文件夹
    • 你的设备是什么?贴出你最新的代码,让我测试一下
    • 我在网上看了一会儿,你的代码帮了大忙。现在可以了。谢谢。
    【解决方案2】:

    应用程序私有数据文件存储在&lt;internal_storage&gt;/data/data/&lt;package&gt; 如果文件创建为MODE_PRIVATE,则无法使用 FileManager 等应用程序查看/访问该文件。

    【讨论】:

    • 除了 MODE_PRIVATE 还有其他存储方法吗?
    • MODE_WORLD_READABLE/MODE_WORLD_WRITEABLE。 .还要检查其他模式
    • 但我使用的是 MODE_APPEND
    • 这是另一段代码。我已经通过在我身边创建一个示例对其进行了测试。尝试这个。该文件在外部目录中成功创建。如果您遇到此代码的问题,请告诉我。 -javatpoint.com/android-external-storage-example
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-03
    • 1970-01-01
    • 1970-01-01
    • 2011-09-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多