public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
File sdCard = Environment.getExternalStorageDirectory(); //取得sdCard 的目录
try {
//测试创建文件和文件夹
boolean CreateFileResult = createFile(sdCard.getAbsolutePath(),"sdCreateFile.sq",false); //创建文件
boolean CreateDirResult = createFile(sdCard.getAbsolutePath(),"sdCreateDir.dir",true); //创建文件夹
} catch (IOException e) {e.printStackTrace();}
//测试删除文件和文件夹
boolean deleteFileResult = deleteFile(sdCard.getAbsolutePath(), "sdCreateFile.sq");
boolean deleteDirResult = deleteFile(sdCard.getAbsolutePath(), "sdCreateDir.dir");
}
//创建文件夹或目录
boolean createFile(String path,String fileName,boolean isDir) throws IOException
{
File dir = new File(path);
if(dir.exists()!=true) return false; //path no exists
File newFile = new File(path+"/"+fileName);
return isDir==true? newFile.mkdir():newFile.createNewFile();
}
//删除文件夹或目录
boolean deleteFile(String path,String fileName)
{
File dir = new File(path);
if(dir.exists()!=true)return false; //path no exists
return new File(path+"/"+fileName).delete();
}