2014-11-02 7 views
1

파일의 길이 (바이트)를 가져 오려고하는데, 다른 파일을 테스트 할 때마다 결과는 항상 4065123.0입니다.파일 길이는 어떻게 받습니까?

이것은 내 코드의 관련 부분 :

File file = new File(path + filename); 
if(!file.exists()) { 
    System.out.println("File does not exist"); 
} else { 
    double bytes = file.length(); 
} 

어떻게 올바르게 파일의 길이를받을 수 있나요? long 값을 사용하여

+1

재현 가능한 완벽한 예를 게시하십시오. 도대체 왜 더블을 쓰고 있니? 파일에 절반의 바이트가 있다고 생각합니까? –

+0

첫째로, 왜'long'을'double'으로 저장하고 있습니까? 둘째,이 코드는 길이가 인쇄되는 곳을 볼 수 없다는 점을 제외하고는 정확합니다. 문제가 있다고 생각됩니다. –

+0

이렇게하는 방법입니다. 반복적으로 호출 할 때 이것이 작동하지 않는다면, 당신은 다른 뭔가 잘못하고있는 것입니다. –

답변

1

보십시오 : 모든 BYT의

File file =new File("myfile_in_test.java"); 

    if(file.exists()){ 

     final double bytes = file.length(); 
     final double kilobytes = (bytes/1024); 

     System.out.println("bytes : " + bytes); 
     System.out.println("kilobytes : " + kilobytes); 
    }else{ 
     System.out.println("File does not exists!"); 
    } 
+0

폴더에있는 파일의 경우이 코드를 사용하는 방법 –

+0

은 File 객체를 만드는 동안 전체 경로를 제공합니다. – SMA

-1

시도 :

long byt = file.length(); 
+0

'Double.MAX_VALUE> Long.MAX_VALUE'이라고 생각합니다. 그래서 문제가되지 않습니다. –

+1

@BoristheSpider 대용량 파일 (OS에서 지원하는 파일보다 클 수 있음)의 경우 정밀도가 떨어질 수 있습니다. –

+1

나는 long을 사용했으며 결과는 4705984196517036032이며,이 결과는 모든 파일에 대해 반복됩니다. –

-1

먼저 여야합니다. 당신이 장치의 디렉토리의 길이를 물으면 또 다른 일이 일어날 수 있습니다. 정규 파일의 경우 올바른 방법이며 변수를 올바른 유형으로 변경하십시오.

0

다음은 잘 작동하는 것을 보여주는 예입니다.

public class Main { 
    public static void main(String[] args) throws IOException, InterruptedException { 
     File file = File.createTempFile("deleteme", ".txt"); 
     file.deleteOnExit(); 
     Thread main = Thread.currentThread(); 
     Thread t = new Thread(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        do { 
         Thread.sleep(200); 
         long length = file.length(); 
         System.out.println("File " + file + " is " + length + " bytes long."); 
        } while(main.isAlive()); 
        System.out.println("Finished"); 
       } catch (InterruptedException e) { 
        System.err.println("Interrupted"); 
       } 
      } 
     }, "monitor"); 
     t.start(); 

     FileOutputStream fos = new FileOutputStream(file); 
     for(int i=0;i<2000;i++) { 
      fos.write("words words words words words words words words words words words\n".getBytes()); 
      Thread.sleep(1); 
     } 
     fos.close(); 
    } 
} 

인쇄 뭔가있다.

File /tmp/deleteme4214599935706768614.txt is 11880 bytes long. 
File /tmp/deleteme4214599935706768614.txt is 23562 bytes long. 
File /tmp/deleteme4214599935706768614.txt is 35376 bytes long. 
File /tmp/deleteme4214599935706768614.txt is 47256 bytes long. 
File /tmp/deleteme4214599935706768614.txt is 59136 bytes long. 
File /tmp/deleteme4214599935706768614.txt is 70950 bytes long. 
File /tmp/deleteme4214599935706768614.txt is 82830 bytes long. 
File /tmp/deleteme4214599935706768614.txt is 94644 bytes long. 
File /tmp/deleteme4214599935706768614.txt is 106524 bytes long. 
File /tmp/deleteme4214599935706768614.txt is 118338 bytes long. 
File /tmp/deleteme4214599935706768614.txt is 130218 bytes long. 
File /tmp/deleteme4214599935706768614.txt is 132000 bytes long. 
Finished 
관련 문제