即拿即用一Android文件存取

写文件

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
/**
* 写文件
*
* @param filePath 文件绝对路径
* @param content 写入内容
* @return 写入是否成功
*/
public static boolean writeFile(String filePath, String content) {
FileOutputStream fileOutputStream = null;
BufferedOutputStream bufferedOutputStream = null;
try {
File file = new File(filePath);
if (file.createNewFile()) {
fileOutputStream = new FileOutputStream(filePath);
bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
bufferedOutputStream.write(content.getBytes("UTF-8"));
bufferedOutputStream.flush();
return true;
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (null != fileOutputStream) {
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (null != bufferedOutputStream) {
try {
bufferedOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return false;
}

读文件

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
/**
* 读文件
*
* @param filePath 文件路径
* @return 文件内容
*/
public static String readFile(String filePath) {
StringBuilder stringBuffer = new StringBuilder();
File file = new File(filePath);
if (file.exists()) {
FileInputStream fileInputStream = null;
InputStreamReader inputStreamReader = null;
BufferedReader bufferedReader = null;
try {
fileInputStream = new FileInputStream(file);
inputStreamReader = new InputStreamReader(fileInputStream, "UTF-8");
bufferedReader = new BufferedReader(inputStreamReader);
String mimeTypeLine;
while ((mimeTypeLine = bufferedReader.readLine()) != null) {
stringBuffer.append(mimeTypeLine);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (null != fileInputStream) {
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (null != inputStreamReader) {
try {
inputStreamReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (null != bufferedReader) {
try {
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
return stringBuffer.toString();
}

Assets文件读取

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
/**
* 读取 Assets 文件
*
* @param context context
* @param filePath 文件路径
* @return 文件内容
*/
public static String readAssetsFile(Context context, String filePath) {
StringBuilder stringBuilder = new StringBuilder();
InputStream inputStream = null;
InputStreamReader inputStreamReader = null;
BufferedReader bufferedReader = null;
try {
inputStream = context.getAssets().open(filePath);
inputStreamReader = new InputStreamReader(inputStream, "UTF-8");
bufferedReader = new BufferedReader(inputStreamReader);
String mimeTypeLine;
while ((mimeTypeLine = bufferedReader.readLine()) != null) {
stringBuilder.append(mimeTypeLine)/*.append("\n")*/;
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (null != inputStream) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (null != inputStreamReader) {
try {
inputStreamReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (null != bufferedReader) {
try {
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return stringBuilder.toString();
}

Bitmap保存

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
/**
* 保存Bitmap到本地
*
* @param bitmap Bitmap
* @param dir 保存路径
* @param fileName 保存的文件名
* @return 保存是否成功
*/
public static boolean saveBitmap(Bitmap bitmap, String dir, String fileName) {
FileOutputStream fileOutputStream = null;
try {
File fileDir = new File(dir);
if (fileDir.exists() || fileDir.mkdirs()) {
// 文件存在
File file = new File(dir + "/" + fileName);
if (file.createNewFile()) {
fileOutputStream = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fileOutputStream);
fileOutputStream.flush();
}
}
} catch (IOException e) {
e.printStackTrace();
return false;
} finally {
if (null != fileOutputStream) {
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return true;
}

文件删除

1
2
3
4
5
6
7
8
9
10
/**
* 删除文件
*
* @param filePath 文件路径
* @return 删除是否成功
*/
public static boolean deleteFile(String filePath) {
File file = new File(filePath);
return file.exists() && file.delete();
}
坚持原创技术分享,您的支持将鼓励我继续创作!