zoukankan      html  css  js  c++  java
  • java获取文件名的三种方法

    import java.io.File;
    import java.util.Arrays;

    public class FileName {


    /**
    * @param args
    */
    public static void main(String[] args) {

    String img_path="image/shop/o/0291514245090552868682.jpg";
    if (img_path == null || img_path.indexOf(".") == -1){
    //如果图片地址为null或者地址中没有"."就返回""
    }
    String result= img_path.substring(img_path.lastIndexOf(".") + 1).
    trim().toLowerCase();
    System.out.println(result);


    // 举例:
    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);

    }

    }

    来自:http://yinny.iteye.com/blog/1508699

    http://rogerfederer.iteye.com/blog/1187798

  • 相关阅读:
    winform程序,备份数据库+并压缩+并删除以前的备份
    冒泡排序
    存储过程和SQL语句比较
    简单的firebird插入速度测试
    [收藏转]由于CredSSP加密Oracle修正 导致远程桌面报错处理
    由Handle转换为控件
    字符串格式化
    查询mssql的死锁语句
    读取和修改app.config文件
    json序列化与反序列化
  • 原文地址:https://www.cnblogs.com/lanliying/p/6169817.html
Copyright © 2011-2022 走看看