当前位置:首页>开发>正文

javafile怎样获取到file文件名的后缀 jsp或者JAVA如何获取文件的后缀名字吗

2023-06-04 19:17:55 互联网 未知 开发

 javafile怎样获取到file文件名的后缀 jsp或者JAVA如何获取文件的后缀名字吗

javafile怎样获取到file文件名的后缀

给你个示例,应该看得懂吧
File f =new File("Test.txt")
String fileName=f.getName()
String prefix=fileName.substring(fileName.lastIndexOf(".") 1)
System.out.println(prefix)
}

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对文件名的几个操作,获取文件扩展名,去掉扩展名

/*
 * Java文件操作 获取文件扩展名
 *
 *  Created on: 2011-8- *      Author: blueeagle
 */
    public static String getExtensionName(String filename) { 
        if ((filename != null) && (filename.length() > 0)) { 
            int dot = filename.lastIndexOf(.) 
            if ((dot >-1) && (dot < (filename.length() - 1))) { 
                return filename.substring(dot   1) 
            } 
        } 
        return filename 
    } 
/*
 * Java文件操作 获取不带扩展名的文件名
 *
 *  Created on: 2011-8- *      Author: blueeagle
 */
    public static String getFileNameNoEx(String filename) { 
        if ((filename != null) && (filename.length() > 0)) { 
            int dot = filename.lastIndexOf(.) 
            if ((dot >-1) && (dot < (filename.length()))) { 
                return filename.substring(0, dot) 
            } 
        } 
        return filename 
    }

java.io.File类中获取文件名的方法是 。

public String getName()

返回由此抽象路径名表示的文件或目录的名称。该名称是路径名名称序列中的最后一个名称。如果路径名名称序列为空,则返回空字符串。

返回:
此抽象路径名表示的文件或目录的名称;如果路径名的名称序列为空,则返回空字符串

java的File怎么用

//假设文件在D盘,文件名为test.txt
import java.io.BufferedReader
import java.io.File
import java.io.FileReader

public class ReadText {

/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
File f=new File("E: est.txt")
BufferedReader br=new BufferedReader(new FileReader(f))
System.out.println("文本文件内容")
String s=br.readLine()
while(s!=null){
System.out.print(s)
System.out.println()
s=br.readLine()
}
br.close()
}

}

最新文章