Android 处理txt文件类FileUtils 利用java反射机制访问非sdk类和函数
2010-09-10 00:59:00 来源:WEB开发网*/
public static void createText(final String path) {
File filename ;
try {
filename = new File(path);
if (!filename.exists()) {
filename.createNewFile();
}
} catch (IOException e) {
e.printStackTrace();
}
}// end method createText()
/**
* 删除Text文件
* @param path
*/
public void deleteText(String path) {
try {
RandomAccessFile file = new RandomAccessFile(path, "rw");
file.setLength(0);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 读取text内容
* @param path
* @return
*/
public static String readText(String path) {
FileReader fileread;
File filename = new File(path);
String line = null;
try {
fileread = new FileReader(filename);
BufferedReader bfr = new BufferedReader(fileread);
try {
line = bfr.readLine();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return line;
}// end method readText()
/**
* 向Text文件中写入内容
* @param body
* @param path
*/
public static void writeText(String content, String path) {
// 先读取原有文件内容,然后进行写入操作
RandomAccessFile mm = null;
File filename = new File(path);
try {
mm = new RandomAccessFile(filename, "rw");
mm.writeBytes(content);
} catch (IOException e1) {
// TODO 自动生成 catch 块
e1.printStackTrace();
} finally {
if (mm != null) {
try {
mm.close();
} catch (IOException e2) {
// TODO 自动生成 catch 块
e2.printStackTrace();
}
}
}
}// end method writeText()
}
更多精彩
赞助商链接