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

Android的java怎么获取文件大小?,第1张

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()

}

}

}

try{

System.out.println("正在链接URL")

url=new URL("http://211.64.201.201/uploadfile/nyz.mp3")

HttpURLConnection urlcon=(HttpURLConnection)url.openConnection()

/滑察/根据响应获取文件大小

fileLength=urlcon.getContentLength()

if(urlcon.getResponseCode()>=400){

System.out.println("搜含服务信漏茄器响应错误")

System.exit(-1)

}

if(fileLength<=0)

System.out.println("无法获知文件大小")

//打印信息

printMIME(urlcon)

System.out.println("文件大小为"+fileLength/1024+"K")

//获取文件名

String trueurl=urlcon.getURL().toString()

String filename=trueurl.substring(trueurl.lastIndexOf('/')+1)

fileOut=new File("D://",filename)

}

catch(MalformedURLException e){

System.err.println(e)

}

catch(IOException e){

System.err.println(e)

}

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就行了


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/tougao/12215072.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-21
下一篇 2023-05-21

发表评论

登录后才能评论

评论列表(0条)

保存