求一段Android数据存储之文件存储数据的测试代码?

小黄鸭和大树 2013-03-26 11:06:19
加精
我知道文件存储方式是一种较常用的方法,在Android中读取/写入文件的方法,与Java中实现I/O的程序是完全一样的,写数据到外部存储器,想求一段Android数据存储之文件存储数据的测试代码,谢谢大家。
...全文
920 24 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
24 条回复
切换为时间正序
请发表友善的回复…
发表回复
ZHAIWEIJIE00 2013-04-06
  • 打赏
  • 举报
回复
支持 支持 !
深夜深几许 2013-04-05
  • 打赏
  • 举报
回复

package com.joboevan.push.tool;

import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import android.content.Context;

/**
 * 书写一个写入内部文件的类
 * 
 * @author lw
 * 
 */
public class MyWriter {

	private BufferedWriter writer;

	public MyWriter(Context activity, String fileName, boolean isApp) {
		try {
			FileOutputStream os = null;
			if (isApp) {
				os = activity.openFileOutput(fileName,
						Context.MODE_WORLD_READABLE
								+ Context.MODE_WORLD_WRITEABLE
								+ Context.MODE_APPEND);
			} else {
				os = activity.openFileOutput(fileName,
						Context.MODE_WORLD_READABLE
								+ Context.MODE_WORLD_WRITEABLE);
			}
			writer = new BufferedWriter(new OutputStreamWriter(os));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}

	// 书写信息
	public void writerMsg(String msg) {
		if (writer == null) {
			return;
		}
		try {
			writer.write(msg);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				writer.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}

JSON存储,调用方式

public static void setJSONValue(Context context, String key, String value) {
		String strValue = new MyReader(context, "refresh.temp").readerMsg();
		// 构建JSON
		JSONObject jsonObject = null;
		if (strValue == null) {
			jsonObject = new JSONObject();
		} else {
			try {
				jsonObject = new JSONObject(strValue);
			} catch (JSONException e) {
				jsonObject = new JSONObject();
			}
		}
		// 储存当前的值
		try {
			if (value == null && key == null) {
				new MyWriter(context, "refresh.temp", false).writerMsg("");
			} else {
				if (value == null)
					jsonObject.remove(key);
				else
					jsonObject.put(key, value);
				String jsoStr = jsonObject.toString();
				new MyWriter(context, "refresh.temp", false).writerMsg(jsoStr);
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}
u010115572 2013-04-01
  • 打赏
  • 举报
回复
学到新知识了,谢谢!
xkry353 2013-03-31
  • 打赏
  • 举报
回复
新人求师父..
u010111468 2013-03-31
  • 打赏
  • 举报
回复
学习,先做好功课
u010107936 2013-03-31
  • 打赏
  • 举报
回复
学习的,技术都很好啊
bileeok 2013-03-29
  • 打赏
  • 举报
回复
zting125240 2013-03-29
  • 打赏
  • 举报
回复
当然还要检查一下剩余空间是否充足。StatFs statFs =new StatFs(sdPath); long blocSize = statfs.getBlockSize(); long availaBlock = statfs.getAvailableBlocks();
gm19910513 2013-03-28
  • 打赏
  • 举报
回复
支持,谢谢,正要用
u010063171 2013-03-28
  • 打赏
  • 举报
回复
yanhelp2 2013-03-28
  • 打赏
  • 举报
回复
CRRedist2008_x86.msiCRRedist2008_x86.msiCRRedist2008_x86.msiCRRedist2008_x86.msiCRRedist2008_x86.msi
abchange 2013-03-27
  • 打赏
  • 举报
回复
多向楼主学习
u010049176 2013-03-27
  • 打赏
  • 举报
回复
新人同学习中……
tianxiameng2 2013-03-27
  • 打赏
  • 举报
回复
新人报道! 同学习中。
u010048325 2013-03-26
  • 打赏
  • 举报
回复
新人报道了了
nutxzycftqg 2013-03-26
  • 打赏
  • 举报
回复
可以用下面的代码调试: public void writeToExternalStoragePublic(String filename, int content) { String packageName = this.getPackageName(); String path = Environment.getExternalStorageDirectory().getAbsolutePath()+ "/Android/data/" + packageName + "/files/"; if (isExternalStorageAvailable() && !isExternalStorageReadOnly()) { try { boolean exists = (new File(path)).exists(); if (!exists) { new File(path).mkdirs(); } FileOutputStream fOut = new FileOutputStream(path + filename,true); fOut.write((Integer.valueOf(content).toString() + " ").getBytes()); fOut.write((Integer.valueOf(content).toString() + " ").getBytes()); fOut.flush(); fOut.close(); } catch (IOException e) { e.printStackTrace(); } } }
Jan_concise 2013-03-26
  • 打赏
  • 举报
回复
支持分享代码。
meng110120130 2013-03-26
  • 打赏
  • 举报
回复
支持分享代码。
line_us 2013-03-26
  • 打赏
  • 举报
回复
支持分享代码。
怀念家驹Y 2013-03-26
  • 打赏
  • 举报
回复
紧急回复!!!
加载更多回复(4)

567

社区成员

发帖
与我相关
我的任务
社区描述
英特尔® 边缘计算,聚焦于边缘计算、AI、IoT等领域,为开发者提供丰富的开发资源、创新技术、解决方案与行业活动。
社区管理员
  • 英特尔技术社区
  • shere_lin
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧