您的位置:首页 > 移动开发 > Android开发

今天复习一下文件读取

2014-10-27 19:59 260 查看
自己整理写的一个文件读取类,可以读取和写入
public class FileService {
public Context context;

public FileService(Context context) {
this.context = context;
}
public void saveToSDCard(String filename,String content){
File file = new File(Environment.getExternalStorageDirectory(),filename);
FileOutputStream fileOutputStream = null;
try {
fileOutputStream = new FileOutputStream(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}

try {
fileOutputStream.write(content.getBytes());
} catch (IOException e) {
e.printStackTrace();
}

try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}

public void save(String filename,String content){
FileOutputStream fileOutputStream = null;
try {
fileOutputStream = context.openFileOutput(filename,Context.MODE_PRIVATE);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
fileOutputStream.write(content.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}

public void saveAppend(String filename,String content){
FileOutputStream fileOutputStream = null;
try {
fileOutputStream = context.openFileOutput(filename,Context.MODE_APPEND);
} catch (FileNotFoundException e) {
e.printStackTrace();
}

try {
fileOutputStream.write(content.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}

public void saveWriteable(String filename,String content){
FileOutputStream fileOutputStream = null;
try {
fileOutputStream = context.openFileOutput(filename,Context.MODE_WORLD_WRITEABLE);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
fileOutputStream.write(content.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}

public void saveReadable(String filename,String content){
FileOutputStream fileOutputStream = null;
try {
fileOutputStream = context.openFileOutput(filename,Context.MODE_WORLD_READABLE);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
fileOutputStream.write(content.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}

public void saveRW(String filename,String content){
FileOutputStream fileOutputStream = null;
try {
fileOutputStream = context.openFileOutput(filename,Context.MODE_WORLD_WRITEABLE+Context.MODE_WORLD_READABLE);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
fileOutputStream.write(content.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}

public String read(String filename){
FileInputStream fileInputStream = null;
try {
fileInputStream = context.openFileInput(filename);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
byte byteArray[] = new byte[1024];
int len = 0;
try {
while((len = fileInputStream.read(byteArray))!=-1){
byteArrayOutputStream.write(byteArray,0,len);
}
} catch (IOException e) {
e.printStackTrace();
}
String content = new String(byteArray);
return content;
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android bytearray