关于Android编程中对于文件的读写的一些小程序
2014-03-05 14:34
393 查看
以下save()为我转载,忘记出处,如有侵权,联系我,我会即刻删除!
read(int)函数为我参照后的重写用来读取指定行的内容
readLines()函数用来读取文件的行数
//data中文件的读取与写入
private void save() { //写入
String content = editText.getText().toString()+"\n";
try {
/* 根据用户提供的文件名,以及文件的应用模式,打开一个输出流.文件不存系统会为你创建一个的,
* 至于为什么这个地方还有FileNotFoundException抛出,我也比较纳闷。在Context中是这样定义的
* public abstract FileOutputStream openFileOutput(String name, int mode)
* throws FileNotFoundException;
* openFileOutput(String name, int mode);
* 第一个参数,代表文件名称,注意这里的文件名称不能包括任何的/或者/这种分隔符,只能是文件名
* 该文件会被保存在/data/data/应用名称/files/chenzheng_java.txt
* 第二个参数,代表文件的操作模式
* MODE_PRIVATE 私有(只能创建它的应用访问) 重复写入时会文件覆盖
* MODE_APPEND 私有 重复写入时会在文件的末尾进行追加,而不是覆盖掉原来的文件
* MODE_WORLD_READABLE 公用 可读
* MODE_WORLD_WRITEABLE 公用 可读写
* */
FileOutputStream outputStream = openFileOutput("b.txt",
Activity.MODE_APPEND);
outputStream.write(content.getBytes());
outputStream.flush();
outputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* @author chenzheng_java 此为原作者 但我做了较大的改动
* 读取刚才用户保存的内容
*/
private void read(int lineNumber) {
int count = 1;
String line;
String content = "";
try {
FileInputStream inputStream = this.openFileInput("b.txt");
InputStreamReader isr = new InputStreamReader(inputStream,"UTF-8");
BufferedReader br = new BufferedReader(isr);
for(; count <= lineNumber && (line = br.readLine()) != null; count++)
content = line.toString();
inputStream.close();
isr.close();
br.close();
text.setText(content);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private int readLines(){
int i = 0;
String line = "";
try {
FileInputStream inputStream = this.openFileInput("b.txt");
InputStreamReader isr = new InputStreamReader(inputStream,"UTF-8");
BufferedReader br = new BufferedReader(isr);
while((line = br.readLine()) != null)
i++;
inputStream.close();
isr.close();
br.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return i;
}
read(int)函数为我参照后的重写用来读取指定行的内容
readLines()函数用来读取文件的行数
//data中文件的读取与写入
private void save() { //写入
String content = editText.getText().toString()+"\n";
try {
/* 根据用户提供的文件名,以及文件的应用模式,打开一个输出流.文件不存系统会为你创建一个的,
* 至于为什么这个地方还有FileNotFoundException抛出,我也比较纳闷。在Context中是这样定义的
* public abstract FileOutputStream openFileOutput(String name, int mode)
* throws FileNotFoundException;
* openFileOutput(String name, int mode);
* 第一个参数,代表文件名称,注意这里的文件名称不能包括任何的/或者/这种分隔符,只能是文件名
* 该文件会被保存在/data/data/应用名称/files/chenzheng_java.txt
* 第二个参数,代表文件的操作模式
* MODE_PRIVATE 私有(只能创建它的应用访问) 重复写入时会文件覆盖
* MODE_APPEND 私有 重复写入时会在文件的末尾进行追加,而不是覆盖掉原来的文件
* MODE_WORLD_READABLE 公用 可读
* MODE_WORLD_WRITEABLE 公用 可读写
* */
FileOutputStream outputStream = openFileOutput("b.txt",
Activity.MODE_APPEND);
outputStream.write(content.getBytes());
outputStream.flush();
outputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* @author chenzheng_java 此为原作者 但我做了较大的改动
* 读取刚才用户保存的内容
*/
private void read(int lineNumber) {
int count = 1;
String line;
String content = "";
try {
FileInputStream inputStream = this.openFileInput("b.txt");
InputStreamReader isr = new InputStreamReader(inputStream,"UTF-8");
BufferedReader br = new BufferedReader(isr);
for(; count <= lineNumber && (line = br.readLine()) != null; count++)
content = line.toString();
inputStream.close();
isr.close();
br.close();
text.setText(content);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private int readLines(){
int i = 0;
String line = "";
try {
FileInputStream inputStream = this.openFileInput("b.txt");
InputStreamReader isr = new InputStreamReader(inputStream,"UTF-8");
BufferedReader br = new BufferedReader(isr);
while((line = br.readLine()) != null)
i++;
inputStream.close();
isr.close();
br.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return i;
}
相关文章推荐
- Vbscript 脚本编程3 关于文件的读写
- Android程序:程序中的文件读写实例
- 关于windows程序相关基础技术的一点小结(1.文件读写篇)
- 关于android里的文件创建及读写问题
- 一个关于文件读写的小程序
- 关于android的mk文件的一些见解
- Android中关于JNI 的学习(二)对于JNI方法名,数据类型和方法签名的一些认识
- 关于Android退出程序清空私密文件的那些事
- 关于Android编程文件路径中含有中文字符的问题解决办法
- 关于Android程序崩溃的一些记录
- 关于android里的文件创建及读写问题
- 关于android编程的curso对象 moveToNext()方法的一些理解
- 编程总结:Android 读写文件
- 关于android USB Host编程的一些总结
- Android编程之文件读写操作与技巧总结【经典收藏】
- Android中关于JNI 的学习(一)对于JNIEnv的一些认识
- Visual Studio 2017中使用正则修改部分内容 如何使用ILAsm与ILDasm修改.Net exe(dll)文件 C#学习-图解教程(1):格式化数字字符串 小程序开发之图片转Base64(C#、.Net) jquery遍历table为每一个单元格取值及赋值 。net加密解密相关方法 .net关于坐标之间一些简单操作
- Android编程之在SD卡上进行文件读写操作实例详解
- 关于android USB Host编程的一些总结