android开发 - 文件的操作
2015-04-01 21:30
155 查看
数据的存储和访问
很多时候我们开发的软件需要对处理后的数据进行存储,以供再次访问,android为数据存储提供如下几种方式
1. 文件
2. SharedPreferences(参数)
3. SQLite数据库
4. 内容提供者(Content Provider)
5. 网络
Android存储分为三个地方
1. 自带的存储空间 一般比较小
2. 外存储设备(SDCard) 一般比较大
写一个简单的程序,把用户输入的文件名和,文件内容一起写到手机内存中,使用业务类
FileService service = new FileService();
service.save(filename,filecontent);
//使用吐司提示
Toast.makeText(getApplicationContext(),R.string.success,1).show();
import android.content.Context;
public class FileService {
private Context context;
public FileService(Context context){
this.context=context;
}
/**
* 保存文件
* @param filename 文件名
* @param content 文件内容
*/
public void save(String filename,String content) throws Exception {
//私有的操作模式,创建出来的文件只能被应用访问,其它应用不能文件该文件,另外采用私有操作模式创建的文件回复该之前的内容
//IO 特别简单,就是要么输入流,要么输出流
//输出流:就需要转二进制输出,最后关闭
//输入流:将二进制数据转成字符串,最后关闭
//做IO操作的时候可能有异常,所以把异常给上一级去处理,那么这里就直接抛异常就可以了
//这种应用默认保存在安卓机的data文件夹下,的指定包下的files文件夹
FileOutputStream outStream = context.openFileOutput(filename, Context.MODE_PRIVATE);
outStream.write(content.getBytes());
outStream.close();
}
public String open(String filename)throws Exception{
FileInputStream inputStream = context.openFileInput(filename);
ByteArrayOutputStream bout = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len=0;
while((len = inputStream.read(buffer))!= -1){
bout.write(buffer,0,len);
}
byte[] data = bout.toByteArray();
return new String(data);
}
}
IO的操作模式 四中操作模式
Context.MODE_PRIVATE
默认的,也叫私有的操作模式,也就是只能当前应用可以 访问 ,操作 ,其它程序访问,会被拒绝
Context.MODE_APPEND
检查文件是否存在,如果存在就追加内容,否则就创建
MODE_WORLD_READABLE 表示当前文件可以被其它应用程序读取
MODE_WORLD_WRITEABLE
表示当前文件可以被其它应用写入,如果希望文件被其它 应用读和写,可以传入:
openFileOutput("itcast.txt",Context.MODE_WORLD_READABLE+Context.MODE_WORLD_WRITEABLE);
如果需要读写操作,就可以把他们加起来
安卓有一套自己的安全模型,当应用程序(.apk)在安装时系统就会分配给他一个userid,当该应用要去访问其它资源,比如文件的时候,就需要userid匹配,默认情况下,任何应用创建的文件,sharedpreferences,数据库都应该是私有的(位于/data/data/<package name>/files),其它程序无法访问,除非在创建时指定了Context.MODE_WORLD_READABLE或者Context.MODE_WORLD_WRITEABLE,只有这样其它程序才能正确访问
以上讲的都是内部存储
安卓文件管理分为两种
外部存储 external storage
内部存储 internal storage
两个API
Activity还提供了getCacheDir()和getFilesDir()方法
getCacheDir()方法用于获取/data/data/<package name>/cache目录
getFilesDir()方法用于获取/data/data/<package name>/files目录
以下为MainActivity下代码
package com.cnnote.filestore;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button savebtn = (Button)this.findViewById(R.id.btnsave);
savebtn.setOnClickListener(new ButtonOnClick());
Button lookbtn = (Button)this.findViewById(R.id.btnlook);
lookbtn.setOnClickListener(new ButtonLookOnClick());
}
private final class ButtonLookOnClick implements View.OnClickListener{
public void onClick(View v){
FileService service = new FileService(getApplicationContext());
EditText tt = (EditText)findViewById(R.id.filename);
String ets = tt.getText().toString();
try {
String str = service.open(ets);
Toast.makeText(getApplicationContext(), str, Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
}
}
}
private final class ButtonOnClick implements View.OnClickListener {
public void onClick(View v){
FileService service = new FileService(MainActivity.this);
try {
EditText filename = (EditText)findViewById(R.id.filename);
EditText filecontent = (EditText)findViewById(R.id.filecontent);
String filen = filename.getText().toString();
String filec = filecontent.getText().toString();
//service.save(filen,filec);
//service.saveOtherRead(filen, filec);
service.saveOtherWriteAndRead(filen, filec);
Toast.makeText(getApplicationContext(), R.string.success, Toast.LENGTH_SHORT).show();
} catch (Exception e) {
//Toast.makeText(getApplicationContext(), R.string.failed, Toast.LENGTH_LONG).show();
Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
}
}
}
}
以下为我们代码写入文件
package com.cnnote.filestore;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import android.content.Context;
public class FileService {
public Context context;
public FileService(Context context){
this.context=context;
}
/**
* 保存文件
* @param filename 文件名
* @param content 文件内容
*/
public void save(String filename,String content) throws Exception {
//私有的操作模式,创建出来的文件只能被应用访问,其它应用不能文件该文件,另外采用私有操作模式创建的文件回复该之前的内容
//IO 特别简单,就是要么输入流,要么输出流
//输出流:就需要转二进制输出,最后关闭
//输入流:将二进制数据转成字符串,最后关闭
//做IO操作的时候可能有异常,所以把异常给上一级去处理,那么这里就直接抛异常就可以了
//这种应用默认保存在安卓机的data文件夹下,的指定包下的files文件夹
FileOutputStream outStream = context.openFileOutput(filename, Context.MODE_APPEND);
outStream.write(content.getBytes());
outStream.close();
}
public void saveOtherRead(String filename,String content) throws Exception{
FileOutputStream output = context.openFileOutput(filename, Context.MODE_WORLD_READABLE);
output.write(content.getBytes());
output.close();
}
public void saveOtherWriteAndRead(String filename,String content) throws Exception{
FileOutputStream output = context.openFileOutput(filename,Context.MODE_WORLD_READABLE+Context.MODE_WORLD_WRITEABLE);
output.write(content.getBytes());
output.close();
}
public String open(String filename) throws Exception{
FileInputStream inputStream = context.openFileInput(filename);
ByteArrayOutputStream bout = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len=0;
while((len = inputStream.read(buffer))!= -1){
bout.write(buffer,0,len);
}
byte[] data = bout.toByteArray();
return new String(data);
}
}
以下为测试
package cn.cnnote.other;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import android.test.AndroidTestCase;
import android.util.Log;
public class testOther extends AndroidTestCase {
/**
* 测试是否可以访问,操作模式设为 MODE_PRIVATE 结果拒绝
* @throws Exception
*/
public void testAccessPrivate() throws Exception {
String path = "/data/data/com.cnnote.filestore/files/aaa.txt";
File file = new File(path);
ByteArrayOutputStream memory = new ByteArrayOutputStream();
FileInputStream inputStream = new FileInputStream(file);
byte[] buffer = new byte[1024];
int len = 0;
while ((len = inputStream.read(buffer)) != -1) {
memory.write(buffer, 0, len);
}
byte[] data = memory.toByteArray();
memory.close();
inputStream.close();
String content = new String(data);
Log.i("Tag", content);
}
/**
* 测试操作是否可以访问, 操作模式设为 MODE_APPEND 结果拒绝
* @throws Exception
*/
public void testAccessP() throws Exception{
String path="/data/data/com.cnnote.filestore/files/appen.txt";
File file = new File(path);
ByteArrayOutputStream memory = new ByteArrayOutputStream();
FileInputStream input = new FileInputStream(file);
byte[] buffer = new byte[1024];
int len=0;
while((len=input.read(buffer))!=-1){
memory.write(buffer,0,len);
}
byte[] reby = memory.toByteArray();
String str = new String(reby);
Log.i("Tag",str);
}
/**
* 测试是否可以写入信息,操作模式设为 MODE_WORLD_WRITEABLE 结果可以
* @throws Exception
*/
public void testWriteable() throws Exception{
String path = "/data/data/com.cnnote.filestore/files/appen.txt";
File file = new File(path);
FileOutputStream files = new FileOutputStream(file);
files.write("这是一个测试追加文件,是否可最佳!".getBytes());
files.close();
}
/**
* 测试是否可以写入并且读取,操作模式设为 MODE_WORLD_WRITEABLE + MODE_WORLD_READABLE 结果可以
* @throws Exception
*/
public void testWriteAndRead()throws Exception{
String path = "/data/data/com.cnnote.filestore/files/wr.txt";
File file = new File(path);
FileOutputStream files = new FileOutputStream(file);
files.write("现在追加写入数据,".getBytes());
files.close();
File file1 = new File(path);
ByteArrayOutputStream memory = new ByteArrayOutputStream();
FileInputStream fis = new FileInputStream(file1);
byte[] buffer = new byte[1024];
int len=0;
while((len=fis.read(buffer))!=-1){
memory.write(buffer,0,len);
}
byte[] b = memory.toByteArray();
String str = new String(b);
System.out.println(str);
}
}
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import android.content.Context;
public class FileService {
public Context context;
public FileService(Context context){
this.context=context;
}
/**
* 保存文件
* @param filename 文件名
* @param content 文件内容
*/
public void save(String filename,String content) throws Exception {
//私有的操作模式,创建出来的文件只能被应用访问,其它应用不能文件该文件,另外采用私有操作模式创建的文件回复该之前的内容
//IO 特别简单,就是要么输入流,要么输出流
//输出流:就需要转二进制输出,最后关闭
//输入流:将二进制数据转成字符串,最后关闭
//做IO操作的时候可能有异常,所以把异常给上一级去处理,那么这里就直接抛异常就可以了
//这种应用默认保存在安卓机的data文件夹下,的指定包下的files文件夹
FileOutputStream outStream = context.openFileOutput(filename, Context.MODE_PRIVATE);
outStream.write(content.getBytes());
outStream.close();
}
public String open(String filename)throws Exception{
FileInputStream inputStream = context.openFileInput(filename);
ByteArrayOutputStream bout = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len=0;
while((len = inputStream.read(buffer))!= -1){
bout.write(buffer,0,len);
}
byte[] data = bout.toByteArray();
return new String(data);
}
}
很多时候我们开发的软件需要对处理后的数据进行存储,以供再次访问,android为数据存储提供如下几种方式
1. 文件
2. SharedPreferences(参数)
3. SQLite数据库
4. 内容提供者(Content Provider)
5. 网络
Android存储分为三个地方
1. 自带的存储空间 一般比较小
2. 外存储设备(SDCard) 一般比较大
写一个简单的程序,把用户输入的文件名和,文件内容一起写到手机内存中,使用业务类
FileService service = new FileService();
service.save(filename,filecontent);
//使用吐司提示
Toast.makeText(getApplicationContext(),R.string.success,1).show();
import android.content.Context;
public class FileService {
private Context context;
public FileService(Context context){
this.context=context;
}
/**
* 保存文件
* @param filename 文件名
* @param content 文件内容
*/
public void save(String filename,String content) throws Exception {
//私有的操作模式,创建出来的文件只能被应用访问,其它应用不能文件该文件,另外采用私有操作模式创建的文件回复该之前的内容
//IO 特别简单,就是要么输入流,要么输出流
//输出流:就需要转二进制输出,最后关闭
//输入流:将二进制数据转成字符串,最后关闭
//做IO操作的时候可能有异常,所以把异常给上一级去处理,那么这里就直接抛异常就可以了
//这种应用默认保存在安卓机的data文件夹下,的指定包下的files文件夹
FileOutputStream outStream = context.openFileOutput(filename, Context.MODE_PRIVATE);
outStream.write(content.getBytes());
outStream.close();
}
public String open(String filename)throws Exception{
FileInputStream inputStream = context.openFileInput(filename);
ByteArrayOutputStream bout = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len=0;
while((len = inputStream.read(buffer))!= -1){
bout.write(buffer,0,len);
}
byte[] data = bout.toByteArray();
return new String(data);
}
}
IO的操作模式 四中操作模式
Context.MODE_PRIVATE
默认的,也叫私有的操作模式,也就是只能当前应用可以 访问 ,操作 ,其它程序访问,会被拒绝
Context.MODE_APPEND
检查文件是否存在,如果存在就追加内容,否则就创建
MODE_WORLD_READABLE 表示当前文件可以被其它应用程序读取
MODE_WORLD_WRITEABLE
表示当前文件可以被其它应用写入,如果希望文件被其它 应用读和写,可以传入:
openFileOutput("itcast.txt",Context.MODE_WORLD_READABLE+Context.MODE_WORLD_WRITEABLE);
如果需要读写操作,就可以把他们加起来
安卓有一套自己的安全模型,当应用程序(.apk)在安装时系统就会分配给他一个userid,当该应用要去访问其它资源,比如文件的时候,就需要userid匹配,默认情况下,任何应用创建的文件,sharedpreferences,数据库都应该是私有的(位于/data/data/<package name>/files),其它程序无法访问,除非在创建时指定了Context.MODE_WORLD_READABLE或者Context.MODE_WORLD_WRITEABLE,只有这样其它程序才能正确访问
以上讲的都是内部存储
安卓文件管理分为两种
外部存储 external storage
内部存储 internal storage
两个API
Activity还提供了getCacheDir()和getFilesDir()方法
getCacheDir()方法用于获取/data/data/<package name>/cache目录
getFilesDir()方法用于获取/data/data/<package name>/files目录
以下为MainActivity下代码
package com.cnnote.filestore;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button savebtn = (Button)this.findViewById(R.id.btnsave);
savebtn.setOnClickListener(new ButtonOnClick());
Button lookbtn = (Button)this.findViewById(R.id.btnlook);
lookbtn.setOnClickListener(new ButtonLookOnClick());
}
private final class ButtonLookOnClick implements View.OnClickListener{
public void onClick(View v){
FileService service = new FileService(getApplicationContext());
EditText tt = (EditText)findViewById(R.id.filename);
String ets = tt.getText().toString();
try {
String str = service.open(ets);
Toast.makeText(getApplicationContext(), str, Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
}
}
}
private final class ButtonOnClick implements View.OnClickListener {
public void onClick(View v){
FileService service = new FileService(MainActivity.this);
try {
EditText filename = (EditText)findViewById(R.id.filename);
EditText filecontent = (EditText)findViewById(R.id.filecontent);
String filen = filename.getText().toString();
String filec = filecontent.getText().toString();
//service.save(filen,filec);
//service.saveOtherRead(filen, filec);
service.saveOtherWriteAndRead(filen, filec);
Toast.makeText(getApplicationContext(), R.string.success, Toast.LENGTH_SHORT).show();
} catch (Exception e) {
//Toast.makeText(getApplicationContext(), R.string.failed, Toast.LENGTH_LONG).show();
Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
}
}
}
}
以下为我们代码写入文件
package com.cnnote.filestore;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import android.content.Context;
public class FileService {
public Context context;
public FileService(Context context){
this.context=context;
}
/**
* 保存文件
* @param filename 文件名
* @param content 文件内容
*/
public void save(String filename,String content) throws Exception {
//私有的操作模式,创建出来的文件只能被应用访问,其它应用不能文件该文件,另外采用私有操作模式创建的文件回复该之前的内容
//IO 特别简单,就是要么输入流,要么输出流
//输出流:就需要转二进制输出,最后关闭
//输入流:将二进制数据转成字符串,最后关闭
//做IO操作的时候可能有异常,所以把异常给上一级去处理,那么这里就直接抛异常就可以了
//这种应用默认保存在安卓机的data文件夹下,的指定包下的files文件夹
FileOutputStream outStream = context.openFileOutput(filename, Context.MODE_APPEND);
outStream.write(content.getBytes());
outStream.close();
}
public void saveOtherRead(String filename,String content) throws Exception{
FileOutputStream output = context.openFileOutput(filename, Context.MODE_WORLD_READABLE);
output.write(content.getBytes());
output.close();
}
public void saveOtherWriteAndRead(String filename,String content) throws Exception{
FileOutputStream output = context.openFileOutput(filename,Context.MODE_WORLD_READABLE+Context.MODE_WORLD_WRITEABLE);
output.write(content.getBytes());
output.close();
}
public String open(String filename) throws Exception{
FileInputStream inputStream = context.openFileInput(filename);
ByteArrayOutputStream bout = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len=0;
while((len = inputStream.read(buffer))!= -1){
bout.write(buffer,0,len);
}
byte[] data = bout.toByteArray();
return new String(data);
}
}
以下为测试
package cn.cnnote.other;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import android.test.AndroidTestCase;
import android.util.Log;
public class testOther extends AndroidTestCase {
/**
* 测试是否可以访问,操作模式设为 MODE_PRIVATE 结果拒绝
* @throws Exception
*/
public void testAccessPrivate() throws Exception {
String path = "/data/data/com.cnnote.filestore/files/aaa.txt";
File file = new File(path);
ByteArrayOutputStream memory = new ByteArrayOutputStream();
FileInputStream inputStream = new FileInputStream(file);
byte[] buffer = new byte[1024];
int len = 0;
while ((len = inputStream.read(buffer)) != -1) {
memory.write(buffer, 0, len);
}
byte[] data = memory.toByteArray();
memory.close();
inputStream.close();
String content = new String(data);
Log.i("Tag", content);
}
/**
* 测试操作是否可以访问, 操作模式设为 MODE_APPEND 结果拒绝
* @throws Exception
*/
public void testAccessP() throws Exception{
String path="/data/data/com.cnnote.filestore/files/appen.txt";
File file = new File(path);
ByteArrayOutputStream memory = new ByteArrayOutputStream();
FileInputStream input = new FileInputStream(file);
byte[] buffer = new byte[1024];
int len=0;
while((len=input.read(buffer))!=-1){
memory.write(buffer,0,len);
}
byte[] reby = memory.toByteArray();
String str = new String(reby);
Log.i("Tag",str);
}
/**
* 测试是否可以写入信息,操作模式设为 MODE_WORLD_WRITEABLE 结果可以
* @throws Exception
*/
public void testWriteable() throws Exception{
String path = "/data/data/com.cnnote.filestore/files/appen.txt";
File file = new File(path);
FileOutputStream files = new FileOutputStream(file);
files.write("这是一个测试追加文件,是否可最佳!".getBytes());
files.close();
}
/**
* 测试是否可以写入并且读取,操作模式设为 MODE_WORLD_WRITEABLE + MODE_WORLD_READABLE 结果可以
* @throws Exception
*/
public void testWriteAndRead()throws Exception{
String path = "/data/data/com.cnnote.filestore/files/wr.txt";
File file = new File(path);
FileOutputStream files = new FileOutputStream(file);
files.write("现在追加写入数据,".getBytes());
files.close();
File file1 = new File(path);
ByteArrayOutputStream memory = new ByteArrayOutputStream();
FileInputStream fis = new FileInputStream(file1);
byte[] buffer = new byte[1024];
int len=0;
while((len=fis.read(buffer))!=-1){
memory.write(buffer,0,len);
}
byte[] b = memory.toByteArray();
String str = new String(b);
System.out.println(str);
}
}
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import android.content.Context;
public class FileService {
public Context context;
public FileService(Context context){
this.context=context;
}
/**
* 保存文件
* @param filename 文件名
* @param content 文件内容
*/
public void save(String filename,String content) throws Exception {
//私有的操作模式,创建出来的文件只能被应用访问,其它应用不能文件该文件,另外采用私有操作模式创建的文件回复该之前的内容
//IO 特别简单,就是要么输入流,要么输出流
//输出流:就需要转二进制输出,最后关闭
//输入流:将二进制数据转成字符串,最后关闭
//做IO操作的时候可能有异常,所以把异常给上一级去处理,那么这里就直接抛异常就可以了
//这种应用默认保存在安卓机的data文件夹下,的指定包下的files文件夹
FileOutputStream outStream = context.openFileOutput(filename, Context.MODE_PRIVATE);
outStream.write(content.getBytes());
outStream.close();
}
public String open(String filename)throws Exception{
FileInputStream inputStream = context.openFileInput(filename);
ByteArrayOutputStream bout = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len=0;
while((len = inputStream.read(buffer))!= -1){
bout.write(buffer,0,len);
}
byte[] data = bout.toByteArray();
return new String(data);
}
}
相关文章推荐
- Android开发之SD卡上文件操作 (转)
- 【转】Android开发之SD卡上文件操作
- android开发学习_文件操作
- Android开发4——文件操作模式
- 【Android开发】 文件读写操作——总结
- Android的NDK开发(5)————Android JNI层实现文件的read、write与seek操作
- Android的NDK开发(5)————Android JNI层实现文件的read、write与seek操作
- Android开发——对sdcard扩展卡文件操作
- Android开发之SD卡上文件操作
- android 手机拍照上传项目的开发-----Camera1.2(图片文件的翻转及缩放操作)
- Android开发之SD卡上文件操作(转载从网络)
- Android开发4——文件操作模式
- Android开发之SD卡上文件操作
- Android的NDK开发(5)————Android JNI层实现文件的read、write与seek操作
- Android开发之SD卡上文件操作
- [120_移动开发Android]005_android开发之数据存储之文件操作
- Android的NDK开发(5)————Android JNI层实现文件的read、write与seek操作
- NDK开发(五):Android JNI层实现文件的read、write与seek操作
- Android开发——对sdcard扩展卡文件操作
- Android开发学习之IO文件读写操作