public static void saveFileNameOnSdcard(String docfilename) { FileOutputStream fos = null; String path=Environment.getExternalStorageDirectory().toString(); try { fos = new FileOutputStream(path+"/log.txt",true); fos.write(docfilename.getBytes()); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ try { fos.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
String path=Environment.getExternalStorageDirectory().toString();
这是获得sdcard的路径。
new FileOutputStream(path+"/log.txt",true)后面的参数是true表示这是已追加的方式写入文件。
FileOutputStream fos = context.openFileOutput("log.txt", Context.MODE_WORLD_READABLE + Context.MODE_WORLD_WRITEABLE);这是在data/data/xxx/下面生成文件