-2

I have a problem getting the size of a file. I have the following code:

    File file = new File(path);
    FilenameFilter mediafilefilter = new FilenameFilter(){
        private String[] filter = {".txt"};
        @Override  
        public boolean accept(File dir, String filename) { 
            for(int i= 0;i< filter.length ; i++){  
                if(filename.indexOf(filter[i]) != -1)return true;  
            } 
            return false; 
        }
     };

     File[] flies = file.listFiles(mediafilefilter);

     if (files != null) {
        {
              if (files.length > 0)
                {
                      System.out.println("Totol is :" + files.length);
                      for (int j = 0; j < files.length; j++) //not work
                 }
        }
     }

some text file is 0 byte

Chunhua
  • 41
  • 1
  • 5
  • 3
    Possible duplicate of [how do I get file size of temp file in android?](https://stackoverflow.com/questions/10202805/how-do-i-get-file-size-of-temp-file-in-android) – AskNilesh Oct 26 '17 at 08:35
  • File().length() returns file size you can check with it, also if you wish to display file size in mb you can use File().length()/(1024*1024) ; – Thracian Oct 26 '17 at 08:37
  • 2
    Possible duplicate of [Get the file size in android sdk?](https://stackoverflow.com/questions/7131930/get-the-file-size-in-android-sdk) – Hexfire Oct 26 '17 at 08:46
  • I try to use : int file_size = Integer.parseInt(String.valueOf(files.length/1024)); but I can't build. – Chunhua Oct 26 '17 at 09:02

2 Answers2

1

Like that

list[0].length()/1024

list[0] is the first file in your array

AbuQauod
  • 919
  • 14
  • 30
0
   public long getFileSizes(File f) throws Exception{
                long s=0;
                if (f.exists()) {
                 FileInputStream fis = null;
                    fis = new FileInputStream(f);
                    s= fis.available();
                } 
            return s;
        }
Chunhua
  • 41
  • 1
  • 5