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;
}
}
测试效果图:
![](http://hi.csdn.net/attachment/201202/27/0_13303259585Ev9.gif)
按钮WriteSdcard点击后,生成目录test_folder,并在此目录下生成文件test-1.txt
![](http://hi.csdn.net/attachment/201202/27/0_1330326132L5Y4.gif)
测试例
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" />