package com.example.menu; import android.os.Bundle; import android.os.Environment; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; public class MainActivity extends AppCompatActivity { package net.bwie.externalstorage; import android.os.Bundle; import android.os.Environment; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; public class MainActivity extends AppCompatActivity implements View.OnClickListener { protected TextView mResultTv; protected EditText mEditText; protected Button mWriteBtn; protected Button mReadBtn; public static final String FILE_NAME = "1511c.txt"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); super.setContentView(R.layout.activity_main); initView(); } @Override public void onClick(View view) { // 外部存储公共路径,例如DCIM, DOWNLOAD, MUSIC等 File publicPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS); // 外部存储私有路径 File privatePath = this.getExternalFilesDir(null);// 代表Android/包名/files路径 // 自定义路径 File myPath = Environment.getExternalStorageDirectory();// 外部存储根目录 // 文件路径+文件名 File file = new File(publicPath, FILE_NAME); if (view.getId() == R.id.write_btn) {// 写入数据 String content = mEditText.getText().toString(); writeFile(content, file); } else if (view.getId() == R.id.read_btn) {// 读取数据 String result = readFile(file); mResultTv.setText(result); } } // 读取文件 private String readFile(File filePath) { BufferedReader br = null; try { br = new BufferedReader(new InputStreamReader(new FileInputStream(filePath))); String result = "";// 总数据 String line = "";// 读取的每一行数据 while ((line = br.readLine()) != null) { result += line; } return result; } catch (Exception e) { e.printStackTrace(); } finally { if (br != null) { try { br.close(); } catch (IOException e) { e.printStackTrace(); } } } return null; } // 写入数据 private void writeFile(String content, File filePath) { // 文件名、文件路径、文件内容 FileOutputStream fos = null; try { fos = new FileOutputStream(filePath); fos.write(content.getBytes());// 字符串转字节数组 Toast.makeText(this, "写入成功", Toast.LENGTH_SHORT).show(); } catch (Exception e) { e.printStackTrace(); } finally { if (fos != null) { try { fos.close(); } catch (IOException e) { e.printStackTrace(); } } } } private void initView() { mResultTv = (TextView) findViewById(R.id.result_tv); mEditText = (EditText) findViewById(R.id.edit_text); mWriteBtn = (Button) findViewById(R.id.write_btn); mWriteBtn.setOnClickListener(MainActivity.this); mReadBtn = (Button) findViewById(R.id.read_btn); mReadBtn.setOnClickListener(MainActivity.this); } } }
SD卡读写
最新推荐文章于 2022-11-22 10:15:23 发布