Android 实现文件 File 读写操作
2010-03-25 17:09:00 来源:WEB开发网File 读写
[功能]
因为文件读写很平常 使用打算把这个功能写出辅助类的形式 以便以后方便使用 就是:FileIOHelper
[代码]
1. 定义指定的File 以及其上的 FileInputStream FileOutputStream
Java代码
Context context;
File file;
FileInputStream fin;
FileOutputStream fout;
public FileIOHelper(Context c, String name,String path) throws IOException{
context = c;
file = new File(path,name);
file.createNewFile();
fin = new FileInputStream(file);
fout = new FileOutputStream(file);
}
Context context;
File file;
FileInputStream fin;
FileOutputStream fout;
public FileIOHelper(Context c, String name,String path) throws IOException{
context = c;
file = new File(path,name);
file.createNewFile();
fin = new FileInputStream(file);
fout = new FileOutputStream(file);
}
2. 文件写
Java代码
public void wrire(String s) throws IOException{
fout.write(s.getBytes());
fout.close();
}
public void wrire(String s) throws IOException{
fout.write(s.getBytes());
fout.close();
}
3. 文件读
Java代码
public byte[] read(int s,int l) throws IOException{
byte[] save = new byte[l];
fin.read(save, s, l);
return save;
}
public byte[] read(int s,int l) throws IOException{
byte[] save = new byte[l];
fin.read(save, s, l);
return save;
}
4. 编码转换
Java代码
public String encode(byte[] array){
return EncodingUtils.getString(array,TEXT_ENCODING);
}
public String encode(byte[] array){
更多精彩
赞助商链接