‘壹’ 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; } }