android 读写sdcard文件

本文介绍了一个用于Android应用中进行SD卡文件操作的类SdcardFile,包括创建文件、目录,判断文件是否存在,写入及读取文件内容等功能,并提供了具体的实现代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

开始学习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"/>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值