android续写文件
来源:学生作业帮助网 编辑:作业帮 时间:2024/09/22 21:29:03 体裁作文
篇一:android文件读写
android文件读写
android文件的读写主要分为两个方面,一个是将内容写入本应用的data文件夹中,另一个是将内容写入到sdcard中。两者都使用I/O流的读写技术。
下面具体具体介绍这两方面的内容:
一。将内容写入本应用中:
[html]
/**
* @description:将内容保存到内置存储中
* @author:Administrator
* @return:boolean
* @param fileName
*文件名
* @param fileContent
*文件内容
* @param context
*上下文对象
* @return
*/
public static boolean fileSaveApp(String fileName, String fileContent,
Context context) {
try {
// 用android提供的输出流来将内容写入到文件中,注意mode的用途 FileOutputStream fos = context.openFileOutput(fileName,
context.MODE_APPEND);
fos.write(fileContent.getBytes());
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
/**
* @description:将内容保存到内置存储中
* @author:Administrator
* @return:boolean
* @param fileName
*文件名
* @param fileContent
*文件内容
* @param context
*上下文对象
* @return
*/
public static boolean fileSaveApp(String fileName, String fileContent,
Context context) {
try {
// 用android提供的输出流来将内容写入到文件中,注意mode的用途
FileOutputStream fos = context.openFileOutput(fileName,
context.MODE_APPEND);
fos.write(fileContent.getBytes());
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
注意android的上下文对象context在这个方法中起的的作用(可以用它来打开一个输出流,或者打开一个输入流),还有文件的写入模式。这里用的是文件的追加模式。数据时只可被本应用操作的。
二。将数据写入sdcard中
[html]
/**
* @description:将内容写入到sdcard的文件当中
* @author:Administrator
* @return:boolean
* @param fileName
*文件名
* @param fileContent
*文件内容
* @param path
*文件路径
* @return
*/
public static boolean fileSave(String fileName, String fileContent,
File path) {
File file = new File(path, fileName);
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
FileOutputStream fos = null;
int count = 0;
try {
fos = new FileOutputStream(file);
count = fileContent.getBytes().length;
fos.write(fileContent.getBytes(), 0, count);
fos.close();
return true;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
/**
* @description:将内容写入到sdcard的文件当中
* @author:Administrator
* @return:boolean
* @param fileName
*文件名
* @param fileContent
*文件内容
* @param path
*文件路径
* @return
*/
public static boolean fileSave(String fileName, String fileContent,
File path) {
File file = new File(path, fileName);
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
FileOutputStream fos = null;
int count = 0;
try {
fos = new FileOutputStream(file);
count = fileContent.getBytes().length;
fos.write(fileContent.getBytes(), 0, count);
fos.close();
return true;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
注意这个方法中的path参数指的是
=Environment.getExternalStorageDirectory();
注意写入数据的时候需要判断
Environment.getExternalStorageState().equals( sdcardsdcard的路径:是否存在是否可写:
Environment.MEDIA_MOUNTED);
最后需要注意如果要向sdcard写入数据必须在manifest.xml加入权限:
最后将从应用的data中读取数据的方法也写进来:
[html]
/**
* @description:读取本应用data文件夹中文件的内容(如果要读取sdcard中的文件时一定要去判断sdcard是否存在,并且是可读的)
* @author:Administrator
* @return:String 文件的内容
* @param fileName
*文件名
* @param context
*上下文对象
* @return
*/
public static String readFile(String fileName, Context context) {
String str = "";
try {
FileInputStream fis = context.openFileInput(fileName);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while ((len = fis.read(buffer)) != -1) {
// 用字节输出流将读到的内容写入到内存中
bos.write(buffer, 0, len);
}
// 将输出流中的数据转换为String
str = new String(bos.toByteArray(), "utf-8");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
/**
* @description:读取本应用data文件夹中文件的内容(如果要读取sdcard中的文件时一定要去判断sdcard是否存在,并且是可读的)
* @author:Administrator
* @return:String 文件的内容
* @param fileName
*文件名
篇二:android文件读写
一、概要
1、apk中有两种资源文件,raw下的和assert下的,这些数据只能读取,不能写入。更重要的是该目录下的文件大小不能超过1M。
2、SD卡中的文件使用FileInputStream和FileOutputStream进行文件的操作。
3、存放在数据区(/data/data/..)的文件只能使用openFileOutput和openFileInput进行操作。
注意不能使用FileInputStream和FileOutputStream进行文件的操作。
二、读写方式
1.资源文件(只读)
两种资源文件,使用两种不同的方式进行打开使用。
raw使用InputStream in =
getResources().openRawResource(R.raw.test);
asset使用InputStream in =
getResources().getAssets().open(fileName);
注:在使用InputStream的时候需要在函数名称后加上throws IOException。
2.数据区文件(/data/data/<应用程序名>目录上的文件)
(1)写操作:
FileOutputStream fout =openFileOutput(fileName, MODE_PRIVATE);
(2)读操作:FileInputStream fin = openFileInput(fileName);
(3)写操作中的使用模式:
MODE_APPEND:即向文件尾写入数据
MODE_PRIVATE:即仅打开文件可写入数据
MODE_WORLD_READABLE:所有程序均可读该文件数据 MODE_WORLD_WRITABLE:即所有程序均可写入数据。
3.sdcard数据
(1)读操作
FileInputStream fin = new FileInputStream(fileName);
(2)写操作
FileOutputStream fout = new FileOutputStream(fileName);
(3)必要步骤
①获取权限
A 获取文件修改权限
B可写
②检查内存状态(是否安装sd卡)
if(Environment.getExternalStorageDirectory().equals(Environment.MEDIA_MOUNTED))③读写操作
关于sdcard
注意:访问SDCard必须在AndroidManifest.xml中加入访问SDCard的权限 if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
File sdCardDir =
Environment.getExternalStorageDirectory();//获取SDCard目录 File saveFile = new File(sdCardDir, “a.txt”); FileOutputStream outStream = new FileOutputStream(saveFile);
outStream.write(“test”.getBytes()); outStream.close();
}
Environment.getExternalStorageState()方法用于获取SDCard的状态,如果手机装有SDCard,并且可以进行读写,那么方法返回的状态等于Environment.MEDIA_MOUNTED。
Environment.getExternalStorageDirectory()方法用于获取SDCard的目录,当然要获取SDCard的目录,你也可以这样写:
File sdCardDir = new File(“/sdcard”); //获取SDCard目录
File saveFile = new File(sdCardDir, “itcast.txt”);
上面两句代码可以合成一句:
File saveFile = new File(“/sdcard/a.txt”);
FileInputStream是InputStream的子类
篇三:android-读写文件
[转]Android读写文件
本文转自:http://blog.sina.com.cn/s/blog_4d25c9870100qpax.html
一、 从resource中的raw文件夹中获取文件并读取数据(资源文件只能
读不能写)
String res = "";
try{
InputStream in =
getResources().openRawResource(R.raw.bbi);
//在\Test\res\raw\bbi.txt,
int length = in.available();
byte [] buffer = new byte[length];
in.read(buffer);
//res = EncodingUtils.getString(buffer, "UTF-8"); //res = EncodingUtils.getString(buffer, "UNICODE"); res = EncodingUtils.getString(buffer, "BIG5"); //依bbi.txt的编码类型选择合适的编码,如果不调整会乱码 in.close();
}catch(Exception e){
e.printStackTrace();
}
myTextView.setText(res);//把得到的内容显示在TextView上
二、 从asset中获取文件并读取数据(资源文件只能读不能写) String fileName = "yan.txt"; //文件名字
Stri(来自:WWw.zW2.CN 爱作文网)ng res="";
try{
InputStream in =
getResources().getAssets().open(fileName);
// \Test\assets\yan.txt这里有这样的文件存在
int length = in.available();
byte [] buffer = new byte[length];
in.read(buffer);
res = EncodingUtils.getString(buffer, "UTF-8");}catch(Exception e){
e.printStackTrace();
}
三、 从sdcard中去读文件,首先要把文件通过
\android-sdk-windows\tools\adb.exe把本地计算机上的文件copy到sdcard上去,adb.exe push e:/Y.txt /sdcard/,不可以用adb.exe push e:\Y.txt\sdcard\同样: 把仿真器上
的文件copy到本地计算机上用: adb
pull ./data/data/com.tt/files/Test.txt e:/
String fileName = "/sdcard/Y.txt";
//也可以用String fileName = "mnt/sdcard/Y.txt";
String res="";
try{
FileInputStream fin = new FileInputStream(fileName); //FileInputStream fin = openFileInput(fileName);//用这个就不行了,必须用FileInputStream
int length = fin.available();
byte [] buffer = new byte[length];
fin.read(buffer);
res = EncodingUtils.getString(buffer, "UTF-8");fin.close();
}catch(Exception e){
e.printStackTrace();
}
myTextView.setText(res);
四、 写文件, 一般写在\data\data\com.test\files\里面,打开DDMS
查看file explorer是可以看到仿真器文件存放目录的结构的
String fileName = "TEST.txt";
String message = "FFFFFFF11111FFFFF" ;
writeFileData(fileName, message);
public voidwriteFileData(String fileName,String message){
try{
FileOutputStream fout =openFileOutput(fileName, MODE_PRIVATE);
byte [] bytes = message.getBytes();
fout.write(bytes);
fout.close();
}
catch(Exception e){
e.printStackTrace();
}
}
五、 写, 读data/data/目录(相当AP工作目录)上的文件,用
openFileOutput
//写文件在./data/data/com.tt/files/下面
public voidwriteFileData(String fileName,String message){
try{
FileOutputStream fout =openFileOutput(fileName, MODE_PRIVATE);
byte [] bytes = message.getBytes();
fout.write(bytes);
fout.close();
}
catch(Exception e){
e.printStackTrace();
}
}
//-------------------------------------------------------
//读文件在./data/data/com.tt/files/下面
public String readFileData(String fileName){
String res="";
try{
FileInputStream fin = openFileInput(fileName); int length = fin.available();
byte [] buffer = new byte[length];
fin.read(buffer);
res = EncodingUtils.getString(buffer, "UTF-8"); fin.close();
}
catch(Exception e){
e.printStackTrace();
体裁作文