写文件
写文件用输出流,读文件用输入流
//往手机内存写文件
//往手机内存写文件 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(); }