Java如何獲取相對(duì)路徑文件
Java獲取相對(duì)路徑文件
1. 文件在src下
class.getResourceAsStream(“”)獲得的是相對(duì)路徑 class.getClassLoader().getResourceAsStream(“”)加載器獲得是絕對(duì)路徑
Class.getResourceAsStream(String path)
:
- (1)不以 “/” 開頭時(shí),默認(rèn)是從此類所在的包下取資源。
- (2)以**“/”**開頭,則是從ClassPath(Src根目錄)根下獲取。
Class.getClassLoader.getResourceAsStream(String path)
:
- 默認(rèn)則是從ClassPath根下獲取,path不能以’/'開頭。
2. 文件在src同級(jí)目錄下
InputStream in = new BufferedInputStream(new FileInputStream("./test/aaa.txt"));
1.FileTest.class.getResource("")
得到的是當(dāng)前類FileTest.class文件的URI目錄。不包括自己!
如:file:/D:/java/eclipse32/workspace/jbpmtest3/bin/com/test/
2.FileTest.class.getResource("/")
得到的是當(dāng)前的classpath的絕對(duì)URI路徑。
如:file:/D:/java/eclipse32/workspace/jbpmtest3/bin/
3.Thread.currentThread().getContextClassLoader().getResource("")
得到的也是當(dāng)前ClassPath的絕對(duì)URI路徑。
如:file:/D:/java/eclipse32/workspace/jbpmtest3/bin/
4.FileTest.class.getClassLoader().getResource("")
得到的也是當(dāng)前ClassPath的絕對(duì)URI路徑。
如:file:/D:/java/eclipse32/workspace/jbpmtest3/bin/
5.ClassLoader.getSystemResource("")
得到的也是當(dāng)前ClassPath的絕對(duì)URI路徑。
如:file:/D:/java/eclipse32/workspace/jbpmtest3/bin/
我推薦使用Thread.currentThread().getContextClassLoader().getResource("")來(lái)得到當(dāng)前的classpath的絕對(duì)路徑的URI表示法。
Java獲取文件大?。ㄎ募?、路徑)
文件大?。窂剑?/h3>
/**
* 文件大小--路徑
*
* @return
*/
public static long getFileLength(String downloadUrl) {
if(downloadUrl == null || "".equals(downloadUrl)){
return 0L ;
}
URL url = null;
try {
url = new URL(downloadUrl);
} catch (MalformedURLException e) {
e.printStackTrace();
return 0L;
}
HttpURLConnection conn = null;
try {
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("HEAD");
conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows 7; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.73 Safari/537.36 YNoteCef/5.8.0.1 (Windows)");
return (long) conn.getContentLength();
} catch (IOException e) {
return 0L;
} finally {
conn.disconnect();
}
}
/** * 文件大小--路徑 * * @return */ public static long getFileLength(String downloadUrl) { if(downloadUrl == null || "".equals(downloadUrl)){ return 0L ; } URL url = null; try { url = new URL(downloadUrl); } catch (MalformedURLException e) { e.printStackTrace(); return 0L; } HttpURLConnection conn = null; try { conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("HEAD"); conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows 7; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.73 Safari/537.36 YNoteCef/5.8.0.1 (Windows)"); return (long) conn.getContentLength(); } catch (IOException e) { return 0L; } finally { conn.disconnect(); } }
文件大?。ㄎ募?/h3>
/**
* 文件大小--文件
*
* @return
*/
public static int getFileSize(File file) {
int fileSize = 0;
FileInputStream fis = null;
try {
if (file.exists() && file.isFile()) {
String fileName = file.getName();
fis = new FileInputStream(file);
fileSize = fis.available();
System.out.println("文件" + fileName + "的大小是:" + fis.available() + "\n");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != fis) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return fileSize;
}
/** * 文件大小--文件 * * @return */ public static int getFileSize(File file) { int fileSize = 0; FileInputStream fis = null; try { if (file.exists() && file.isFile()) { String fileName = file.getName(); fis = new FileInputStream(file); fileSize = fis.available(); System.out.println("文件" + fileName + "的大小是:" + fis.available() + "\n"); } } catch (Exception e) { e.printStackTrace(); } finally { if (null != fis) { try { fis.close(); } catch (IOException e) { e.printStackTrace(); } } } return fileSize; }
測(cè)試下
public static void main(String[] args) { File file = new File("C:\\Users\\xuzh\\Desktop\\6f3c0052d684451a92e7fb5b55eda9fd.jpg"); getFileSize(file); }
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Servlet的兩種創(chuàng)建方式(xml?注解)示例詳解
這篇文章主要為大家介紹了Servlet的兩種創(chuàng)建方式(xml?注解)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08淺析Java中Map與HashMap,Hashtable,HashSet的區(qū)別
HashMap和Hashtable兩個(gè)類都實(shí)現(xiàn)了Map接口,二者保存K-V對(duì)(key-value對(duì));HashSet則實(shí)現(xiàn)了Set接口,性質(zhì)類似于集合2013-09-09解決bootstrap.yml不生效,無(wú)法優(yōu)先于application.yml文件加載問題
文章主要討論了在Spring Boot項(xiàng)目中,`bootstrap.yml`文件無(wú)法優(yōu)先于`application.yml`文件加載的問題,原因是缺少了`nacos-config`依賴,且需要確保Spring Boot版本與`nacos-config`版本匹配,作者希望通過分享個(gè)人經(jīng)驗(yàn),幫助他人解決類似問題2024-12-12Java創(chuàng)建圖形用戶界面(GUI)入門詳細(xì)指南(Swing庫(kù)JFrame類)
這篇文章主要介紹了使用Java?Swing庫(kù)的JFrame類創(chuàng)建基本的圖形用戶界面,包括窗口的創(chuàng)建、組件的添加和事件處理,通過代碼講解了如何設(shè)置窗口大小、添加按鈕及處理按鈕點(diǎn)擊事件,適合初學(xué)者學(xué)習(xí)和開發(fā)GUI應(yīng)用程序,需要的朋友可以參考下2024-11-11使用Java將DOCX文檔解析為Markdown文檔的代碼實(shí)現(xiàn)
在現(xiàn)代文檔處理中,Markdown(MD)因其簡(jiǎn)潔的語(yǔ)法和良好的可讀性,逐漸成為開發(fā)者、技術(shù)寫作者和內(nèi)容創(chuàng)作者的首選格式,然而,許多文檔仍然以Microsoft Word的DOCX格式保存,本文將介紹如何使用Java和相關(guān)庫(kù)將DOCX文檔解析為Markdown文檔,需要的朋友可以參考下2025-04-04spring boot補(bǔ)習(xí)系列之幾種scope詳解
這篇文章主要給大家介紹了關(guān)于spring boot補(bǔ)習(xí)系列之幾種scope的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用spring boot具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-07-07spring-boot集成spring-security的oauth2實(shí)現(xiàn)github登錄網(wǎng)站的示例
本篇文章主要介紹了spring-boot集成spring-security的oauth2實(shí)現(xiàn)github登錄網(wǎng)站的示例,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-10-10