45 int main(){ 46 char * filePath = "sam";//自己设置目录 47 vector<string> files; 48 49 ///获取该路径下的所有文件 50 getFiles(filePath, files); 51 52 char str[30]; 53 int size = files.size(); 54 for (int i = 0; i < size; i++) 55 { 56 cout << files[i].c_str...
import java.io.File; public class FileSizeExample { public static void main(String[] args) { File file = new File("path/to/your/file"); long fileSize = file.length(); System.out.println("文件大小为:" + fileSize + " 字节"); } } ``` 在上面的代码中,我们首先创建了一个File对象,...
2. 代码实现 importjava.io.File;importjava.io.IOException;importjava.nio.file.Files;publicclassFileSizeAndContentType{publicstaticvoidmain(String[]args){// 1. 创建一个 File 对象Filefile=newFile("/path/to/file");try{// 2. 使用 length() 方法获取文件大小longfileSize=file.length();System.out...
Main.java 文件 importjava.io.File;publicclassMain{publicstaticlonggetFileSize(Stringfilename){Filefile=newFile(filename);if(!file.exists()|| !file.isFile()){System.out.println("文件不存在");return-1;}returnfile.length();}publicstaticvoidmain(String[]args){longsize=getFileSize("c:/java....
3.Java取得文件大小 publicstaticlonggetFileSize(File f)throwsException {longs = 0;if(f.isDirectory()) {for(File file : f.listFiles()) { s+=getFileSize(file); } }else{ FileInputStream fis=null;try{ fis=newFileInputStream(f);
public long getFileSizes(File f) throws Exception{//取得文件大小 long s=0; if (f.exists()) { FileInputStream fis = null; fis = new FileInputStream(f); s= fis.available(); } else { f.createNewFile(); System.out.println("文件不存在"); ...
1. Check File Size usingFile.length() To get the size of the file,java.io.Fileclass provideslength()a method that returns the file’s length in bytes. We may getSecurityExceptionif the read access to the file is denied. If the file represents a directory then the return value is unspec...
}publicstaticvoidmain(finalString[] args) {finallongstart =System.nanoTime();finallongtotal =newTotalFileSizeSequential() .getTotalSizeOfFilesInDir(newFile(fileName));finallongend =System.nanoTime(); System.out.println("Total Size: " +total); ...
File file = new File("d:\hello.gif"); FileInputStream fis = null; try{ fis = new FileInputStream(file); System.out.println("文件size:"+String.valueOf(fis.available()/1000)+"k"); }catch(IOException e1){ System.out.println("IO出错!"); } }catch(FileNotFoundException e2){ System...
我们也可以使用 FileUtils 来获得。 使用的方法是: 代码语言:javascript 复制 FileUtils.sizeOf(localFileCache) localFileCache 中定义的是文件对象。 关于sizeOf 的使用为:返回指定的文件或者文件夹的大小。如果你的 File 对象为一个文件的话,这个方法将会返回文件的大小。