Android------文件与权限管理

写文件

写文件用输出流,读文件用输入流
在这里插入图片描述

//往手机内存写文件

				//往手机内存写文件
  				String str ="hello world";
                try {
                    FileOutputStream fos=openFileOutput("testfile",MODE_PRIVATE);
                    fos.write(str.getBytes());
                    fos.close();
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }

往外存私有文件写

//往外存私有文件写,但其父目录 不容易
                File dir=getExternalFilesDir(null);
                File testfile=new File(dir,"testsdfile");
                FileOutputStream fos= null;
                try {
                    fos = new FileOutputStream(testfile);
                    fos.write(str.getBytes());
                    fos.close();
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }

//往其外存公有文件写

 String str ="hello world";
                //往其外存公有文件写,牵扯到一个问题,权限,得要权限
                File dir= Environment.getExternalStorageDirectory();
                File testfile=new File(dir,"testpublicsdfile");
                FileOutputStream fos= null;
                try {
                    fos = new FileOutputStream(testfile);
                    fos.write(str.getBytes());
                    fos.close();
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }

猜你喜欢

转载自blog.csdn.net/he1234555/article/details/106792017