在安卓中,通过Activity或Environment对象可以获得好多不同的路径,下面来一一分析
第一类:没什么用处的路径
System.out.println("根目录是"+Environment.getRootDirectory());//: /system
System.out.println("数据目录是"+Environment.getDataDirectory());//: /data
System.out.println("下载缓存目录是"+Environment.getDownloadCacheDirectory());//: /cache
第二类:系统固定存放多媒体文件的文件夹
System.out.println("本app图片目录是"+getExternalFilesDir(Environment.DIRECTORY_PICTURES));//: /storage/emulated/0/Android/data/com.example.situ_android_publish/files/Photo
System.out.println("系统照片目录是"+Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM));//: /storage/emulated/0/Camera
还有很多,大家自己改后面的参数就好
第三类:sd卡上的公用目录和app自己的缓存目录
System.out.println("SD卡路径"+Environment.getExternalStorageDirectory());//: /storage/emulated/0
System.out.println("本app外部缓存是"+getExternalCacheDir());//: /storage/emulated/0/Android/data/com.example.situ_android_publish/cache
第四类:机身存储app私有文件的目录
System.out.println("本app目录是"+getFilesDir());//: /data/data/com.example.situ_android_publish/files
System.out.println("本app缓存是"+getCacheDir()); //: /data/data/com.example.situ_android_publish/cache
System.out.println("文件流路径是"+getFileStreamPath("test.txt"));//: /data/data/com.example.situ_android_publish/files/test.txt
System.out.println(getDir("文件名.txt", MODE_APPEND));//: /data/data/com.example.situ_android_publish/app_文件名.txt
根据sd卡目录,sd卡缓存,机身内存,机身缓存,本人写了一个类专门读写这些目录里的文件
package com.situ.android.util;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import android.content.Context;
import android.os.Environment;
public class FileUtils {
/**
* 将字节数组写入手机内部存储
* @param context
* @param fileName 只要文件名,不要别的
* @param mode
* @param data
* @return 如果成功,返回被存储文件的file,如果失败,返回null
*/
public static File saveBytesToFile(Context context,String fileName,int mode,byte[] data) {
boolean flag = false;
FileOutputStream outputStream = null;
try {
outputStream = context.openFileOutput(fileName, mode);
outputStream.write(data);
flag=true;
System.out.println("文件存储"+fileName+"成功!");
return context.getFileStreamPath(fileName);
} catch (Exception e) {
// TODO: handle exception
System.out.println("写入文件"+fileName+"失败!!!!!!");
return null;
}finally{
if(outputStream!=null){
try {
outputStream.close();
} catch (Exception e2) {
// TODO: handle exception
}
}
}
}
/**
* 将文件写入SD卡,可以规定父目录,如: /storage/emulated/0/imgs/haha2.txt
* @param parentFolder 形如“imgs”
* @param fileName 形如photo.jpg
* @param data
* @return
*/
public static File saveBytesToFile_SD(String parentFolder,String fileName,byte[] data) {
File file =null;
if(parentFolder==null||parentFolder.length()==0){
file = new File(Environment.getExternalStorageDirectory().toString()+"/"+fileName);
}else {
file = new File(Environment.getExternalStorageDirectory().toString()+"/"+parentFolder+"/"+fileName);
}
return saveBytesToFile_SD(file, data);
}
/**
* 把文件写入sd卡的缓存目录下,如/storage/emulated/0/Android/data/com.example.situ_android_publish/cache
* @param context
* @param parentFolder 父目录形如“imgs”
* @param fileName 形如photo.jpg
* @param data
* @return
*/
public static File saveBytesToFile_SD_cache(Context context,String parentFolder,String fileName,byte[] data) {
File file =null;
if(parentFolder==null||parentFolder.length()==0){
file = new File(context.getExternalCacheDir().toString()+"/"+fileName);
}else {
file = new File(context.getExternalCacheDir().toString()+"/"+parentFolder+"/"+fileName);
}
return saveBytesToFile_SD(file, data);
}
/**
* 把文件写进机身内存的缓存目录中
* @param context
* @param parentFolder 父目录
* @param fileName
* @param data
* @return
*/
public static File saveBytesToFile_cache(Context context,String parentFolder,String fileName,byte[] data) {
File file =null;
if(parentFolder==null||parentFolder.length()==0){
file = new File(context.getCacheDir().toString()+"/"+fileName);
}else {
file = new File(context.getCacheDir().toString()+"/"+parentFolder+"/"+fileName);
}
return saveBytesToFile(file, data);
}
/**
* 将字节数组写入sd卡
* @param file
* @param data
* @return
*/
public static File saveBytesToFile_SD(File file,byte[] data) {
String sdStatus = Environment.getExternalStorageState();
if(!sdStatus.equals(Environment.MEDIA_MOUNTED)) {
System.out.println("未安装SD卡或没有权限");
return null;
}
//判断存储空间是否足够
// if(StorageUtil.getAvailableExternal_SDMemorySize()<data.length){
// System.out.println("SD卡空间不足,写入失败");
// return null;
// }
FileOutputStream outputStream = null;
if(!file.getParentFile().exists()){
file.getParentFile().mkdirs();
}
try {
file.createNewFile();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
System.out.println("创建文件"+file+"失败!!!!!!");
return null;
}
try {
outputStream = new FileOutputStream(file);
outputStream.write(data);
System.out.println("文件存储"+file+"成功!");
return file;
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
System.out.println("写入文件"+file+"失败!!!!!!");
return null;
}finally{
if(outputStream!=null){
try {
outputStream.close();
} catch (Exception e2) {
// TODO: handle exception
}
}
}
}
/**
* 根据父目录和文件名读取sd卡中的数据
* @param parentFolder
* @param fileName
* @return
*/
public byte[] readBytesFromFile_SD(String parentFolder,String fileName){
File file =null;
if(parentFolder==null||parentFolder.length()==0){
file = new File(Environment.getExternalStorageDirectory().toString()+"/"+fileName);
}else {
file = new File(Environment.getExternalStorageDirectory().toString()+"/"+parentFolder+"/"+fileName);
}
return readBytesFromFile_SD(file);
}
public byte[] readBytesFromFile_SD_cache(Context context,String parentFolder,String fileName){
File file =null;
if(parentFolder==null||parentFolder.length()==0){
file = new File(context.getExternalCacheDir().toString()+"/"+fileName);
}else {
file = new File(context.getExternalCacheDir().toString()+"/"+parentFolder+"/"+fileName);
}
return readBytesFromFile_SD(file);
}
/**
* 根据file对象读取sd卡中的数据
* @param file
* @return
*/
@SuppressWarnings("resource")
public static byte[] readBytesFromFile_SD(File file){
String sdStatus = Environment.getExternalStorageState();
if(!sdStatus.equals(Environment.MEDIA_MOUNTED)) {
System.out.println("未安装SD卡或没有权限");
return null;
}
System.out.println("正在读取"+file);
FileInputStream fileInputStream = null;
//输出流用于生产字节数组
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
if(!file.exists()){
System.out.println("要读取的文件不存在"+file);
return null;
}
try {
fileInputStream = new FileInputStream(file);
int len = 0;
byte[] data = new byte[1024];
while ((len=fileInputStream.read(data))!=-1) {
outputStream.write(data,0,len);
}
return outputStream.toByteArray();
} catch (Exception e) {
// TODO: handle exception
System.out.println("读取文件"+file+"失败");
}finally{
try {
outputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return null;
}
/**
* 通过文件父目录和文件名来获取手机内存里的文件
* @param context
* @param parentFolder
* @param fileName
* @return
*/
public static byte[] readBytesFromFile(Context context,String parentFolder,String fileName){
File file =null;
if(parentFolder==null||parentFolder.length()==0){
file = new File(context.getFilesDir().toString()+"/"+fileName);
}else {
file = new File(context.getFilesDir().toString()+"/"+parentFolder+"/"+fileName);
}
return readBytesFromFile(file);
}
/**
* 获取手机内存中本app的缓存文件
* @param context
* @param parentFolder
* @param fileName
* @return
*/
public static byte[] readBytesFromFile_cache(Context context,String parentFolder,String fileName){
File file =null;
if(parentFolder==null||parentFolder.length()==0){
file = new File(context.getCacheDir().toString()+"/"+fileName);
}else {
file = new File(context.getCacheDir().toString()+"/"+parentFolder+"/"+fileName);
}
return readBytesFromFile(file);
}
public static byte[] readBytesFromFile(File file){
System.out.println("正在读取"+file);
FileInputStream fileInputStream = null;
//输出流用于生产字节数组
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
if(!file.exists()){
System.out.println("要读取的文件不存在"+file);
return null;
}
try {
fileInputStream = new FileInputStream(file);
int len = 0;
byte[] data = new byte[1024];
while ((len=fileInputStream.read(data))!=-1) {
outputStream.write(data,0,len);
}
return outputStream.toByteArray();
} catch (Exception e) {
// TODO: handle exception
System.out.println("读取文件"+file+"失败");
}finally{
try {
outputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return null;
}
/**
* 根据父目录和文件名写入手机内部存储
* @param context
* @param parentFolder
* @param fileName
* @param data
* @return
*/
public static File saveBytesToFile(Context context,String parentFolder,String fileName,byte[] data) {
File file =null;
if(parentFolder==null||parentFolder.length()==0){
file = new File(context.getFilesDir().toString()+"/"+fileName);
}else {
file = new File(context.getFilesDir().toString()+"/"+parentFolder+"/"+fileName);
}
return saveBytesToFile(file, data);
}
/**
* 通过file对象写手机的内部存储
* @param file
* @param data
* @return
*/
public static File saveBytesToFile(File file,byte[] data) {
FileOutputStream outputStream = null;
if(!file.getParentFile().exists()){
file.getParentFile().mkdirs();
}
try {
file.createNewFile();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
System.out.println("创建文件"+file+"失败!!!!!!");
return null;
}
try {
outputStream = new FileOutputStream(file);
outputStream.write(data);
System.out.println("文件存储"+file+"成功!");
return file;
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
System.out.println("写入文件"+file+"失败!!!!!!");
return null;
}finally{
if(outputStream!=null){
try {
outputStream.close();
} catch (Exception e2) {
// TODO: handle exception
}
}
}
}
/**
* 写新的字符串或者覆盖原来的文件
* @param context
* @param fileName 只要文件名,不要别的
* @param myData
* @return
*/
public static File saveTextToNewFile(Context context,String fileName,String myData) {
byte[] data = myData.getBytes();
return saveBytesToFile(context, fileName, context.MODE_PRIVATE, data);
}
/**
* 追加文件,在系统内部存储
* @param context
* @param fileName 只要文件名,不要别的
* @param data
* @return
*/
public static File appendTextToFile(Context context,String fileName,String data){
return saveBytesToFile(context, fileName, context.MODE_APPEND, data.getBytes());
}
public static File appendBytesToFile(Context context,String fileName,byte[] data){
return saveBytesToFile(context, fileName, context.MODE_APPEND, data);
}
/**
* 从内部存储文件中读取字符数组
* @param context
* @param fileName
* @return
*/
public static byte[] readBytesFromFile(Context context ,String fileName){
System.out.println("正在读取"+context.getFileStreamPath(fileName));
FileInputStream fileInputStream = null;
//输出流用于生产字节数组
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
try {
fileInputStream = context.openFileInput(fileName);
int len = 0;
byte[] data = new byte[1024];
while ((len=fileInputStream.read(data))!=-1) {
outputStream.write(data,0,len);
}
return outputStream.toByteArray();
} catch (Exception e) {
// TODO: handle exception
System.out.println("读取文件"+fileName+"失败");
}finally{
try {
outputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return null;
}
/**
* 读取本app机身内存根目录下的文件里的字符串
* @param context
* @param fileName
* @return
*/
public static String readTextFromFile(Context context ,String fileName){
byte[] bytes = readBytesFromFile(context, fileName);
if(bytes==null){
System.out.println("文件不存在"+fileName);
return null;
}
return new String(bytes);
}
}
简单用法:
System.out.println(FileUtils.saveTextToNewFile(this, "test.txt", "哈哈哈哈哈"));
System.out.println("保存内容是"+FileUtils.readTextFromFile(this, "test.txt"));
File file = FileUtils.saveBytesToFile_SD("imgs", "haha2.txt", "哈哈哈哈哈".getBytes());
System.out.println();
System.out.println("读取sd卡"+new String(FileUtils.readBytesFromFile_SD(file)));