Android的SD卡文件读写的帮助类——FileHelper,以及查询SD卡是否存在及其容量的方法
【摘要】
FileHelper.java是文件的帮助类,完成文件创建、删除、读。
在开始之前别忘了加权限:<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
public class FileHelper { ...
FileHelper.java是文件的帮助类,完成文件创建、删除、读。
在开始之前别忘了加权限:<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
-
public class FileHelper {
-
-
private Context context;
-
-
/** SD卡是否存在**/
-
-
private boolean hasSD = false;
-
-
/** SD卡的路径**/
-
-
private String SDPATH;
-
-
/** 当前程序包的路径**/
-
-
private String FILESPATH;
-
-
public FileHelper(Context context) {
-
-
this.context = context;
-
-
hasSD = Environment.getExternalStorageState().equals(
-
-
android.os.Environment.MEDIA_MOUNTED);
-
-
SDPATH = Environment.getExternalStorageDirectory().getPath();
-
-
FILESPATH = this.context.getFilesDir().getPath();
-
-
}
-
-
/**
-
-
* 在SD卡上创建文件
-
-
*
-
-
* @throws IOException
-
-
*/
-
-
public File createSDFile(String fileName) throws IOException {
-
-
File file = new File(SDPATH + "//" + fileName);
-
-
if (!file.exists()) {
-
-
file.createNewFile();
-
-
}
-
-
return file;
-
-
}
-
-
/**
-
-
* 删除SD卡上的文件
-
-
*
-
-
* @param fileName
-
-
*/
-
-
public boolean deleteSDFile(String fileName) {
-
-
File file = new File(SDPATH + "//" + fileName);
-
-
if (file == null || !file.exists() || file.isDirectory())
-
-
return false;
-
-
return file.delete();
-
-
}
-
-
/**
-
-
* 读取SD卡中文本文件
-
-
*
-
-
* @param fileName
-
-
* @return
-
-
*/
-
-
public String readSDFile(String fileName) {
-
-
StringBuffer sb = new StringBuffer();
-
-
File file = new File(SDPATH + "//" + fileName);
-
-
try {
-
-
FileInputStream fis = new FileInputStream(file);
-
-
int c;
-
-
while ((c = fis.read()) != -1) {
-
-
sb.append((char) c);
-
-
}
-
-
fis.close();
-
-
} catch (FileNotFoundException e) {
-
-
e.printStackTrace();
-
-
} catch (IOException e) {
-
-
e.printStackTrace();
-
-
}
-
-
return sb.toString();
-
-
}
-
-
public String getFILESPATH() {
-
-
return FILESPATH;
-
-
}
-
-
public String getSDPATH() {
-
-
return SDPATH;
-
-
}
-
-
public boolean hasSD() {
-
-
return hasSD;
-
-
}
-
-
}
Environment 常用方法:
* 方法:getDataDirectory()
解释:返回 File ,获取 Android 数据目录。
* 方法:getDownloadCacheDirectory()
解释:返回 File ,获取 Android 下载/缓存内容目录。
* 方法:getExternalStorageDirectory()
解释:返回 File ,获取外部存储目录即 SDCard
* 方法:getExternalStoragePublicDirectory(String type)
解释:返回 File ,取一个高端的公用的外部存储器目录来摆放某些类型的文件
* 方法:getExternalStorageState()
解释:返回 File ,获取外部存储设备的当前状态
* 方法:getRootDirectory()
解释:返回 File ,获取 Android 的根目录
-
public void getSDPath(){
-
File sdDir = null;
-
File sdDir1 = null;
-
File sdDir2 = null;
-
boolean sdCardExist = Environment.getExternalStorageState()
-
.equals(android.os.Environment.MEDIA_MOUNTED); //判断sd卡是否存在
-
if (sdCardExist)
-
{
-
sdDir = Environment.getExternalStorageDirectory();//获取跟目录
-
sdDir1 = Environment.getDataDirectory();
-
sdDir2 =Environment.getRootDirectory();
-
}
-
System.out.println("getExternalStorageDirectory(): "+sdDir.toString());
-
System.out.println("getDataDirectory(): "+sdDir1.toString());
-
System.out.println("getRootDirectory(): "+sdDir2.toString());
-
}
-
//判断一个路径下的文件(文件夹)是否存在
-
-
public class IsExist {
-
public static void main(String[] args) {
-
isExist("e://12");
-
}
-
/**
-
*
-
* @param path 文件夹路径
-
*/
-
public static void isExist(String path) {
-
File file = new File(path);
-
//判断文件夹是否存在,如果不存在则创建文件夹
-
if (!file.exists()) {
-
file.mkdir();
-
}
-
}
-
}
-
-
//1.在SD卡上创建一个文件夹
-
public class make extends Activity {
-
/** Called when the activity is first created. */
-
@Override
-
public void onCreate(Bundle savedInstanceState) {
-
super.onCreate(savedInstanceState);
-
setContentView(R.layout.main);
-
File sd=Environment.getExternalStorageDirectory();
-
String path=sd.getPath()+"/notes";
-
File file=new File(path);
-
if(!file.exists())
-
file.mkdir();
-
-
}
Android 判断SD卡是否存在及容量查询的简单方法如下:
首先要在AndroidManifest.xml中增加SD卡访问权限
-
<!-- 在SDCard中创建与删除文件权限 -->
-
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
-
<!-- 往SDCard写入数据权限 -->
-
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
SD卡剩余空间
-
public long getSDFreeSize(){
-
//取得SD卡文件路径
-
File path = Environment.getExternalStorageDirectory();
-
StatFs sf = new StatFs(path.getPath());
-
//获取单个数据块的大小(Byte)
-
long blockSize = sf.getBlockSize();
-
//空闲的数据块的数量
-
long freeBlocks = sf.getAvailableBlocks();
-
//返回SD卡空闲大小
-
//return freeBlocks * blockSize; //单位Byte
-
//return (freeBlocks * blockSize)/1024; //单位KB
-
return (freeBlocks * blockSize)/1024 /1024; //单位MB
-
}
SD卡总容量
-
public long getSDAllSize(){
-
//取得SD卡文件路径
-
File path = Environment.getExternalStorageDirectory();
-
StatFs sf = new StatFs(path.getPath());
-
//获取单个数据块的大小(Byte)
-
long blockSize = sf.getBlockSize();
-
//获取所有数据块数
-
long allBlocks = sf.getBlockCount();
-
//返回SD卡大小
-
//return allBlocks * blockSize; //单位Byte
-
//return (allBlocks * blockSize)/1024; //单位KB
-
return (allBlocks * blockSize)/1024/1024; //单位MB
-
}
文章来源: panda1234lee.blog.csdn.net,作者:panda1234lee,版权归原作者所有,如需转载,请联系作者。
原文链接:panda1234lee.blog.csdn.net/article/details/8701920
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)