Android关于文件File的工具类

读取资源文件

Android的res文件夹是用来存储资源的,可以在res文件夹下建立一个raw文件夹,放置在raw文件夹下的内容会被原样打包,而不会被编译成二进制文件,并且可以通过R文件进行很方便地访问。
比如我们可以将更新信息、版权信息等放到txt文件中,然后放到raw文件中,然后很方便地进行访问。
在raw中放入一个a.txt文件,然后就可以在Activity中使用getResources().openRawResource(R.raw.a);方法获取一个此文件的InputStream类,而后就可以很方便地进行读写a.txt了。
InputStream inputStream = getResources().openRawResource(R.raw.a);

一个获取InputStream中字符串内容的方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public static String getString(InputStream inputStream) {
InputStreamReader inputStreamReader = null;
try {
inputStreamReader = new InputStreamReader(inputStream, "gbk");
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
BufferedReader reader = new BufferedReader(inputStreamReader);
StringBuffer sb = new StringBuffer();
String line;
try {
while ((line = reader.readLine()) != null) {
sb.append(line);
sb.append("\n");
}
} catch (IOException e) {
e.printStackTrace();
}
return sb.toString();
}

其中,inputStreamReader = new InputStreamReader(inputStream, "gbk")为以gbk编码读取内容,不同的文本文件可能编码不同,如果出现乱码,可能需要调整编码。

文件保存到应用和SD卡中

权限添加:

1
2
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
public static String getDataFolderPath(Context paramContext) {
return Environment.getDataDirectory() + "/data/" + paramContext.getPackageName() + "/files";
}
public static String getMyFileDir(Context context){
return context.getFilesDir().toString();
}
public static String getMyCacheDir(Context context){
return context.getCacheDir().toString();
}
/**
* @desc 保存内容到文件中
* @param fileName
* @param content
* @throws Exception
*/
public static voidsave(Context context, String fileName, String content, int module) {
try{
FileOutputStream os = context.openFileOutput(fileName, module);
os.write(content.getBytes());
os.close();
} catch(Exception e) {
e.printStackTrace();
}
}
/**
* @desc 读取文件内容
* @param fileName
* @return
*/
public static String read(Context context, String fileName){
try{
FileInputStream fis = context.openFileInput(fileName);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] b = new byte[1024];
int len = 0;
while((len = fis.read(b)) != -1){
bos.write(b, 0, len);
}
byte[] data = bos.toByteArray();
fis.close();
bos.close();
return newString(data);
} catch(Exception e) {
e.printStackTrace();
}
return null;
}
/**
* @desc 将文本内容保存到sd卡的文件中
* @param context
* @param fileName
* @param content
* @throws IOException
*/
public static void saveToSDCard(Context context, String fileName, String content) throwsIOException{
File file = new File(Environment.getExternalStorageDirectory(),fileName);
FileOutputStream fos = new FileOutputStream(file);
fos.write(content.getBytes());
fos.close();
}
/**
* @desc 读取sd卡文件内容
* @param fileName
* @return
* @throws IOException
*/
public static String readSDCard(String fileName) throwsIOException {
File file = new File(Environment.getExternalStorageDirectory(),fileName);
FileInputStream fis = new FileInputStream(file);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while((len = fis.read(buffer)) != -1)
{
bos.write(buffer, 0, len);
}
byte[] data = bos.toByteArray();
fis.close();
bos.close();
return new String(data);
}

序列化操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
public class SerializeUtil {
public static String FILENAME = "db";
public static void saveObject(Context context, Object object, String name) throws IOException {
FileOutputStream fos = null;
ObjectOutputStream oos = null;
try {
fos = context.openFileOutput(name + "." + FILENAME, Context.MODE_PRIVATE);
oos = new ObjectOutputStream(fos);
oos.writeObject(object);
} finally {
if (fos != null) {
fos.close();
}
if (oos != null) {
oos.close();
}
}
}
public static Object getObject(Context context, String name) throws IOException, ClassNotFoundException {
FileInputStream fis = null;
ObjectInputStream ois = null;
try {
fis = context.openFileInput(name + "." + FILENAME);
ois = new ObjectInputStream(fis);
return ois.readObject();
} finally {
if (fis != null) {
fis.close();
}
if (ois != null) {
ois.close();
}
}
}
}