android create file file, text and write the contents

Create a file after file requires the user to manually turn on android 6.0 Permissions The following describes the case of using

Here are just written content so we add write permissions in the configuration file,

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

If necessary, read, delete permissions, you need to add another

 Look at specific code

  //sdk 大于6.0的判断
                if (Build.VERSION.SDK_INT >= 23) {
                    int REQUEST_CODE_CONTACT = 101;
                    String[] permissions = {
                            Manifest.permission.WRITE_EXTERNAL_STORAGE};
                    //验证是否许可权限
                    for (String str : permissions) {
                        if (MainActivity.this.checkSelfPermission(str) != PackageManager.PERMISSION_GRANTED) {
                            //申请权限
                            MainActivity.this.requestPermissions(permissions, REQUEST_CODE_CONTACT);
                            return;
                        } else {
                            String path = Environment.getExternalStorageDirectory() + "/MyTest";
                            Log.e("------path", path);
                            File files = new File(path);
                            if (!files.exists()) {
                                files.mkdirs();
                            }
                            try {
                                FileWriter fw = new FileWriter(path + File.separator + "log.txt");
                                fw.write("学而时习之,温故而知新");
                                fw.close();
                                Toast.makeText(MainActivity.this, "文件写入成功", Toast.LENGTH_SHORT).show();
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                            
                        }
                    }
                }  

FileOutputStream when the content is written a lot of people use this to see their habits, because the FileOutputStream to the incoming content is a byte

Also need to convert once, using FileWriter is so String can be used directly in, the type used here FileWriter

Look specifically at the mobile phone inside that directory

Look print path E / ------ path: / storage / emulated / 0 / MyTest, beginning to search for your emulated to this folder inside to find, but did not find content

And finally check the code feel no problem then, finally put their own time into the most recent file in the file created above to see the 

Look at the contents of the written

This done, but if there is a special place to write, developers need to use the write log files and record time

This allows the use of the above into a static class, see their habits put the tools inside the line, look at the code below

public class FileLog{

    /**
     * 保存日志到本地存储根目录下
     * @param message      保存的信息
     * @param fileName     保存的文件名称
     * @param messageTitle 保存的信息标题
     */
    public static void saveLog(String messageTitle,String message, String fileName) {
        String path = Environment.getExternalStorageDirectory() + "/MyLog";
        File files = new File(path);
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-M-dd HH:mm:ss,EE", Locale.CHINA);
        String formatDate = dateFormat.format(date);
        if (!files.exists()) {
            files.mkdirs();
        }
        if (files.exists()) {
            try {
                FileWriter fw = new FileWriter(path + File.separator
                        + fileName + ".txt");
                fw.write(formatDate + " " + messageTitle + "\n");
                fw.write(message + "\n");
                fw.write("\n");
                fw.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }
}

Use the following


public class MainActivity extends AppCompatActivity {

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


        findViewById(R.id.txt).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //sdk 大于6.0的判断
                if (Build.VERSION.SDK_INT >= 23) {
                    int REQUEST_CODE_CONTACT = 101;
                    String[] permissions = {
                            Manifest.permission.WRITE_EXTERNAL_STORAGE};
                    //验证是否许可权限
                    for (String str : permissions) {
                        if (MainActivity.this.checkSelfPermission(str) != PackageManager.PERMISSION_GRANTED) {
                            //申请权限
                            MainActivity.this.requestPermissions(permissions, REQUEST_CODE_CONTACT);
                            return;
                        } else {
                            FileLog.saveLog("------查看saveLog是否生效", "具体要查看的内容,这个可以看成Log里面要查看日志的内容", "MainActivityLog");

                        }
                    }

                }
            }
        });


    }
}

 

View the generated files

Generated txt take their own name

View generated content

 

View source address, go Pikachu

Published 658 original articles · won praise 250 · views 630 000 +

Guess you like

Origin blog.csdn.net/qq_33210042/article/details/100973069