Code example for File

Methods: exists

0
	}	 
	 
	protected boolean createDirectory(String directoryName){
		boolean status;
		if ((testSaveLocationExists())&&(!directoryName.equals(""))){
			File path = Environment.getExternalStorageDirectory();
            File newPath = constructFilePaths(path.toString(), directoryName);
			status = newPath.mkdir();
			status = true;
		}else 
			status = false;
		return status;
	} 
	 
	protected boolean testSaveLocationExists(){ 
		String sDCardStatus = Environment.getExternalStorageState();
		boolean status;
		if (sDCardStatus.equals(Environment.MEDIA_MOUNTED)){
			status = true;
		}else 
			status = false;
		return status;
	} 
	 
	protected boolean deleteDirectory(String fileName){
		boolean status;
		SecurityManager checker = new SecurityManager();
			 
		if ((testSaveLocationExists())&&(!fileName.equals(""))){
		 
			File path = Environment.getExternalStorageDirectory();
            File newPath = constructFilePaths(path.toString(), fileName);
			checker.checkDelete(newPath.toString());
			if(newPath.isDirectory()){
				String[] listfile = newPath.list();
				// delete all files within the specified directory and then delete the directory 
				try{ 
					for (int i=0; i < listfile.length; i++){
						File deletedFile = new File (newPath.toString()+"/"+listfile[i].toString());
						deletedFile.delete();
					} 
					newPath.delete();
					Log.i("DirectoryManager deleteDirectory", fileName);
					status = true;
				}catch (Exception e){
					e.printStackTrace();
					status = false;
				} 
				 
			}else 
				status = false;
		}else 
			status = false;
		return status;
	} 
	 
	protected boolean deleteFile(String fileName){
		boolean status;
		SecurityManager checker = new SecurityManager();
			 
		if ((testSaveLocationExists())&&(!fileName.equals(""))){
		 
			File path = Environment.getExternalStorageDirectory();
            File newPath = constructFilePaths(path.toString(), fileName);
			checker.checkDelete(newPath.toString());
			if (newPath.isFile()){
				try { 
					Log.i("DirectoryManager deleteFile", fileName);
					newPath.delete();
					status = true;
				}catch (SecurityException se){
					se.printStackTrace();
					status = false;
				} 
			}else 
				status = false;
		}else 
			status = false;
		return status;
	} 
	 
	private File constructFilePaths (String file1, String file2){
		File newPath;
		newPath = new File(file1+"/"+file2);
		return newPath;
	} 
 
}