zoukankan      html  css  js  c++  java
  • android--------根据文件路径使用File类获取文件相关信息

    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 + "
    " + "文件大小:"
                            + ShowLongFileSzie(f.length()) + "
    " + "文件名称:"
                            + f.getName() + "
    " + "文件是否存在:" + f.exists() + "
    "
                            + "文件的相对路径:" + f.getPath() + "
    " + "文件的绝对路径:"
                            + f.getAbsolutePath() + "
    " + "文件可以写入:" + f.canWrite()
                            + "
    " + "是否是文件夹类型:" + 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

  • 相关阅读:
    iOS 之 创建分类
    iOS 之 动画
    iOS 倒出spa文件 打包
    js闭包
    wampserver配置多站点
    js重定向
    php obstart
    php保存远程图片
    php获取前天的昨天的日期
    weixin js接口
  • 原文地址:https://www.cnblogs.com/zhangqie/p/6237770.html
Copyright © 2011-2022 走看看