Android使用文件保存数据,采用比特流保存(解决“\n”无效)

在做项目的时候经常会遇到保存文件到SD卡的问题,这次介绍一种采用比特流的方式保存数据

操作SD卡别忘了添加下权限

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


写个文件操作类DTFileHelper

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

import android.os.Environment;

public class DTFileHelper {
	/** SD卡是否存在**/ 
	private boolean hasSD = false; 
	/** SD卡的路径**/ 
	private String SDPATH; 
	/** 文件名	 */
	private String fileName = "dtNetLog.txt";
	public DTFileHelper() { 
		hasSD = Environment.getExternalStorageState().equals( 
				android.os.Environment.MEDIA_MOUNTED); 
		SDPATH = Environment.getExternalStorageDirectory().getPath(); 
	} 
	/** 
	 * 在SD卡上创建文件 
	 * 
	 * @throws IOException 
	 */ 
	public File createSDFile() throws IOException { 
		File file = new File(SDPATH + "//" + fileName); 
		if (!file.exists()) { 
			file.createNewFile(); 
		} 
		return file; 
	} 
	/** 
	 * 删除SD卡上的文件 
	 * 
	 * @param fileName 
	 */ 
	public boolean deleteSDFile() { 
		File file = new File(SDPATH + "//" + fileName); 
		if (file == null || !file.exists() || file.isDirectory()) 
			return false; 
		return file.delete(); 
	} 
	/** 
	 * 写入内容到SD卡中的txt文本中 
	 * str为内容 
	 */ 
	public boolean writeSDFile( String content) {

		// 判断是否有sd卡
		if (!hasSD) {
			return false;
		}
		String tmp = readSDFile()+content;
		FileOutputStream fileOutputStream = null;
		File file = new File(Environment.getExternalStorageDirectory(),
				fileName);
		try {
			fileOutputStream = new FileOutputStream(file);
			fileOutputStream.write(tmp.getBytes());
			return true;
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {

				if (fileOutputStream != null) {
					fileOutputStream.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return false;
	}
	/** 
	 * 读取SD卡中文本文件 
	 * 
	 * @param fileName 
	 * @return 
	 */ 
	public String readSDFile() {
		// 判断是否有sd卡
		if (!hasSD) {
			return "";
		}
		FileInputStream fileInputStream = null;
		File file = new File(Environment.getExternalStorageDirectory(),
				fileName);
		try {
			fileInputStream = new FileInputStream(file);
			int len = 0;
			byte[] buffer = new byte[1024];
			ByteArrayOutputStream byteArrayInputStream = new ByteArrayOutputStream();
			while ((len = fileInputStream.read(buffer)) != -1) {
				byteArrayInputStream.write(buffer, 0, len);
			}
			String string = new String(byteArrayInputStream.toByteArray());
			return string;
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (fileInputStream != null) {
				try {
					fileInputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}

		return null;
	}
}


这里根据自己的需求调用对应的方法就可以了,用法我就不给了。


这里我在使用的时候遇到一个问题:我是把数据保存为txt文件,用来看里面的内容。在保存的数据中有回车("\n")在保存的时候没了,取出来的数据全挤在一起;

解决方法:保存数据的时候使用"\r\n"代替"\n",这样的到的文件就有回车了!!!


以上有错误之处欢迎指出,谢谢!!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值