zoukankan      html  css  js  c++  java
  • 第九篇-新建文件夹和文本文件mkdirs,createNewFile

    一、新建一个empty activity的项目

    二、修改AndroidMainfest.xml,添加用户权限。

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.aimee.test1">
        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
        <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
        <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    
        <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>
    
    </manifest>
    

      三、修改activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello World!"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    
        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="8dp"
            android:layout_marginTop="8dp"
            android:text="CreateFile"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    
    </android.support.constraint.ConstraintLayout>
    

      四、修改MainActivity.java

    package com.example.aimee.test1;
    
    import android.Manifest;
    import android.content.pm.PackageManager;
    import android.os.Build;
    import android.os.Environment;
    import android.support.annotation.NonNull;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Toast;
    
    import java.io.File;
    import java.io.IOException;
    
    public class MainActivity extends AppCompatActivity {
        private final int REQUESTCODE=101;
        private String fileName="test";
    
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    try {
                        create(fileName);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
    
                }
            });
        }
        @Override
        public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,@NonNull int[] grantResults){
            super.onRequestPermissionsResult(requestCode,permissions,grantResults);
            if(requestCode==REQUESTCODE){
                if (permissions[0].equals(Manifest.permission.WRITE_EXTERNAL_STORAGE) && grantResults[0]== PackageManager.PERMISSION_GRANTED){
                    Toast.makeText(MainActivity.this,"111",Toast.LENGTH_LONG).show();
    
                }else {
                    Toast.makeText(MainActivity.this,"222",Toast.LENGTH_LONG).show();
                }
            }
        }
    
        public void create(String fileName) throws IOException {
            if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.M){
                int checkSelfPermission=checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE);
                if(checkSelfPermission==PackageManager.PERMISSION_DENIED){
                    requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},REQUESTCODE);
                }
            }
            String path=Environment.getExternalStorageDirectory().getAbsolutePath();
            File file=new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/"+fileName);
            if(!file.exists()){
                boolean isSuccess=file.mkdirs();
                Toast.makeText(MainActivity.this,"333",Toast.LENGTH_LONG).show();
    
            }else{
                String file_name=path+"/test.txt";
                File f=new File(file_name);
                if(!f.exists()){
                    Toast.makeText(MainActivity.this,"文件不存在",Toast.LENGTH_LONG).show();
                    f.createNewFile();
                }
                else{
                    Toast.makeText(MainActivity.this,"文件创建成功",Toast.LENGTH_LONG).show();
                }
                Toast.makeText(MainActivity.this,path,Toast.LENGTH_LONG).show();
            }
        }
    }
    

      如此便可以创建一个文件夹test,在手机的根目录下,同时创建一个text.txt的文件,在test文件夹下。

  • 相关阅读:
    Java实现Http请求的常用方式
    Java中Map和Object的互相转换方式
    java中过多if-else分支语句的优化方案
    将一个数组,以特定的字符拼接成字符串
    括号匹配算法
    面试题随笔1
    Docker run 的一些简单命令
    集群,负载均衡,分布式的区别
    Docker 容器的常用命令
    web应用常用的CURL基本命令
  • 原文地址:https://www.cnblogs.com/smart-zihan/p/9825312.html
Copyright © 2011-2022 走看看