package com.example.suruixue.myapplication;

import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        savetptofile("ddddd");
        savetptofile("22222");
        savetptofile("3333");
    }
    private void savetptofile(String mstring){
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date curDate = new Date(System.currentTimeMillis());
        String nDate = formatter.format(curDate);
        mstring = nDate+"     " + mstring +"\r\n";
        FileWriter fw = null;
        try {
            String myPath = "/sdcard";// Environment.getInternalStoragePath().getPath();
            //File mfile = new File(myPath+"/srx-tp.txt");
            File mfile = new File(myPath,"/srx-tp.txt");
            if(mfile.exists()){
                Toast.makeText(this,"file exits", Toast.LENGTH_LONG);
                Log.e("Tptext","file exits");
            } else {
                Toast.makeText(this,"file not exits", Toast.LENGTH_LONG);
                Log.e("TeemoTp","file not exits");
            }
            fw = new FileWriter(mfile, true);
            fw.write(mstring);
            fw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

  

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.suruixue.myapplication">

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

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

这样数据就会被保存到/sdcard/tp.txt下
pull出来看到如下

2015-01-01 08:53:05     ddddd
2015-01-01 08:53:05     22222
2015-01-01 08:53:05     3333

如上是apk的方法,在framework层此法不行,放在如下位置可以。不用加权限

    private void savetptofile(String mstring){
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date curDate = new Date(System.currentTimeMillis());
        String nDate = formatter.format(curDate);
        mstring = nDate+"     " + mstring +"\r\n";
        FileWriter fw = null;
        //String myPath = "/data/data/com.xx.xx/srx-tp.txt";
        //String myPath = "/sdcard/tp-test.txt";
        try {
            String myPath = "/data/data"; //"/data/system"
            File mfile = new File(myPath,"/tp.txt");
            //File mfile = new File(myPath);
            if(!mfile.exists()){
                Log.e("Tptext","file not exits");
            
            }else{
               Log.e("Tptext","file exits");
            }
            fw = new FileWriter(mfile, true);
            fw.write(mstring);
            fw.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

posted on 2020-03-23 19:12  snowdrop  阅读(100)  评论(0)    收藏  举报