當前位置:首頁 » 編程語言 » 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();
}
}
}

熱點內容
java返回this 發布:2025-10-20 08:28:16 瀏覽:550
製作腳本網站 發布:2025-10-20 08:17:34 瀏覽:834
python中的init方法 發布:2025-10-20 08:17:33 瀏覽:540
圖案密碼什麼意思 發布:2025-10-20 08:16:56 瀏覽:719
怎麼清理微信視頻緩存 發布:2025-10-20 08:12:37 瀏覽:641
c語言編譯器怎麼看執行過程 發布:2025-10-20 08:00:32 瀏覽:958
郵箱如何填寫發信伺服器 發布:2025-10-20 07:45:27 瀏覽:213
shell腳本入門案例 發布:2025-10-20 07:44:45 瀏覽:70
怎麼上傳照片瀏覽上傳 發布:2025-10-20 07:44:03 瀏覽:762
python股票數據獲取 發布:2025-10-20 07:39:44 瀏覽:668