Android studio 37 SD卡保存文件 新建文件夹

增加权限
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />




import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.widget.Toast;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;

public class MainActivity extends AppCompatActivity {
    private String  bath= Environment.getExternalStorageDirectory().getPath()+"/test/ggg";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final EditText etext = findViewById(R.id.editText);
        Button btn_save=findViewById(R.id.btn_save);
        Button btn_cancel=findViewById(R.id.btn_cancel);

        btn_save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                File file =new File(bath);
                if(!file.exists()){
                    boolean flag=file.mkdirs();
                    if(flag){
                        Toast.makeText(MainActivity.this,bath,Toast.LENGTH_SHORT).show();

                        Toast.makeText(MainActivity.this,"创建成功",Toast.LENGTH_SHORT).show();
                    }else{
                        Toast.makeText(MainActivity.this,"创建失败",Toast.LENGTH_SHORT).show();
                    }
                }

                String filename = "xuxing2020.txt";

                String string = "Hello world!";

                FileOutputStream outputStream;

                try{

                    outputStream = openFileOutput(filename, Context.MODE_PRIVATE);

                    outputStream.write(string.getBytes());

                    outputStream.close();

                } catch(Exception e) {

                    e.printStackTrace();

                }

                try{
                    FileOutputStream outStream = new FileOutputStream("/sdcard/"+"xxxd222"+".txt",true);
                    OutputStreamWriter writer = new OutputStreamWriter(outStream,"gb2312");
                    writer.write("sssssssssssssdddddddddddddddddddddddddddd");
                    writer.write("/n");
                    writer.flush();
                    writer.close();//记得关闭

                    outStream.close();
                    }
                    catch (Exception e)
                    {
                    Log.e("m", "file write error");
                    }

///  data/data/com.example.myapplication/files/xuxing2020.txt
            }
        });
    }

}





展开阅读全文
©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客
应支付0元
点击重新获取
扫码支付

支付成功即可阅读