新聞中心
這篇文章將為大家詳細(xì)講解有關(guān)怎么通過(guò)java獲取文件名和擴(kuò)展名,小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。

成都創(chuàng)新互聯(lián)公司專注于網(wǎng)站建設(shè)|網(wǎng)頁(yè)維護(hù)|優(yōu)化|托管以及網(wǎng)絡(luò)推廣,積累了大量的網(wǎng)站設(shè)計(jì)與制作經(jīng)驗(yàn),為許多企業(yè)提供了網(wǎng)站定制設(shè)計(jì)服務(wù),案例作品覆蓋辦公窗簾等行業(yè)。能根據(jù)企業(yè)所處的行業(yè)與銷(xiāo)售的產(chǎn)品,結(jié)合品牌形象的塑造,量身定制品質(zhì)網(wǎng)站。
如:文件filePath = "E:\\test\\test.dxf"
1.獲取文件名
eg:獲取 test.dxf
通過(guò)file對(duì)象
import java.io.File;
public class test {
public static void main(String[] args) {
String filePath = "E:\\test\\test.dxf";
File tmpFile=new File(filePath);
String fileName=tmpFile.getName();
System.out.println(fileName);
}
}使用split
public class test {
public static void main(String[] args) {
String filePath = "E:\\test\\test.dxf";
//帶擴(kuò)展名的文件名
String temp[] = filePath.split("\\\\");
String fileName = temp[temp.length - 1];
System.out.println(fileName);
}
}使用substring
public class test {
public static void main(String[] args) {
String filePath = "E:\\test\\test.dxf";
String fileName = filePath.substring(filePath.lastIndexOf("\\")+1);
System.out.println(fileName);
}
}2.獲取不帶擴(kuò)展名的文件名
eg:獲取 test
使用substring
public class test {
public static void main(String[] args) {
String filePath = "E:\\test\\test.dxf";
String fileName = filePath.substring(filePath.lastIndexOf("\\")+1);
String name = fileName.substring(0,fileName.lastIndexOf("."));
System.out.println(name);
}
}
3.擴(kuò)展名
eg:獲取 dxf
使用substring
public class test {
public static void main(String[] args) {
String filePath = "E:\\test\\test.dxf";
String fileName = filePath.substring(filePath.lastIndexOf("\\")+1);
String name = fileName.substring(filePath.lastIndexOf(".")+1);
System.out.println(name);
}
}或
public class test {
public static void main(String[] args) {
String filePath = "E:\\test\\test.dxf";
String fileName = filePath.substring(filePath.lastIndexOf("\\")+1);
String[] strArray = fileName.split("\\.");
int suffixIndex = strArray.length -1;
System.out.println(strArray[suffixIndex]);
}
}或
public class test {
public static void main(String[] args) {
String filePath = "E:\\test\\test.dxf";
String fileName = filePath.substring(filePath.lastIndexOf("\\")+1);
System.out.println(fileName);
String extension=fileName.substring(fileName.lastIndexOf(".")+1,fileName.length());
System.out.println(extension);
}
}關(guān)于“怎么通過(guò)java獲取文件名和擴(kuò)展名”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。
文章題目:怎么通過(guò)java獲取文件名和擴(kuò)展名
文章出自:http://www.dlmjj.cn/article/pigjsp.html


咨詢
建站咨詢
