『壹』 java獲取文件名, 提取字元串中局部字元串。拆分文件名及後綴
正則不是更簡單
([^\\/]+)\.([^\\/]+)
$1是文件名,$2是擴展名 ,下面是Javascript的測試代碼:
<script type="text/javascript">
var a="c:\\windows\\abc.txt";
var reg = /([^\\/]+)\.([^\\/]+)/i;
reg.test(a);
alert(RegExp.$1);
alert(RegExp.$2);
</script>
『貳』 javafile怎樣獲取到file文件名的後綴
演示:
File f =new File("Test.txt");
String fileName=f.getName();
String prefix=fileName.substring(fileName.lastIndexOf(".")+1);
System.out.println(prefix);
}
JAVA一般存在兩種文件格式,如下:
1.*.java文件是保存源代碼的文本文件 (*代表類名)
使用 javac *.java可以編譯該文件
使用 java *可以運行該類
2.*.class是用於保存 Java類的 二進制編碼以及Class對象,每一個 Java類都有一個解釋該類特徵的 Class對象。*.jar文件 是一種壓縮文件格式
『叄』 java文件名提取
java獲取文件名方法有三種代碼如下展示:
// 舉例:
String fName =" G:\\Java_Source\\navigation_tigra_menu\\demo1\\img\\lev1_arrow.gif ";
// 方法一:
File tempFile =new File( fName.trim());
String fileName = tempFile.getName();
System.out.println("方法一:fileName = " + fileName);
// 方法二:
fName = fName.trim();
// fileName = fName.substring(fName.lastIndexOf("/")+1);
// 或者
fileName = fName.substring(fName.lastIndexOf("\\")+1);
System.out.println("方法二:fileName = " + fileName);
// 方法三:
fName = fName.trim();
String temp[] = fName.split("\\\\"); /**split裡面必須是正則表達式,"\\"的作用是對字元串轉義*/
//temp[] = [G:, Java_Source, navigation_tigra_menu, demo1, img, lev1_arrow.gif]
System.out.println("temp[] = " + Arrays.toString(temp));
fileName = temp[temp.length-1];
System.out.println("方法三:fileName = " + fileName);
『肆』 java對文件名的幾個操作,獲取文件擴展名,去掉擴展名
/*
*Java文件操作獲取文件擴展名
*
*Createdon:2011-8-2
*Author:blueeagle
*/
(Stringfilename){
if((filename!=null)&&(filename.length()>0)){
intdot=filename.lastIndexOf('.');
if((dot>-1)&&(dot<(filename.length()-1))){
returnfilename.substring(dot+1);
}
}
returnfilename;
}
/*
*Java文件操作獲取不帶擴展名的文件名
*
*Createdon:2011-8-2
*Author:blueeagle
*/
(Stringfilename){
if((filename!=null)&&(filename.length()>0)){
intdot=filename.lastIndexOf('.');
if((dot>-1)&&(dot<(filename.length()))){
returnfilename.substring(0,dot);
}
}
returnfilename;
}
『伍』 java獲取上傳的圖片後綴名出錯 變成.image%3A1245499 請問什麼原因呢怎麼解決呢
應該是文件名出現空格了了,%3A是空格的意思,被轉為unicode編碼了
『陸』 jsp或者JAVA如何獲取文件的後綴名字嗎
public String getFileType(String fileUri){
File file = new File(fileUri);
String fineName = file.getName();
String fileType = fileName.substring(fileName.lastIndexOf(\".\")+1,fileName.length())
return fileType;
}
『柒』 Java Base64 直接獲取文件後綴
import sun.misc.BASE64Encoder; import sun.misc.BASE64Decoder; // 將 s 進行 BASE64 編碼 public static String getBASE64(String s) { if (s == null) return null; return (new sun.misc.BASE64Encoder()).encode( s.getBytes() ); }敞飢搬渴植韭邦血鮑摩 // 將 BASE64 編碼的字元串 s 進行解碼 public static String getFromBASE64(String s) { if (s == null) return null; BASE64Decoder decoder = new BASE64Decoder(); try { byte[] b = decoder.decodeBuffer(s); return new String(b); } catch (Exception e) { return null; } }