public class MainActivity extends AppCompatActivity implements View.OnClickListener { private static final String TAG ="MainActivity" ; private static final int REQUEST_EXTERNAL_STORAGE = 1; String fileName = "test.txt"; @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); if(requestCode == 1){ for(int i=0;i<permissions.length;i ){ if(permissions[i].equals("android.permission.WRITE_EXTERNAL_STORAGE")&&grantResults[i]== PackageManager.PERMISSION_GRANTED){ Toast.makeText(this,"获得权限",Toast.LENGTH_SHORT).show(); }else{ Toast.makeText(this,"权限被拒绝",Toast.LENGTH_SHORT).show(); } } } } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button btn_innerwrite = findViewById(R.id.btn_innerwrite); btn_innerwrite.setOnClickListener(this); Button btn_innerread = findViewById(R.id.btn_innerread); btn_innerread.setOnClickListener(this); Button btn_outerwrite = findViewById(R.id.btn_outerwrite); btn_outerwrite.setOnClickListener(this); Button btn_outerread = findViewById(R.id.btn_outerread); btn_outerread.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()){ case R.id.btn_innerwrite: innerwrite(); break; case R.id.btn_innerread: innerread(); break; case R.id.btn_outerwrite: outerwrite(); break; case R.id.btn_outerread: outerread(); break; default: break; } } private void outerread() { //检查外部设备状态 String envior = Environment.getExternalStorageState(); //如果是加载状态,那么开始写入 if(envior.equals(Environment.MEDIA_MOUNTED)){ ActivityCompat.requestPermissions(this,new String[]{"android.permission.WRITE_EXTERNAL_STORAGE"},1); File sd_path = Environment.getExternalStorageDirectory(); sd_path = new File(sd_path,"/txtfile");//创建路径 sd_path.mkdirs(); File file = new File(sd_path,fileName); String str = ""; FileInputStream fis = null; try { fis = new FileInputStream(file); BufferedReader buff = new BufferedReader(new InputStreamReader(fis)); str=buff.readLine(); //fis.write(str.getBytes()); fis.close(); Toast.makeText(this,"read scucess!" str,Toast.LENGTH_SHORT).show(); Log.i(TAG,"read scucess!" str); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } private void outerwrite() { //检查外部设备状态 String envior = Environment.getExternalStorageState(); //如果是加载状态,那么开始写入 if(envior.equals(Environment.MEDIA_MOUNTED)){ ActivityCompat.requestPermissions(this,new String[]{"android.permission.WRITE_EXTERNAL_STORAGE"},1); File sd_path = Environment.getExternalStorageDirectory(); sd_path = new File(sd_path,"/txtfile");//创建路径 sd_path.mkdirs(); File file = new File(sd_path,fileName); String str = "outer write testing!"; FileOutputStream fos = null; try { fos = new FileOutputStream(file); fos.write(str.getBytes()); fos.close(); Toast.makeText(this,"write scucess!" sd_path.getAbsolutePath(),Toast.LENGTH_SHORT).show(); Log.i(TAG,"write scucess!" sd_path.getAbsolutePath()); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } private void innerread() { //String strr = ""; FileInputStream fis = null; try { fis = openFileInput(fileName); byte[] buffer = new byte[fis.available()]; fis.read(buffer); String strr = new String(buffer); Toast.makeText(this,"read scucess!" strr,Toast.LENGTH_SHORT).show(); fis.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } private void innerwrite() { String str = "innerwrite test!"; FileOutputStream fos = null; try { fos = openFileOutput(fileName,MODE_PRIVATE); fos.write(str.getBytes()); Toast.makeText(this,"write scucess!" getFilesDir().getAbsolutePath(),Toast.LENGTH_SHORT).show(); Log.i(TAG,"file address:" getFilesDir().getAbsolutePath()); fos.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }}
评论