22 EYLÜL 2008, PAZARTESİ
java verimli bir ÅŸekilde dosya boyutunu almak
Googling, java.io.File#length() kullanarak yavaş görüyorum.
FileChannel da mevcuttur size() bir yöntemi vardır.
Java etkin bir şekilde dosya boyutunu almak için var mı?
CEVAP
22 EYLÜL 2008, PAZARTESİ
Bunu kod ile aşağıda ölçmeye çalıştım:
İçin çalışır = 1 ve yineleme = 1 URL yöntemi en hızlı çoğu kez kanal tarafından takip edilir. Yaklaşık 10 kez biraz bekledikten taze ile bu işletiyorum. Bir kez erişmek için, URL kullanarak düşünebildiğim en hızlı yoludur:
LENGTH sum: 10626, per Iteration: 10626.0
CHANNEL sum: 5535, per Iteration: 5535.0
URL sum: 660, per Iteration: 660.0
İçin çalışır = 5 ve yineleme = 50 farklı resim çizer.
LENGTH sum: 39496, per Iteration: 157.984
CHANNEL sum: 74261, per Iteration: 297.044
URL sum: 95534, per Iteration: 382.136
Dosya kanallar ve URL bazı yükü varken dosya sistemi çağrıları önbelleğe olmalıdır.
Kod:
import java.io.*;
import java.net.*;
import java.util.*;
public enum FileSizeBench {
LENGTH {
@Override
public long getResult() throws Exception {
File me = new File(FileSizeBench.class.getResource(
"FileSizeBench.class").getFile());
return me.length();
}
},
CHANNEL {
@Override
public long getResult() throws Exception {
FileInputStream fis = null;
try {
File me = new File(FileSizeBench.class.getResource(
"FileSizeBench.class").getFile());
fis = new FileInputStream(me);
return fis.getChannel().size();
} finally {
fis.close();
}
}
},
URL {
@Override
public long getResult() throws Exception {
InputStream stream = null;
try {
URL url = FileSizeBench.class
.getResource("FileSizeBench.class");
stream = url.openStream();
return stream.available();
} finally {
stream.close();
}
}
};
public abstract long getResult() throws Exception;
public static void main(String[] args) throws Exception {
int runs = 5;
int iterations = 50;
EnumMap<FileSizeBench, Long> durations = new EnumMap<FileSizeBench, Long>(FileSizeBench.class);
for (int i = 0; i < runs; i ) {
for (FileSizeBench test : values()) {
if (!durations.containsKey(test)) {
durations.put(test, 0l);
}
long duration = testNow(test, iterations);
durations.put(test, durations.get(test) duration);
// System.out.println(test " took: " duration ", per iteration: " ((double)duration / (double)iterations));
}
}
for (Map.Entry<FileSizeBench, Long> entry : durations.entrySet()) {
System.out.println();
System.out.println(entry.getKey() " sum: " entry.getValue() ", per Iteration: " ((double)entry.getValue() / (double)(runs * iterations)));
}
}
private static long testNow(FileSizeBench test, int iterations)
throws Exception {
long result = -1;
long before = System.nanoTime();
for (int i = 0; i < iterations; i ) {
if (result == -1) {
result = test.getResult();
//System.out.println(result);
} else if ((result = test.getResult()) != result) {
throw new Exception("variance detected!");
}
}
return (System.nanoTime() - before) / 1000;
}
}
Bunu PaylaÅŸ:

Java'in Dosya bir MD5 SaÄŸlama alm...
'in Bir Dosya almak Mıme Java Yaz...
En verimli şekilde artış Java Göster b...
Kopyalama aklı başında, güvenli ve ver...
Diskteki dosya boyutunu almak...