2023年7月3日发(作者:)
java获取已知⽂件扩展名的代码⼀、需求分析1、获取已知⽂件的扩展名----------------------------------------------》要先读到⽂件,得到⽂件名2、的扩展名是txt, 的扩展名也是txt-------------》获取扩展名的正确性保证⼆、技术难点1、将⼀个给出的路径转换成⼀个⽂件对象,并获取到完整的⽂件名直接⽤new File()类就可以实现,然后通过getName获取到⽂件名2、怎么通过⽂件名获取到扩展名?通过对⽂件名进⾏正则表达式的分割可以得到代码实现:(PS添加了给出指定⽬录获取循环⽬录下的⽂件扩展名的函数)package a;import ;/** * 7、 编写程序获取已知⽂件的扩展名. 注意: 的扩展名是txt, 的扩展名也是txt. *
* @author 281167413@ */public class Test7 { public static void main(String[] args) { String srcPath = "D:/java/"; getFilenameExtension(srcPath); } // 获取指定⽂件的扩展名 public static void getFilenameExtension(String srcPath) { // 将源路径转换成⽂件对象 File file = new File(srcPath); if (()) { String name = e(); String[] exName = ("."); n(exName[ - 1]); } else { n("It's not a file!"); } } // 获取指定⽬录下的⽂件的扩展名 public static void getDirFilenameExtension(String srcPath) { // 将源路径转换成⽬录对象 File[] file = (new File(srcPath)).listFiles(); for (int i = 0; i < ; i++) { if (file[i].isDirectory()) { // 准备复制的源⽂件夹 srcPath = srcPath + "/" + file[i].getName(); getDirFilenameExtension(srcPath); } else { // 源⽂件 File sourceFile = file[i]; // ⽂件名字 String name = e(); String[] exName = ("."); n(exName[ - 1]); } } }}具体其它的⽅法可以参考之前发布的⽂章。
发布者:admin,转转请注明出处:http://www.yc00.com/xiaochengxu/1688382629a129685.html
评论列表(0条)