package com.example.lol.util;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import com.example.lol.constant.Constants;
import android.content.Context;
public class FileUtil {
public static void writeFiles(String content,Context context) {
try {
// 打开文件获取输出流,文件不存在则自动创建
FileOutputStream fos =context.openFileOutput(Constants.FILENAME,
Context.MODE_APPEND);
fos.write(content.getBytes());
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
// 读取文件内容
public static String readFiles(Context context) {
String content = null;
try {
FileInputStream fis =context.openFileInput(Constants.FILENAME);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while ((len = fis.read(buffer)) != -1) {
baos.write(buffer, 0, len);
}
content = baos.toString();
fis.close();
baos.close();
} catch (Exception e) {
e.printStackTrace();
}
return content;
}
}
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import com.example.lol.constant.Constants;
import android.content.Context;
public class FileUtil {
public static void writeFiles(String content,Context context) {
try {
// 打开文件获取输出流,文件不存在则自动创建
FileOutputStream fos =context.openFileOutput(Constants.FILENAME,
Context.MODE_APPEND);
fos.write(content.getBytes());
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
// 读取文件内容
public static String readFiles(Context context) {
String content = null;
try {
FileInputStream fis =context.openFileInput(Constants.FILENAME);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while ((len = fis.read(buffer)) != -1) {
baos.write(buffer, 0, len);
}
content = baos.toString();
fis.close();
baos.close();
} catch (Exception e) {
e.printStackTrace();
}
return content;
}
}