Android数据处理之File数据处理

Assets中的数据处理


Assets目录的文件位置

assets目录位于项目文件下,其与java、res目录是平级的关系,我们可以在assets目录下放置我们需要的资源文件。

 

对资源文件的读取

  • 通过AssetManager读取

context.getAsset().open(fileName)

  • 通过文件路径读取

file:///android_asset/fileName

使用文件路径读取资源,这种方法一般用于WebView来读取资源

通过WebView的loadUrl方法对资源进行读取

private WebView webView;
webView.loadUrl("file:///android_asset/index.html");

 

示例

创建一个Button和TextView,当点击Button,TextView显示Assets目录下文件的内容

private Button mBtnReadAssets;
private TextView mTvContent;
@Override
protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.activity_main);
	//对控件进行初始化
	initViews();
}

private void initViews() {
	mBtnReadAssets=(Button) findViewById(R.id.btnReadAssets);
	mTvContent=(TextView) findViewById(R.id.tvAssetsContent);
	//给Button设置点击事件
	mBtnReadAssets.setOnClickListener(new OnClickListener() {		
		@Override
		public void onClick(View v) {
			//读取Assets的内容
			String result=readAssetsContent("file.txt");
			mTvContent.setText(result);
		}
	});
}
	
protected String readAssetsContent(String fileName) {
	InputStream fis=null;
	BufferedReader reader=null;
	try {
		fis = this.getAssets().open(fileName);
		reader=new BufferedReader(new InputStreamReader(fis));
		//用于字符串的拼接
		StringBuilder builder=new StringBuilder();
		//读取某一行
		String line;
		while((line=reader.readLine()) != null){
			builder.append(line);
		}
		return builder.toString();
	} catch (IOException e) {
		e.printStackTrace();
	}finally{
		try {
			if(fis!=null)
				fis.close();
			if(reader!=null)
				reader.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	return null;
}

 

注意事项与总结

  • assets中文件不会被编译
  • 占用APK体积
  • 应用场景:App中用到的静态文件,如证书等

 

 

内部存储


简述与分析

  • 内部存储不等于内存
  • 特点:只能被自己应用访问到,空间有限
  • 存储位置:/data/data/<package_name>/files
  • 操作模式:MODE_PRIVATE & MODE_APPEND

MODE_PRIVATE是比较常用的,只有自己的应用能够访问到,并且每次写都是覆盖的

MODE_APPEND每次写的时候都是从末尾追加的。

 

例子

将数据存储到内部文件中,从内部文件中读取数据

布局文件

<EditText 
    android:id="@+id/etNote"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>
<Button 
    android:id="@+id/btnSave"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="保存到内部存储"/>
<Button
    android:id="@+id/btnRead" 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="读取内部存储内容"/>
<TextView 
    android:id="@+id/tvContent"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

java文件

private EditText mEtNode;
private Button mBtnSave,mBtnRead;
private TextView mTvContent;
@Override
protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.activity_inner_file);
	//初始化控件
	initViews();
}
private void initViews() {
	mEtNode=(EditText) findViewById(R.id.etNote);
	mBtnSave=(Button) findViewById(R.id.btnSave);
	mBtnRead=(Button) findViewById(R.id.btnRead);
	mTvContent=(TextView) findViewById(R.id.tvContent);
	//实现保存的功能
	mBtnSave.setOnClickListener(new OnClickListener() {
		@Override
		public void onClick(View arg0) {
			String content = mEtNode.getText().toString().trim();
			boolean success= saveToInternalFile("mynote.txt",content);
			if(success){
				Toast.makeText(InnerFIleActivity.this, "保存到内部存储成功", Toast.LENGTH_SHORT).show();
			}else {
				Toast.makeText(InnerFIleActivity.this, "保存到内部存储失败", Toast.LENGTH_SHORT).show();
			}
		}
	});
	//实现读取内部存储功能
	mBtnRead.setOnClickListener(new OnClickListener() {
		@Override
		public void onClick(View arg0) {
			String content=readFromInternalFile("mynote.txt");
			mTvContent.setText(content);
		}
	});
}
protected String readFromInternalFile(String fileName) {
	FileInputStream fis=null;
	BufferedReader reader=null;
	try {
		fis = this.openFileInput(fileName);
		reader=new BufferedReader(new InputStreamReader(fis));
		//用于拼接结果的字符串
		StringBuilder builder=new StringBuilder();
		String line;
		while((line=reader.readLine())!=null){
			builder.append(line);
		}
		return builder.toString();
	} catch (FileNotFoundException e) {
		e.printStackTrace();
	} catch (IOException e) {
		e.printStackTrace();
	}finally{
		try {
			if(fis!=null)
				fis.close();
			if(reader!=null)
				reader.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	return null;
}
protected boolean saveToInternalFile(String fileName, String content) {
	if(fileName==null||fileName.length()==0||content==null||content.length()==0){
		return false;
	}
	FileOutputStream fos=null;
	try {
		fos = this.openFileOutput(fileName,MODE_PRIVATE);
		fos.write(content.getBytes());
		return true;
	} catch (FileNotFoundException e) {
		e.printStackTrace();
	} catch (IOException e) {
		e.printStackTrace();
	}finally{
		try {
			if(fos!=null){
				fos.close();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	return false;
}

 

 

外部存储


简述与分析

  • 特点:即可公有又可私有,空间相对充足
  • 存储位置:
    • 公有:Environment.getExternalStorageDirectory()
    • 私有:/Android/data/<package_name>/files

 

因为外部存储是各个应用程序都可以访问到的,设及到一些隐私的权限,因此外部文件的存储一定需要相关的权限。

在manifest文件下进行权限的申请

<!-- 读取外部的文件存储的权限 -->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<!-- 写入文件权限 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值