Android操作SD卡文件

1)修改MainActivity.java代码如下

package com.example.androiddemo;


import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;

import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {

	private Button button1=null;
	private Button button2=null;
	/*获取sd card根目录路径*/
	private String rootPath=Environment.getExternalStorageDirectory().getPath();
	private File rootFolder=null;
	private File folder=null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		rootFolder=new File(rootPath);
		/*创建文件、文件夹*/
		button1=(Button)findViewById(R.id.button1);
		button1.setOnClickListener(new Button.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				/*创建文件夹*/
				folder=createFolder(rootFolder, "a_folder");
				/*创建子文件夹*/
				createFolder(folder, "b_folder");
				/*创建文件*/
				createFile(folder,"a.txt","test");
			}
		});
		/*删除文件、文件夹*/
		button2=(Button)findViewById(R.id.button2);
		button2.setOnClickListener(new Button.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				/*删除文件*/
				deleteFile(folder);
			}
		});
	}
	/*删除文件夹、文件夹*/
	private void deleteFile(File file){
		if(Environment.getExternalStorageState().equals
		(Environment.MEDIA_MOUNTED)){
			if(file.exists()){
				if(file.isFile()){//文件直接删除
					file.delete();
				}
				if(file.isDirectory()){//文件夹
					File[] childfFiles=file.listFiles();
					if(childfFiles==null||childfFiles.length==0){//文件夹不存在子文件(文件夹)
						file.delete();
					}else {//文件夹中存在文件(子文件)
						for (File chilFile : childfFiles) {
							deleteFile(chilFile);
						}
						file.delete();
					}
				}
			}
		}
	}
	/*创建文件夹*/
	private File createFolder(File parentFolder,String folderName){
		File folder=null;
		if(Environment.getExternalStorageState().equals
		(Environment.MEDIA_MOUNTED)){
			if(parentFolder.isDirectory()){
				folder=new File(parentFolder.getPath()+"/"+folderName);
				if(!folder.exists()){
					folder.mkdir();
				}
			}
		}
		return folder;
	}
	/*创建文件*/
	private File createFile(File parentFolder,String fileName,String text){
		File file=null;
		if(Environment.getExternalStorageState().equals
		(Environment.MEDIA_MOUNTED)){
			if(parentFolder.isDirectory()){
				file=new File(parentFolder.getPath()+"/"+fileName);
				OutputStreamWriter osw;
				try {
					if(!file.exists()){
						file.createNewFile();
						osw=new OutputStreamWriter(new FileOutputStream(file),"utf-8");
						osw.write(text);
						osw.close();
					}else {
						osw=new OutputStreamWriter(new FileOutputStream(file,true),"utf-8");
						osw.write("\n");
						osw.write(text);
						osw.flush();
						osw.close();
					}
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		return file;
	}
	@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;
	}

}
2)在AndroidManifest.xml中添加权限代码如下


<!--添加访问扩展设备的权限 -->
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>


你可能感兴趣的:(android,删除,创建,文件,SD卡)