亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

關(guān)于Java文件路徑問(wèn)題

瀏覽:66日期:2024-06-06 17:45:53
內(nèi)容: 1.如何獲得當(dāng)前文件路徑常用:字符串類型:System.getProperty('user.dir');綜合:package com.zcjl.test.base;import java.io.File;public class Test { public static void main(String[] args) throws Exception { System.out.println( Thread.currentThread().getContextClassLoader().getResource('')); System.out.println(Test.class.getClassLoader().getResource('')); System.out.println(ClassLoader.getSystemResource('')); System.out.println(Test.class.getResource('')); System.out.println(Test.class.getResource('/')); System.out.println(new File('').getAbsolutePath()); System.out.println(System.getProperty('user.dir')); }}2.Web服務(wù)中(1).WeblogicWebApplication的系統(tǒng)文件根目錄是你的weblogic安裝所在根目錄。例如:如果你的weblogic安裝在c:beaweblogic700.....那么,你的文件根路徑就是c:.所以,有兩種方式能夠讓你訪問(wèn)你的服務(wù)器端的文件:a.使用絕對(duì)路徑:比如將你的參數(shù)文件放在c:yourconfigyourconf.properties,直接使用 new FileInputStream('yourconfig/yourconf.properties');b.使用相對(duì)路徑:相對(duì)路徑的根目錄就是你的webapplication的根路徑,即WEB-INF的上一級(jí)目錄,將你的參數(shù)文件放在yourwebappyourconfigyourconf.properties,這樣使用:new FileInputStream('./yourconfig/yourconf.properties');這兩種方式均可,自己選擇。(2).Tomcat在類中輸出System.getProperty('user.dir');顯示的是%Tomcat_Home%/bin(3).Resin不是你的JSP放的相對(duì)路徑,是JSP引擎執(zhí)行這個(gè)JSP編譯成SERVLET的路徑為根.比如用新建文件法測(cè)試File f = new File('a.htm');這個(gè)a.htm在resin的安裝目錄下 (4).如何讀相對(duì)路徑哪?在Java文件中g(shù)etResource或getResourceAsStream均可例:getClass().getResourceAsStream(filePath);//filePath可以是'/filename',這里的/代表web發(fā)布根路徑下WEB-INF/classes(5).獲得文件真實(shí)路徑string file_real_path=request.getRealPath('mypath/filename'); 通常使用request.getRealPath('/'); 3.文件操作的類import java.io.*;import java.net.*;import java.util.*;//import javax.swing.filechooser.*;//import org.jr.swing.filter.*;/*** 此類中封裝一些常用的文件操作。* 所有方法都是靜態(tài)方法,不需要生成此類的實(shí)例,* 為避免生成此類的實(shí)例,構(gòu)造方法被申明為private類型的。* @since 0.1*/public class FileUtil { /** * 私有構(gòu)造方法,防止類的實(shí)例化,因?yàn)楣ぞ哳惒恍枰獙?shí)例化。 */ private FileUtil() { } /** * 修改文件的最后訪問(wèn)時(shí)間。 * 如果文件不存在則創(chuàng)建該文件。 * 目前這個(gè)方法的行為方式還不穩(wěn)定,主要是方法有些信息輸出,這些信息輸出是否保留還在考慮中。 * @param file 需要修改最后訪問(wèn)時(shí)間的文件。 * @since 0.1 */ public static void touch(File file) { long currentTime = System.currentTimeMillis(); if (!file.exists()) { System.err.println('file not found:' + file.getName()); System.err.println('Create a new file:' + file.getName()); try { if (file.createNewFile()) { // System.out.println('Succeeded!'); } else { // System.err.println('Create file failed!'); } } catch (IOException e) { // System.err.println('Create file failed!'); e.printStackTrace(); } } boolean result = file.setLastModified(currentTime); if (!result) { // System.err.println('touch failed: ' + file.getName()); } } /** * 修改文件的最后訪問(wèn)時(shí)間。 * 如果文件不存在則創(chuàng)建該文件。 * 目前這個(gè)方法的行為方式還不穩(wěn)定,主要是方法有些信息輸出,這些信息輸出是否保留還在考慮中。 * @param fileName 需要修改最后訪問(wèn)時(shí)間的文件的文件名。 * @since 0.1 */ public static void touch(String fileName) { File file = new File(fileName); touch(file); } /** * 修改文件的最后訪問(wèn)時(shí)間。 * 如果文件不存在則創(chuàng)建該文件。 * 目前這個(gè)方法的行為方式還不穩(wěn)定,主要是方法有些信息輸出,這些信息輸出是否保留還在考慮中。 * @param files 需要修改最后訪問(wèn)時(shí)間的文件數(shù)組。 * @since 0.1 */ public static void touch(File[] files) { for (int i = 0; i < files.length; i++) { touch(files); } } /** * 修改文件的最后訪問(wèn)時(shí)間。 * 如果文件不存在則創(chuàng)建該文件。 * 目前這個(gè)方法的行為方式還不穩(wěn)定,主要是方法有些信息輸出,這些信息輸出是否保留還在考慮中。 * @param fileNames 需要修改最后訪問(wèn)時(shí)間的文件名數(shù)組。 * @since 0.1 */ public static void touch(String[] fileNames) { File[] files = new File[fileNames.length]; for (int i = 0; i < fileNames.length; i++) { files = new File(fileNames); } touch(files); } /** * 判斷指定的文件是否存在。 * @param fileName 要判斷的文件的文件名 * @return 存在時(shí)返回true,否則返回false。 * @since 0.1 */ public static boolean isFileExist(String fileName) { return new File(fileName).isFile(); } /** * 創(chuàng)建指定的目錄。 * 如果指定的目錄的父目錄不存在則創(chuàng)建其目錄書(shū)上所有需要的父目錄。 * 注意:可能會(huì)在返回false的時(shí)候創(chuàng)建部分父目錄。 * @param file 要?jiǎng)?chuàng)建的目錄 * @return 完全創(chuàng)建成功時(shí)返回true,否則返回false。 * @since 0.1 */ public static boolean makeDirectory(File file) { File parent = file.getParentFile(); if (parent != null) { return parent.mkdirs(); } return false; } /** * 創(chuàng)建指定的目錄。 * 如果指定的目錄的父目錄不存在則創(chuàng)建其目錄書(shū)上所有需要的父目錄。 * 注意:可能會(huì)在返回false的時(shí)候創(chuàng)建部分父目錄。 * @param fileName 要?jiǎng)?chuàng)建的目錄的目錄名 * @return 完全創(chuàng)建成功時(shí)返回true,否則返回false。 * @since 0.1 */ public static boolean makeDirectory(String fileName) { File file = new File(fileName); return makeDirectory(file); } /** * 清空指定目錄中的文件。 * 這個(gè)方法將盡可能刪除所有的文件,但是只要有一個(gè)文件沒(méi)有被刪除都會(huì)返回false。 * 另外這個(gè)方法不會(huì)迭代刪除,即不會(huì)刪除子目錄及其內(nèi)容。 * @param directory 要清空的目錄 * @return 目錄下的所有文件都被成功刪除時(shí)返回true,否則返回false. * @since 0.1 */ public static boolean emptyDirectory(File directory) { boolean result = false; File[] entries = directory.listFiles(); for (int i = 0; i < entries.length; i++) { if (!entries.delete()) { result = false; } } return true; } /** * 清空指定目錄中的文件。 * 這個(gè)方法將盡可能刪除所有的文件,但是只要有一個(gè)文件沒(méi)有被刪除都會(huì)返回false。 * 另外這個(gè)方法不會(huì)迭代刪除,即不會(huì)刪除子目錄及其內(nèi)容。 * @param directoryName 要清空的目錄的目錄名 * @return 目錄下的所有文件都被成功刪除時(shí)返回true,否則返回false。 * @since 0.1 */ public static boolean emptyDirectory(String directoryName) { File dir = new File(directoryName); return emptyDirectory(dir); } /** * 刪除指定目錄及其中的所有內(nèi)容。 * @param dirName 要?jiǎng)h除的目錄的目錄名 * @return 刪除成功時(shí)返回true,否則返回false。 * @since 0.1 */ public static boolean deleteDirectory(String dirName) { return deleteDirectory(new File(dirName)); } /** * 刪除指定目錄及其中的所有內(nèi)容。 * @param dir 要?jiǎng)h除的目錄 * @return 刪除成功時(shí)返回true,否則返回false。 * @since 0.1 */ public static boolean deleteDirectory(File dir) { if ( (dir == null) || !dir.isDirectory()) { throw new IllegalArgumentException('Argument ' + dir + ' is not a directory. '); } File[] entries = dir.listFiles(); int sz = entries.length; for (int i = 0; i < sz; i++) { if (entries.isDirectory()) { if (!deleteDirectory(entries)) { return false; } } else { if (!entries.delete()) { return false; } } } if (!dir.delete()) { return false; } return true; } /** * 返回文件的URL地址。 * @param file 文件 * @return 文件對(duì)應(yīng)的的URL地址 * @throws MalformedURLException * @since 0.4 * @deprecated 在實(shí)現(xiàn)的時(shí)候沒(méi)有注意到File類本身帶一個(gè)toURL方法將文件路徑轉(zhuǎn)換為URL。 * 請(qǐng)使用File.toURL方法。 */ public static URL getURL(File file) throws MalformedURLException { String fileURL = 'file:/' + file.getAbsolutePath(); URL url = new URL(fileURL); return url; } /** * 從文件路徑得到文件名。 * @param filePath 文件的路徑,可以是相對(duì)路徑也可以是絕對(duì)路徑 * @return 對(duì)應(yīng)的文件名 * @since 0.4 */ public static String getFileName(String filePath) { File file = new File(filePath); return file.getName(); } /** * 從文件名得到文件絕對(duì)路徑。 * @param fileName 文件名 * @return 對(duì)應(yīng)的文件路徑 * @since 0.4 */ public static String getFilePath(String fileName) { File file = new File(fileName); return file.getAbsolutePath(); } /** * 將DOS/Windows格式的路徑轉(zhuǎn)換為UNIX/Linux格式的路徑。 * 其實(shí)就是將路徑中的''全部換為'/',因?yàn)樵谀承┣闆r下我們轉(zhuǎn)換為這種方式比較方便, * 某中程度上說(shuō)'/'比''更適合作為路徑分隔符,而且DOS/Windows也將它當(dāng)作路徑分隔符。 * @param filePath 轉(zhuǎn)換前的路徑 * @return 轉(zhuǎn)換后的路徑 * @since 0.4 */ public static String toUNIXpath(String filePath) { return filePath.replace('', '/'); } /** * 從文件名得到UNIX風(fēng)格的文件絕對(duì)路徑。 * @param fileName 文件名 * @return 對(duì)應(yīng)的UNIX風(fēng)格的文件路徑 * @since 0.4 * @see #toUNIXpath(String filePath) toUNIXpath */ public static String getUNIXfilePath(String fileName) { File file = new File(fileName); return toUNIXpath(file.getAbsolutePath()); } /** * 得到文件的類型。 * 實(shí)際上就是得到文件名中最后一個(gè)“.后面的部分。 * @param fileName 文件名 * @return 文件名中的類型部分 * @since 0.5 */ public static String getTypePart(String fileName) { int point = fileName.lastIndexOf('.'); int length = fileName.length(); if (point == -1 || point == length - 1) { return ''; } else { return fileName.substring(point + 1, length); } } /** * 得到文件的類型。 * 實(shí)際上就是得到文件名中最后一個(gè)“.后面的部分。 * @param file 文件 * @return 文件名中的類型部分 * @since 0.5 */ public static String getFileType(File file) { return getTypePart(file.getName()); } /** * 得到文件的名字部分。 * 實(shí)際上就是路徑中的最后一個(gè)路徑分隔符后的部分。 * @param fileName 文件名 * @return 文件名中的名字部分 * @since 0.5 */ public static String getNamePart(String fileName) { int point = getPathLsatIndex(fileName); int length = fileName.length(); if (point == -1) { return fileName; } else if (point == length - 1) { int secondPoint = getPathLsatIndex(fileName, point - 1); if (secondPoint == -1) { if (length == 1) { return fileName; } else { return fileName.substring(0, point); } } else { return fileName.substring(secondPoint + 1, point); } } else { return fileName.substring(point + 1); } } /** * 得到文件名中的父路徑部分。 * 對(duì)兩種路徑分隔符都有效。 * 不存在時(shí)返回''。 * 如果文件名是以路徑分隔符結(jié)尾的則不考慮該分隔符,例如'/path/'返回''。 * @param fileName 文件名 * @return 父路徑,不存在或者已經(jīng)是父目錄時(shí)返回'' * @since 0.5 */ public static String getPathPart(String fileName) { int point = getPathLsatIndex(fileName); int length = fileName.length(); if (point == -1) { return ''; } else if (point == length - 1) { int secondPoint = getPathLsatIndex(fileName, point - 1); if (secondPoint == -1) { return ''; } else { return fileName.substring(0, secondPoint); } } else { return fileName.substring(0, point); } } /** * 得到路徑分隔符在文件路徑中首次出現(xiàn)的位置。 * 對(duì)于DOS或者UNIX風(fēng)格的分隔符都可以。 * @param fileName 文件路徑 * @return 路徑分隔符在路徑中首次出現(xiàn)的位置,沒(méi)有出現(xiàn)時(shí)返回-1。 * @since 0.5 */ public static int getPathIndex(String fileName) { int point = fileName.indexOf('/'); if (point == -1) { point = fileName.indexOf(''); } return point; } /** * 得到路徑分隔符在文件路徑中指定位置后首次出現(xiàn)的位置。 * 對(duì)于DOS或者UNIX風(fēng)格的分隔符都可以。 * @param fileName 文件路徑 * @param fromIndex 開(kāi)始查找的位置 * @return 路徑分隔符在路徑中指定位置后首次出現(xiàn)的位置,沒(méi)有出現(xiàn)時(shí)返回-1。 * @since 0.5 */ public static int getPathIndex(String fileName, int fromIndex) { int point = fileName.indexOf('/', fromIndex); if (point == -1) { point = fileName.indexOf('', fromIndex); } return point; } /** * 得到路徑分隔符在文件路徑中最后出現(xiàn)的位置。 * 對(duì)于DOS或者UNIX風(fēng)格的分隔符都可以。 * @param fileName 文件路徑 * @return 路徑分隔符在路徑中最后出現(xiàn)的位置,沒(méi)有出現(xiàn)時(shí)返回-1。 * @since 0.5 */ public static int getPathLsatIndex(String fileName) { int point = fileName.lastIndexOf('/'); if (point == -1) { point = fileName.lastIndexOf(''); } return point; } /** * 得到路徑分隔符在文件路徑中指定位置前最后出現(xiàn)的位置。 * 對(duì)于DOS或者UNIX風(fēng)格的分隔符都可以。 * @param fileName 文件路徑 * @param fromIndex 開(kāi)始查找的位置 * @return 路徑分隔符在路徑中指定位置前最后出現(xiàn)的位置,沒(méi)有出現(xiàn)時(shí)返回-1。 * @since 0.5 */ public static int getPathLsatIndex(String fileName, int fromIndex) { int point = fileName.lastIndexOf('/', fromIndex); if (point == -1) { point = fileName.lastIndexOf('', fromIndex); } return point; } /** * 將文件名中的類型部分去掉。 * @param filename 文件名 * @return 去掉類型部分的結(jié)果 * @since 0.5 */ public static String trimType(String filename) { int index = filename.lastIndexOf('.'); if (index != -1) { return filename.substring(0, index); } else { return filename; } } /** * 得到相對(duì)路徑。 * 文件名不是目錄名的子節(jié)點(diǎn)時(shí)返回文件名。 * @param pathName 目錄名 * @param fileName 文件名 * @return 得到文件名相對(duì)于目錄名的相對(duì)路徑,目錄下不存在該文件時(shí)返回文件名 * @since 0.5 */ public static String getSubpath(String pathName,String fileName) { int index = fileName.indexOf(pathName); if (index != -1) { return fileName.substring(index + pathName.length() + 1); } else { return fileName; } }} 4.遺留問(wèn)題目前new FileInputStream()只會(huì)使用絕對(duì)路徑,相對(duì)沒(méi)用過(guò),因?yàn)橐鄬?duì)于web服務(wù)器地址,比較麻煩還不如寫(xiě)個(gè)配置文件來(lái)的快哪5.按Java文件類型分類讀取配置文件配置文件是應(yīng)用系統(tǒng)中不可缺少的,可以增加程序的靈活性。java.util.Properties是從jdk1.2就有的類,一直到現(xiàn)在都支持load()方法,jdk1.4以后save(output,string) ->store(output,string)。如果只是單純的讀,根本不存在煩惱的問(wèn)題。web層可以通過(guò)Thread.currentThread().getContextClassLoader().getResourceAsStream('xx.properties')獲取;Application可以通過(guò)new FileInputStream('xx.properties');直接在classes一級(jí)獲取。關(guān)鍵是有時(shí)我們需要通過(guò)web修改配置文件,我們不能將路徑寫(xiě)死了。經(jīng)過(guò)測(cè)試覺(jué)得有以下心得:1.servlet中讀寫(xiě)。如果運(yùn)用Struts或者Servlet可以直接在初始化參數(shù)中配置,調(diào)用時(shí)根據(jù)servlet的getRealPath('/')獲取真實(shí)路徑,再根據(jù)String file = this.servlet.getInitParameter('abc');獲取相對(duì)的WEB-INF的相對(duì)路徑。例:InputStream input = Thread.currentThread().getContextClassLoader().getResourceAsStream('abc.properties');Properties prop = new Properties();prop.load(input);input.close();OutputStream out = new FileOutputStream(path);prop.setProperty('abc', “test');prop.store(out, “–test–');out.close();2.直接在jsp中操作,通過(guò)jsp內(nèi)置對(duì)象獲取可操作的絕對(duì)地址。例:// jsp頁(yè)面String path = pageContext.getServletContext().getRealPath('/');String realPath = path+'/WEB-INF/classes/abc.properties';//java 程序InputStream in = getClass().getClassLoader().getResourceAsStream('abc.properties'); // abc.properties放在webroot/WEB-INF/classes/目錄下prop.load(in);in.close();OutputStream out = new FileOutputStream(path); // path為通過(guò)頁(yè)面?zhèn)魅氲穆窂絧rop.setProperty('abc', “abcccccc');prop.store(out, “–test–');out.close();3.只通過(guò)Java程序操作資源文件InputStream in = new FileInputStream('abc.properties'); // 放在classes同級(jí)OutputStream out = new FileOutputStream('abc.properties'); Java, java, J2SE, j2se, J2EE, j2ee, J2ME, j2me, ejb, ejb3, JBOSS, jboss, spring, hibernate, jdo, struts, webwork, ajax, AJAX, mysql, MySQL, Oracle, Weblogic, Websphere, scjp, scjd 1.如何?
標(biāo)簽: Java
相關(guān)文章:
主站蜘蛛池模板: 亚洲精品亚洲人成人网 | 精品樱空桃一区二区三区 | 久久在线免费观看 | 日韩一级片视频 | 国产香蕉视频在线 | 国产午夜精品不卡观看 | 黄色小网站在线观看 | 国产99视频精品免费视频免里 | 91精品国产美女福到在线不卡 | 国产精品麻豆 | 久热香蕉精品视频在线播放 | 午夜欧美成人久久久久久 | 中文字幕永久在线视频 | 99久久www免费 | 天天看天天摸色天天综合网 | 小明看看在线观看 | 大美女久久久久久j久久 | 一级毛片一级片 | 国产一区二区三区四区小蝌蚪 | 国产精品第二页在线播放 | 午夜精品久久久久久99热 | 中文字字幕码一二三区 | 青青青青手机在线视频观看国产 | 一级不卡毛片免费 | 99人中文字幕亚洲区 | 视频二区肥岳精品推荐 | 可以免费观看的黄色网址 | 91亚洲精品一区二区在线观看 | 精品国产一区二区三区不卡在线 | 欧洲成人爽视频在线观看 | 亚洲国产精品成人午夜在线观看 | 黄色小视频在线观看 | 中文字幕精品一区二区日本大胸 | 日本一区二区三区久久精品 | 老司机一级毛片 | 日本一级特级毛片视频 | 一级毛片日本特黄97人人 | 毛片大全在线观看 | 永久免费在线 | 国产成人精品免费视频网页大全 | 永久免费观看午夜视频在线 |