Android 程序私有文件(TXT)操作类
来源:互联网 发布:明基显示器知乎 编辑:程序博客网 时间:2024/05/25 05:38
package com.ltc.datastorage.text;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import android.content.Context;
/**
* Android私有TXT文件操作类
* @author LTC
*
*/
public class TextManager {
private Context context;
private String fileName;
/**
* 构造函数
* @param context
* @param fileName
*/
public TextManager(Context context, String fileName){
this.context = context;
this.fileName = fileName;
}
/**
* 判断文件是否存在
* @return
*/
public boolean isExist(){
String [] fileNameArray = context.fileList();
for(int i = 0; i< fileNameArray.length; i++){
if(fileNameArray[i].equals(fileName)){
return true;
}
}
return false;
}
/**
* 创建文件
* @return
*/
public boolean create(){
boolean result = false;
if(!isExist()){
FileOutputStream fos = null;
try {
fos = context.openFileOutput(fileName, Context.MODE_PRIVATE);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
if(fos != null){
result = true;
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return result;
}
/**
* 写入
* @param str
* @return
*/
public boolean write(String str){
boolean result = true;
FileOutputStream fos = null;
if(isExist()){
try {
fos = context.openFileOutput(fileName, Context.MODE_APPEND);
} catch (FileNotFoundException e) {
e.printStackTrace();
result = false;
}
byte bt [] = (str+"\n").getBytes();
if(fos != null){
try {
fos.write(bt);
} catch (IOException e) {
e.printStackTrace();
result = false;
}
try{
fos.close();
}catch (Exception e) {
e.printStackTrace();
result = false;
}
}
}
return result;
}
/**
* 读取一行
* @return
*/
public String readLine(){
String str = "";
FileInputStream fis = null;
if(isExist()){
try {
fis = context.openFileInput(fileName);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
try {
str = br.readLine();
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return str;
}
/**
* 读取全部
* @return
*/
public String read(){
String str = "";
String line = "";
FileInputStream fis = null;
if(isExist()){
try {
fis = context.openFileInput(fileName);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
try {
while((line = br.readLine()) != null){
str += line;
}
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return str;
}
/**
* 清空文件
*/
public void clean(){
delete();
create();
}
/**
* 删除文件
*/
public void delete(){
context.deleteFile(fileName);
}
}
转自:http://www.eoeandroid.com/thread-70490-1-1.html
- Android 程序私有文件(TXT)操作类
- android txt文件读写(读取资源文件,读取私有和SD文件的方法)
- 私有ini文件操作类
- 文件夹,txt文件操作
- txt文件操作 读取
- txt 文件操作
- 操作TXT文件
- c# 操作txt文件
- Java操作txt文件
- 文本txt文件操作
- java操作txt文件
- 读取txt文件操作
- C#操作txt文件
- 操作txt文件(记事本)
- delphi txt 文件操作
- java操作txt文件
- matlab txt 文件操作
- js操作txt文件
- linux 查看进程“打开”的文件或文件对应的进程及网络状态
- 拉齐奥最后时刻演疯狂 尤文图斯1-2遭淘汰
- device eth0 does not seem to be present, delaying initialization
- MKS命令
- debian 6.0 func 安装 局域网基本测试完成
- Android 程序私有文件(TXT)操作类
- 如何根据纬度,经度和日期判断太阳的日出和日落的时间
- 程序员看世界之铁老大一票难求(12)2013-01-30
- java I/O 以及文件编码
- 关于表格样式
- wxworks DOS文件系统
- 软件随想录(local.joelonsoftware.com/wiki)-2000年04月12日 使用介面设计手冊第三章 - User Interface Design for Programmers
- HDU1058 Humble Numbers
- Struts1和struts2的工作流程