sd卡读写操作

public class SDCard {
	private static final String APP_TAG = "sdcard";
	private static final String DIRECTORY = "test_folder";
	private File sdDir;
	private String sdDirFilePath;

	public SDCard() {
		sdDir = new File(Environment.getExternalStorageDirectory().getPath());
		sdDirFilePath = sdDir.getPath() + "/" + DIRECTORY;
	}

	public void writeFile(String fileName, String content) {
		if (isExistsAndCanWrite(sdDir) == true) {
			File testDir = new File(sdDirFilePath);
			testDir.mkdir();
			if (isExistsAndCanWrite(testDir) == true) {
				File file = new File(testDir.getAbsolutePath() + "/" + fileName);
				try {
					file.createNewFile();
				} catch (IOException e) {
					Log.e(APP_TAG, "error creating file", e);
				}

				if (isExistsAndCanWrite(file) == true) {
					FileOutputStream fos = null;
					try {
						fos = new FileOutputStream(file, true);
						fos.write(content.getBytes());
					} catch (FileNotFoundException e) {
						Log.e(APP_TAG, "ERROR", e);
					} catch (IOException e) {
						Log.e(APP_TAG, "ERROR", e);
					} finally {
						if (fos != null) {
							try {
								fos.flush();
								fos.close();
							} catch (IOException e) {
							}
						}
					}
				} else {
					Log.e(APP_TAG, "error writing to file");
				}

			} else {
				Log.e(APP_TAG, "ERROR, unable to write to /sdcard/test_folder");
			}
		} else {
			Log.e(APP_TAG, "ERROR, /sdcard path not available");
		}
	}

	private boolean isExistsAndCanWrite(File sdDir) {
		return sdDir.exists() && sdDir.canWrite();
	}

	public String readFile(String fileName) {
		String returnStr = "";
		File readFile = new File(sdDirFilePath + "/" + fileName);

		if (isExistsAndCanWrite(readFile) == true) {
			FileInputStream fis = null;
			try {
				fis = new FileInputStream(readFile);
				byte[] reader = new byte[fis.available()];
				while (fis.read(reader) != -1) {
					returnStr = new String(reader);
				}
			} catch (IOException e) {
				Log.e(APP_TAG, e.getMessage(), e);
			} finally {
				if (fis != null) {
					try {
						fis.close();
					} catch (IOException e) {
					}
				}
			}
		}
		return returnStr;
	}

	public String deleteFile(String fileName) {
		String returnStr = "";
		File readFile = new File(sdDirFilePath + "/" + fileName);
		boolean isDeleted = readFile.delete();
		if (isDeleted == true) {
			returnStr = "File is sucessfuly deleted";
		}
		return returnStr;
	}

	/**
	 * Directories must be empty before they will be deleted. <br>
	 * 删除目录时,此目录下必须为空。
	 * 
	 */
	public String deleteDirectory() {
		String returnStr = "";
		File directory = new File(sdDirFilePath);
		if (isExistsAndCanWrite(directory) == false) {
			return "No directory";
		}

		// 清空目录下的所有文件
		File file[] = directory.listFiles();
		for (File element : file) {
			if (element.isFile() == true) {
				deleteFile(element.getName());
			}
		}

		// 删除该目录
		boolean isDeleted = directory.delete();
		if (isDeleted == true) {
			returnStr = "Directory is sucessfuly deleted";
		}
		return returnStr;
	}
}
测试效果图:
 
 

按钮WriteSdcard点击后,生成目录test_folder,并在此目录下生成文件test-1.txt

测试例
	private void onButtonWriteSdCardClick() {
		String fileName = "test-" + txtCount + ".txt";
		String content = "Hello, World!---test---";
		SDCard sdCard = new SDCard();
		sdCard.writeFile(fileName, content);
	}

 按钮ReadSdcard读取test-1.txt里的数据 

测试例:
	private void onButtonReadSdCardClick() {
		String fileName = "test-1.txt";
		SDCard sdCard = new SDCard();
		String returnStr = sdCard.readFile(fileName);
		textView.setText("sdcard:" + returnStr);
	}

 按钮DeleteFileSdcard,按文件名删除文件 

测试例:
	private void onButtonDeleteFileSdCardClick() {
		String fileName = "test-1.txt";
		SDCard sdCard = new SDCard();
		String returnStr = sdCard.deleteFile(fileName);
		textView.setText("sdcard:" + returnStr);
	}

 按钮DeleteDirectorySdcard,删除目录test_folder 

测试例:
	private void onButtonDeleteDirectorySdCardClick() {
		SDCard sdCard = new SDCard();
		String returnStr = sdCard.deleteDirectory();
		textView.setText("sdcard:" + returnStr);
	}
* AndroidManifest.xml里追加下面语句<br> * <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
 
 
 
 
 
 
 
 
 
 
 
 
 
 

                
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值