android SDCARD 读写操作

本文介绍了一个用于Android平台的文件操作工具类,包括文件创建、写入字符串到文件及从文件中读取字符串等功能。

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

代码如下所示:



  1. package com.mzz.utils;  
  2.   
  3. import java.io.File;  
  4. import java.io.FileInputStream;  
  5. import java.io.FileNotFoundException;  
  6. import java.io.FileOutputStream;  
  7. import java.io.IOException;  
  8.   
  9. import org.apache.http.util.EncodingUtils;  
  10.   
  11. import android.os.Environment;  
  12.   
  13. public class FileUtil {  
  14.   
  15.     private File file = null;  
  16.       
  17.     private final String SDCARD_PATH = Environment.getExternalStorageDirectory() + "/";  
  18.     /** 
  19.      * 根据目录和文件名创建文件 
  20.      * @param dir 如果为空代表在SDCARD跟目录创建新文件 
  21.      * @param fileName 创建文件的文件名 
  22.      * @return -1表示文件已存在 0表示创建失败 1表示创建成功 
  23.      */  
  24.     public int createNewFile(String dir , String fileName) {  
  25.           
  26.         if(dir.length() != 0) {  
  27.             File path = new File(SDCARD_PATH + dir);  
  28.             if(!path.exists()) {  
  29.                 file = new File(SDCARD_PATH + dir);  
  30.                 //创建目录   
  31.                 if(file.mkdirs()) {  
  32.                     //将file重新赋值为带文件名的   
  33.                     file = new File(SDCARD_PATH + dir + "/" + fileName);  
  34.                 }  
  35.             }  
  36.             else {  
  37.                 file = new File(SDCARD_PATH + dir + "/" + fileName);  
  38.             }  
  39.         }   
  40.         else {  
  41.             file = new File(SDCARD_PATH + fileName);  
  42.         }  
  43.         if(file.exists()) {  
  44.             return -1;  
  45.         }   
  46.         else {  
  47.             try {  
  48.                 if(file.createNewFile()) {  
  49.                     return 1;  
  50.                 }  
  51.             } catch (IOException e) {  
  52.                 // TODO Auto-generated catch block   
  53.                 e.printStackTrace();  
  54.             }  
  55.         }  
  56.           
  57.         return 0;  
  58.     }  
  59.       
  60.     //往指定目录、指定文件中写入String对象   
  61.     public void writeString2File(String dir , String fileName, String src) {  
  62.         FileOutputStream out = null;  
  63.         if(createNewFile(dir,fileName) != 0)  
  64.         {  
  65.             try {  
  66.                 out = new FileOutputStream(file);  
  67.                 out.write(src.getBytes());  
  68.             } catch (FileNotFoundException e) {  
  69.                 // TODO Auto-generated catch block   
  70.                 e.printStackTrace();  
  71.             } catch (IOException e) {  
  72.                 // TODO Auto-generated catch block   
  73.                 e.printStackTrace();  
  74.             } finally {  
  75.                 try {  
  76.                     if(createNewFile(dir,fileName) != 0)  
  77.                         out.close();  
  78.                 } catch (IOException e) {  
  79.                     // TODO Auto-generated catch block   
  80.                     e.printStackTrace();  
  81.                 }  
  82.             }  
  83.               
  84.         }  
  85.     }  
  86.       
  87.     //读指定目录、指定文件中的String数据   
  88.     public String readFile(String dir, String fileName) {  
  89.         FileInputStream input = null;  
  90.         byte[] buffer = new byte[1024 * 10];//10K   
  91.         if(createNewFile(dir, fileName) != 0) {  
  92.             try {  
  93.                 input = new FileInputStream(file);  
  94.                 input.read(buffer);  
  95.                 return EncodingUtils.getString(buffer, "UTF-8");  
  96.             } catch (FileNotFoundException e) {  
  97.                 // TODO Auto-generated catch block   
  98.                 e.printStackTrace();  
  99.             } catch (IOException e) {  
  100.                 // TODO Auto-generated catch block   
  101.                 e.printStackTrace();  
  102.             }  
  103.         }  
  104.         return null;  
  105.     }  
  106. }  
package com.mzz.utils;

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

import org.apache.http.util.EncodingUtils;

import android.os.Environment;

public class FileUtil {

	private File file = null;
	
	private final String SDCARD_PATH = Environment.getExternalStorageDirectory() + "/";
	/**
	 * 根据目录和文件名创建文件
	 * @param dir 如果为空代表在SDCARD跟目录创建新文件
	 * @param fileName 创建文件的文件名
	 * @return -1表示文件已存在 0表示创建失败 1表示创建成功
	 */
	public int createNewFile(String dir , String fileName) {
		
		if(dir.length() != 0) {
			File path = new File(SDCARD_PATH + dir);
			if(!path.exists()) {
				file = new File(SDCARD_PATH + dir);
				//创建目录
				if(file.mkdirs()) {
					//将file重新赋值为带文件名的
					file = new File(SDCARD_PATH + dir + "/" + fileName);
				}
			}
			else {
				file = new File(SDCARD_PATH + dir + "/" + fileName);
			}
		} 
		else {
			file = new File(SDCARD_PATH + fileName);
		}
		if(file.exists()) {
			return -1;
		} 
		else {
			try {
				if(file.createNewFile()) {
					return 1;
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		return 0;
	}
	
	//往指定目录、指定文件中写入String对象
	public void writeString2File(String dir , String fileName, String src) {
		FileOutputStream out = null;
		if(createNewFile(dir,fileName) != 0)
		{
			try {
				out = new FileOutputStream(file);
				out.write(src.getBytes());
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} finally {
				try {
					if(createNewFile(dir,fileName) != 0)
						out.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
		}
	}
	
	//读指定目录、指定文件中的String数据
	public String readFile(String dir, String fileName) {
		FileInputStream input = null;
		byte[] buffer = new byte[1024 * 10];//10K
		if(createNewFile(dir, fileName) != 0) {
			try {
				input = new FileInputStream(file);
				input.read(buffer);
				return EncodingUtils.getString(buffer, "UTF-8");
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return null;
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值