自己整理写的一个文件读取类,可以读取和写入
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;
}
}