當前位置:首頁 » 編程語言 » java獲取文件大小

java獲取文件大小

發布時間: 2022-09-25 23:30:12

Ⅰ 如何用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;
}

Ⅱ Java獲取下載文件的大小

可以直接通過HttpURLConnection 的getContentLength()方法來獲取下載文件的大小。
//找到要下載的文件
url=new URL( "http://www..com/uploadfile/oracle.txt");
//根據響應獲取文件大小
HttpURLConnection urlcon=(HttpURLConnection)url.openConnection();
//獲取相應的文件長度
fileLength=urlcon.getContentLength();
備註:以上方法中url變換即可,下面的方法不用變更,即可獲取到對應下載文件的大小。

Ⅲ java如何編程實現獲取文件的長度

File file = new File("文件路徑");
System.out.println(file.length());//輸出的是文件的位元組數
這樣就可以獲得文件的長度了

Ⅳ 用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("此文件不存在");
}
}

Ⅳ 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();
}
}
}

熱點內容
c語言大數階乘 發布:2024-05-08 19:40:51 瀏覽:542
華為手機上滑自動出來清理緩存 發布:2024-05-08 19:29:21 瀏覽:282
如何取消列印機共享密碼 發布:2024-05-08 19:24:23 瀏覽:240
各層次的教育應如何配置財政資金 發布:2024-05-08 19:23:24 瀏覽:543
如何知道cpu配置是多少 發布:2024-05-08 19:23:18 瀏覽:366
python解釋 發布:2024-05-08 18:52:58 瀏覽:580
開利中央空調密碼如何修改 發布:2024-05-08 18:52:14 瀏覽:639
廠房配置應包含哪些 發布:2024-05-08 18:26:22 瀏覽:760
訪問極路由硬碟 發布:2024-05-08 18:15:15 瀏覽:718
小學生如何自學編程 發布:2024-05-08 18:14:31 瀏覽:561