javafile获取文件大小|Java 判断文件大小

|

Ⅰ java 判断文件大小

public static void main(String[] args) {File file = new File("D:/201709201336160.05V");getFileSize(file);}/*** 获取文件大小* @param file*/public static void getFileSize(File file) {FileInputStream fis = null;try {if(file.exists() && file.isFile()){String fileName = file.getName();fis = new FileInputStream(file);System.out.println("文件"+fileName+"的大小是:"+fis.available()+"\n");}} catch (Exception e) {e.printStackTrace();}finally{if(null!=fis){try {fis.close();} catch (IOException e) {e.printStackTrace();}}}}这个可以判断大小 输出的是byte 你转化一下MB就行了

Ⅱ 用java代码如何查看本地一个文件的大小

publicstaticvoidgetFileSize(Stringpath){//传入文件路径Filefile=newFile(path);//测试此文件是否存在if(file.exists()){//如果是文件夹//这里只检测了文件夹中第一层如果有需要可以继续递归检测if(file.isDirectory()){intsize=0;for(Filezf:file.listFiles()){if(zf.isDirectory())continue;size+=zf.length();}System.out.println("文件夹"+file.getName()+"Size:"+(size/1024f)+"kb");}else{System.out.println(file.getName()+"Size:"+(file.length()/1024f)+"kb");}//如果文件不存在}else{System.out.println("此文件不存在");}}

Ⅲ java怎么查看file的大小

FileInputStream fis=new FileInputStream(new File(filepath));System.out.println(fis.available());当然里面需要处理下异常

Ⅳ 如何用java获取网络文件的大小

你好,这边有一个示例代码,希望对你有所帮助。示例中的urlString,你可以下载之后看看是否跟打印信息大小一致。我这边是一致的。

p:所导入的包都是java.net下面的。

main方法中 直接调用这个函数即可。

staticintgetNetWorkFile(){StringurlString="https://img-blog.csdn.net/20180323154952670?watermark/2/text/==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70";intlength=0;URLurl;try{url=newURL(urlString);HttpURLConnectionurlcon=(HttpURLConnection)url.openConnection();//打开连接//根据响应获取文件大小length=urlcon.getContentLength();urlcon.disconnect();//关闭连接}catch(Exceptione){e.printStackTrace();}System.out.println(length);returnlength;}

Ⅳ Android的java怎么获取文件大小

android中的java获取文件大小的方法:import java.io.File; import java.io.FileInputStream; import java.io.FileReader; import java.io.IOException; public class FileContent { private String path = "F:\\下载说明.txt"; public FileContent() throws IOException { File f = new File(path); FileReader fileReader = new FileReader(f); BufferedReader br = new BufferedReader(fileReader); String str; while((str = br.readLine() ) != null) { System.out.println(str); } System.out.println(new FileInputStream(new File(path)).available() / 1024 / 1024 +"M"); } public static void main(String[] args) { try { new FileContent(); } catch (IOException e) { e.printStackTrace(); } } }

Ⅵ java根据文件保存路径获取文件大小

String肯定是获取路径长度啊。 File file=new File("fileinfofilepath"); file.length();


赞 (0)