Android通过文件路径如何得到文件相关信息,如 文件名称,文件大小,创建时间,文件的相对路径,文件的绝对路径等。

如图:

public class MainActivity extends Activity {

    private String path = "/storage/emulated/0/Android/data/cn.wps.moffice_eng/mm.doc";
private TextView mTextView; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
} @SuppressLint("SimpleDateFormat")
private void initView() {
// TODO Auto-generated method stub
mTextView = (TextView) findViewById(R.id.textview);
File f = new File(path);
if (f.exists()) {
FileInputStream fis = null;
try {
fis = new FileInputStream(f);
String time = new SimpleDateFormat("yyyy-MM-dd")
.format(new Date(f.lastModified()));
System.out.println("文件文件创建时间" + time);
System.out.println("文件大小:" + ShowLongFileSzie(f.length()));// 计算文件大小
// B,KB,MB,
System.out.println("文件大小:" + fis.available() + "B");
System.out.println("文件名称:" + f.getName());
System.out.println("文件是否存在:" + f.exists());
System.out.println("文件的相对路径:" + f.getPath());
System.out.println("文件的绝对路径:" + f.getAbsolutePath());
System.out.println("文件可以读取:" + f.canRead());
System.out.println("文件可以写入:" + f.canWrite());
System.out.println("文件上级路径:" + f.getParent());
System.out.println("文件大小:" + f.length() + "B");
System.out.println("文件最后修改时间:" + new Date(f.lastModified()));
System.out.println("是否是文件类型:" + f.isFile());
System.out.println("是否是文件夹类型:" + f.isDirectory());
mTextView.setText("文件文件创建时间:" + time + "\n" + "文件大小:"
+ ShowLongFileSzie(f.length()) + "\n" + "文件名称:"
+ f.getName() + "\n" + "文件是否存在:" + f.exists() + "\n"
+ "文件的相对路径:" + f.getPath() + "\n" + "文件的绝对路径:"
+ f.getAbsolutePath() + "\n" + "文件可以写入:" + f.canWrite()
+ "\n" + "是否是文件夹类型:" + f.isDirectory());
} catch (Exception e) {
e.printStackTrace();
}
}
} /****
* 计算文件大小
*
* @param length
* @return
*/
public String ShowLongFileSzie(Long length) {
if (length >= 1048576) {
return (length / 1048576) + "MB";
} else if (length >= 1024) {
return (length / 1024) + "KB";
} else if (length < 1024) {
return length + "B";
} else {
return "0KB";
}
} }

不要忘记在AndroidManifest.xml加权限哦!

<!-- SD卡权限 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />

源码点击下载:https://github.com/DickyQie/android-file

最新文章

  1. Word Search
  2. [linux]如何为Virtualbox虚拟硬盘扩容(转载)
  3. handlebars,each循环里面套each循环
  4. java命名规范有感
  5. SQL*Loader之CASE7
  6. js String对象
  7. SqlServer 注入技巧
  8. cf111D Petya and Coloring 组合数学,二项式反演
  9. 细雨学习笔记:Jmeter上一个请求的结果作为下一个请求的参数--使用正则提取器
  10. android中向bitmap里写入文字
  11. 基于python的接口测试学习笔记一(初出茅庐)
  12. PHP之验证码代码
  13. 玩转Windows服务系列&mdash;&mdash;命令行管理Windows服务
  14. Struts2 程序步骤
  15. 201521123119《Java程序设计》第12周学习总结
  16. Python数据结构之四——set(集合)
  17. KeyError: &#39;Spider not found: test&#39;
  18. Android Studio3.1.2升级问题:Configuration &#39;compile&#39; is obsolete and has been replaced with &#39;implementation&#39;.
  19. ecs主机被破解后下载的执行文件的脚本
  20. winform 子窗体调用父窗体中的方法

热门文章

  1. 【转】MySQL数据丢失讨论
  2. VC非法内存值的判断
  3. KETTLE实现数据的删除和更新
  4. bond汇总
  5. 实现ApplicationContextAware接口时,获取ApplicationContext为null
  6. Ninject之旅之二:开始使用Ninject(附程序下载)
  7. 如何设置Java开发环境
  8. android第二天(项目的组成结构)
  9. python包下载地址
  10. javascript获取当前时间