得到扩展名,路径和文件名 : 文件 « 文件输入输出 « Java

En
Java
1. 图形用户界面
2. 三维图形动画
3. 高级图形
4. 蚂蚁编译
5. Apache类库
6. 统计图
7. 
8. 集合数据结构
9. 数据类型
10. 数据库JDBC
11. 设计模式
12. 开发相关类
13. EJB3
14. 电子邮件
15. 事件
16. 文件输入输出
17. 游戏
18. 泛型
19. GWT
20. Hibernate
21. 本地化
22. J2EE平台
23. 基于J2ME
24. JDK-6
25. JNDI的LDAP
26. JPA
27. JSP技术
28. JSTL
29. 语言基础知识
30. 网络协议
31. PDF格式RTF格式
32. 映射
33. 常规表达式
34. 脚本
35. 安全
36. Servlets
37. Spring
38. Swing组件
39. 图形用户界面
40. SWT-JFace-Eclipse
41. 线程
42. 应用程序
43. Velocity
44. Web服务SOA
45. 可扩展标记语言
Java 教程
Java » 文件输入输出 » 文件屏幕截图 
得到扩展名,路径和文件名
得到扩展名,路径和文件名
 
/*
 * Copyright (c) 1995 - 2008 Sun Microsystems, Inc.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 *   - Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *
 *   - Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *
 *   - Neither the name of Sun Microsystems nor the names of its
 *     contributors may be used to endorse or promote products derived
 *     from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

public class FilenameDemo {
  public static void main(String[] args) {
    final String FPATH = "/home/mem/index.html";
    Filename myHomePage = new Filename(FPATH, '/''.');
    System.out.println("Extension = " + myHomePage.extension());
    System.out.println("Filename = " + myHomePage.filename());
    System.out.println("Path = " + myHomePage.path());
  }
}

/*
 * Copyright (c) 1995 - 2008 Sun Microsystems, Inc. All rights reserved.
 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *  - Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 *  - Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 *  - Neither the name of Sun Microsystems nor the names of its contributors may
 * be used to endorse or promote products derived from this software without
 * specific prior written permission.
 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

/**
 * This class assumes that the string used to initialize fullPath has a
 * directory path, filename, and extension. The methods won't work if it
 * doesn't.
 */
class Filename {
  private String fullPath;
  private char pathSeparator, extensionSeparator;

  public Filename(String str, char sep, char ext) {
    fullPath = str;
    pathSeparator = sep;
    extensionSeparator = ext;
  }

  public String extension() {
    int dot = fullPath.lastIndexOf(extensionSeparator);
    return fullPath.substring(dot + 1);
  }

  public String filename() { // gets filename without extension
    int dot = fullPath.lastIndexOf(extensionSeparator);
    int sep = fullPath.lastIndexOf(pathSeparator);
    return fullPath.substring(sep + 1, dot);
  }

  public String path() {
    int sep = fullPath.lastIndexOf(pathSeparator);
    return fullPath.substring(0, sep);
  }
}

   
  
Related examples in the same category
1. 创建文件
2. 创建一个临时文件
3. 创建一个临时文件并删除它退出
4. 创建一个目录(或几个目录)
5. 获取文件大小
6. 变更文件或目录上次修改的时间
7. 构建文件路径
8. 创建临时文件指定的扩展名后缀
9. 创建临时文件中指定的目录
10. 创建新的空文件
11. 比较两个文件路径
12. 删除文件
13. 删除文件或目录
14. 删除目录(空目录)
15. 删除文件或目录时,虚拟机终止
16. 确定文件或目录
17. 确定文件是否可以读取
18. 确定文件是否可以这样写:
19. 判断是否存在文件或目录
20. 确定文件或目录是隐藏
21. 证明文件
22. 移动文件或目录到另一个目录
23. 查找目录查找目录
24. 从java.io.File获得所有的路径信息
25. Getting an Absolute Filename Path from a Relative Filename Path
26. Getting an Absolute Filename Path from a Relative Filename with Path
27. Getting an Absolute Filename Path from a Relative Filename parent Path
28. 获得绝对文件的路径
29. 获取以字节为单位文件大小
30. 获取父目录的File对象
31. 取得文件最后修改日期
32. File.getCanonicalFile() converts a filename path to a unique canonical form suitable for comparisons.
33. Getting the Parents of a Filename Path
34. Get the parents of an absolute filename path
35. Getting and Setting the Modification Time of a File or Directory
36. 制作只读文件或目录
37. 文件根列表
38. 驱动器列表
39. 列出目录内容列出目录内容
40. 重命名文件或目录
41. 迫使更新一个文件到磁盘
42. 随机文件随机文件
43. 创建一个目录;所有祖先目录必须存在
44. Create a directory; all non-existent ancestor directories are automatically created
45. 获取当前的工作目录
46. 改变文件属性为可写
47. 数据文件数据文件
48. 输出到一个文本文件
49. 选择文件选择文件
50. 读取文本文件数据
51. 复制文件
52. 查询文件信息
53. Working with RandomAccessFileWorking with RandomAccessFile
54. 获取文件列表,并检查是否任何文件丢失
55. 从Java删除文件
56. Java中临时文件
57. 比较文件日期
58. 排序文件,基于他们的最后修改日期
59. 字符串-提取打印二进制文件字符串
60. 读取文件内容字符串使用输入输出工具
61. Get all xml files by file extension
62. 文件名组件
63. 获取文件类型图标
64. 改变文件属性为只读
65. 获取文件扩展名
66. 递归的搜索文件
67. 创建一个人类可读的文件大小
68. 设置文件属性
www.java2java.com | Contact Us
Copyright 2010 - 2030 Java Source and Support. All rights reserved.
All other trademarks are property of their respective owners.