现在的位置: 首页 > 综合 > 正文

android 读写sdcard文件

2013年10月02日 ⁄ 综合 ⁄ 共 2426字 ⁄ 字号 评论关闭

毕业快半年了,不是专业的码农了,以后的工作相对比较清闲,但总感觉少点什么,想培养自己的一些兴趣,就从这里开始吧!

开始学习android开发一个多月了,学着写了一些代码,觉着还是要学一点总结一点才会有收获,就从这里开始吧!

package com.cxf;

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

import android.content.Context;
import android.os.Environment;

public class SdcardFile {
	private String SdPath;//目录
	private String FilePath;//文件路径
	
	public String getSdPath(){
		return SdPath;
	}
	public String getSdFilePath(){
		return FilePath;
	}
	
	private Context context;
	public SdcardFile(Context context){
		//得到当前外部存储设备的目录
		SdPath = Environment.getExternalStorageDirectory() + "/";
		FilePath = null;
		this.context = context;
	}
	public void setFilePath(String filePath){
		FilePath = filePath;
		
	//	return FilePath;
	}
	/**
	 * 在sd卡上创建文件
	 */
	public File createSDFile(String fileName) throws IOException{
		//验证当前目录是否存在
		File file1 = new File(SdPath);
		if(!file1.exists()){
			return file1;
		}
		
		FilePath = SdPath + fileName;
		File file2 = new File(FilePath);
		//如果当前文件不存在,则创建文件
		if(!file2.exists()){
			file2.createNewFile();
		}
		return file2;
	}
	/**
	 * 在sd卡上创建目录
	 */
	public File createSDDir(String dirName){
		File dir = new File(dirName);
		
		if(!dir.exists()){
			dir.mkdir();
			SdPath = new String(dir.toString());
		}
		return dir;
	}
	
	/**
	 * 判断sd卡上的文件是否存在
	 */
	public boolean isFileExist(String fileName){
		File file = new File(SdPath + fileName);
		return file.exists();
	}
	/**
	 * 写文件,保存至手机
	 * 输入:title-主题,content-内容
	 * 返回1 保存失败
	 * 返回0   成功
	 *
	 */
	 public int FileWrite(String title,String content) throws IOException{	
		 try{
			FileOutputStream os = context.openFileOutput(FilePath, context.MODE_PRIVATE);
			os.write((title+"\n").getBytes());
			os.write((content+"\n").getBytes());
			os.close();
		}
		catch(IOException e){
			return 1;
		}
		return 0;
	}
	 /**
		 * 写文件,保存至SD卡
		 * 输入:title-主题,content-内容
		 * 返回1 保存失败
		 * 返回0   成功
		 *
	*/
	public int SDFileWrite(String title,String content) throws IOException{	
		try{
			FileOutputStream os = new FileOutputStream(FilePath);
		//	os.write((title+"\n").getBytes());
			os.write((content+"\n").getBytes());
			os.close();
		}
		catch(IOException e){
			return 1;
		}
		return 0;
	}
	/**
	 * 读文件
	 * 输入:文件名SdPath
	 * 输出:文件内容
	 */
	public String SDFileRead(){
		String text = null;
		if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
			try{
				FileInputStream read = new FileInputStream(FilePath);
				
				byte[] buffer = new byte[1024];
				read.read(buffer);
				text = new String(buffer);
			}
			catch(IOException e){
				
			}
		}
		return text; 
	}
	/**
	 * 删除文件
	 */
	public void SDFileDelete(String fileName){
		File file = new File(fileName);
		
		if(file.exists()){
			if(file.isFile()){
				file.delete();
			}
		}
	}
}

在Manifest文件中记得要上下面两句:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>

抱歉!评论已关闭.