在AndroidManifest.xml中加入:
- <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE">
- </uses-permission>
写入SD卡:
- File f = new File(android.os.Environment.getExternalStorageDirectory()+"/aaa.txt");
- String str="this is a test about Write SD card file";
- 方法A:
- FileOutputStream fileOS=new FileOutputStream(f);
- fileOS.write(str.getBytes());
- fileOS.close();
- BufferedWriter buf = new BufferedWriter (new OutputStreamWriter(fileOS));
- buf.write(str,0,str.length());
- buf.flush();
- buf.close();
- // FileWriter fw = new FileWriter("/sdcard/cc.txt");
- // fw.write(str);
- // fw.close();
- File file[] = android.os.Environment.getExternalStorageDirectory().listFiles();
- //这里我们只是取得列表中的第二个文件的绝对路径
- String path=file[1].getAbsolutePath();
- try{
- FileInputStream fileIS = new FileInputStream(path);
- BufferedReader buf = new BufferedReader(new InputStreamReader(fileIS));
- String readString = new String();
- //just reading each line and pass it on the debugger
- while((readString = buf.readLine())!= null){
- Log.d("line: ", readString);
- }
- fileIS.close();
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e){
- e.printStackTrace();
- }
- File file1= new File(path);
- boolean isdelte=file1.delete();