android基礎——內部存儲files(讀/取)

/**
 * 
 * @author 劉中林
 *
 */
public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	}

	// 操作內部存儲---->跟sp位置相似都在data/data中,----》Android設備其他的應用無法訪問
	// Android需要root才能訪問這個區域
	// 不能保存大量的數據---》內部存儲的空間有限
	// 也會伴隨應用程序的卸載一起刪除
	public void btnInternalFile(View view) {
		switch (view.getId()) {
		case R.id.save:// 向內部存儲區保存一個文件
			// 1.保存的內容
			byte[] data = "天生我才必有用,千金散盡還復來。".getBytes();
			FileOutputStream fos = null;
			// 2.打開文件流
			try {
				fos = openFileOutput("libai", MODE_APPEND);// 這個模式,繼續想原有文件末尾增加內容
				// 3.向文件當中寫入數據
				fos.write(data, 0, data.length);
				fos.flush();
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}finally{
//				4.關閉打開的流
				if(fos!=null){
					try {
						fos.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}

			break;
			
		case R.id.getFile:
//			1.獲取inputStream
			FileInputStream fis = null;
			ByteArrayOutputStream baos = null;
			try {
				fis = openFileInput("libai");
//				2.聲明內存流接收數據
				baos = new ByteArrayOutputStream();
				
				int len = 0;
				byte[] buf = new byte[1024];
//				向內存流讀取數據
				while((len = fis.read(buf))!=-1){
					baos.write(buf, 0, len);
				}
//				獲取內存流中的數據
				byte[] result = baos.toByteArray();
				String dataResult = new String(result);
				Toast.makeText(this, "獲取內部存儲的文件內容是"+dataResult, Toast.LENGTH_SHORT).show();
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}finally{
				if(fis!=null){
					try {
						fis.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
				if(baos!=null){
					try {
						baos.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
			break;
			
		case R.id.delete:
//			1.獲取這個文件
//			該方法的返回值就是data/data/包名/files
			File filesDir = getFilesDir();
			
//			2.刪除文件,文件夾
//			filesDir.delete();
			
//			遞歸刪除文件
			deleteFiles(filesDir);
		default:
			break;
		}
	}

	private void deleteFiles(File filesDir) {
		// TODO Auto-generated method stub
//		listFiles()方法的使用
		if(filesDir.isFile()){ //判斷成立的話,是文件,直接刪除
			filesDir.delete();
		}else{
			if(filesDir.listFiles().length==0){//空的文件夾
				filesDir.delete();
			}else{//文件夾中有文件或者文件夾
				File[] files = filesDir.listFiles();
				
				for(File file:files){//增強for循環
					if(file.isFile()){
						file.delete();
					}else{
						deleteFiles(file);
					}
				}
//				for循環執行結束,filesDir就是一個空文件夾了
				filesDir.delete();
			}
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章