3

Java中从文件路径字符串中提取文件扩展名3种方法

 7 months ago
source link: https://www.jdon.com/72318.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Java中从文件路径字符串中提取文件扩展名3种方法

Java 中,使用文件很常见,了解如何从文件路径中提取文件扩展名对于根据文件类型做出明智的决策至关重要。

在本文中,我们将探索有效完成此任务的技术,使开发人员能够改进其与文件相关的操作。

1、Java中从文件路径字符串中提取文件扩展名的程序
下面是从文件路径字符串中提取文件扩展名的三种方法及其实现。

import java.io.File; 

public class Code { 
    public static void main(String[] args) 
    { 
        String filepath = "example.txt"; 
        // 打印给定文件路径的文件名和扩展名;
        printFileNameAndExtension(filepath); 

filepath = "/path/to/your/file/example.pdf"; 
        printFileNameAndExtension(filepath); 

filepath = "no_extension_file"; 
        printFileNameAndExtension(filepath); 
    } 

// 用于打印给定文件路径的文件名和扩展名的函数;
    public static void printFileNameAndExtension(String filepath) 
    { 
        // Print filename and extension for the given 
        // filepath 
        System.out.println("Filename and Extension: "
                        + filepath); 
        // Get and print the extension for the given 
        // filepath 
        String extension = getFileExtension(filepath); 
        System.out.println("Extension: " + extension); 
    } 

// 从文件路径获取扩展名的函数 ;
    public static String getFileExtension(String filePath) 
    { 
        int lastIndexOfDot = filePath.lastIndexOf('.'); 
        if (lastIndexOfDot == -1) { 
            return "No extension"; 
        } 

return filePath.substring(lastIndexOfDot + 1); 
    } 
}

方法概述:

  • getFileExtension方法将文件路径作为输入。
  • 它利用lastIndexOf方法来查找文件路径中最后一次出现的点('.') 字符。
  • 如果没有找到点(lastIndexOfDot == -1),则表示没有文件扩展名,返回“ No extension ”。
  • 否则,它将从最后一个点之后的索引中提取到文件路径末尾的子字符串,从而有效地给出文件扩展名。
  • 提供示例文件路径来展示实际方法。
  • 打印结果,展示该方法如何准确提取文件扩展名。
  • 处理不存在扩展的情况,返回适当的消息

2、使用Java的File类提取文件扩展名

import java.io.File; 

// Driver Class 
public class Code2 { 
    // Main Function 
    public static void main(String[] args) { 
        String filepath = "example.txt"; 

// It will print file name and extension 
        printFileNameAndExtension(filepath); 

filepath = "/path/to/your/file/example.pdf"; 

//它将打印给定文件路径的文件名和扩展名 ;
        printFileNameAndExtension(filepath); 

filepath = "no_extension_file"; 
        printFileNameAndExtension(filepath); 
    } 

// 打印文件名和扩展名的功能 ;
    public static void printFileNameAndExtension(String filepath) 
    { 
        System.out.println("Filename and Extension: " + filepath); 

String ans = getFileExtension(filepath);     
        System.out.println("Extension: " + ans); 
    } 

// 函数从路径 .中获取扩展名;
    public static String getFileExtension(String filePath) { 
        String fileName = new File(filePath).getName(); 
        int dotIndex = fileName.lastIndexOf('.'); 
        return (dotIndex == -1) ? "No extension" : fileName.substring(dotIndex + 1); 
    } 

方法概述:

  • 利用 File 类从给定文件路径获取文件名。
  • 使用lastIndexOf方法查找文件名中最后一次出现的点('.') 字符。
  • 如果没有找到点(dotIndex == -1),则返回“ No extension ”。
  • 否则,它从文件名最后一个点之后的索引中提取子字符串,从而有效地给出文件扩展名。
用法:
  • 演示了该方法在提取文件扩展名方面的有效性。
  • 处理不存在分机的情况,提供清晰且适当的消息。

3.使用Java NIO的Path类提取文件扩展名

import java.nio.file.Path; 
import java.nio.file.Paths; 

public class Code3 { 
    public static void main(String[] args) 
    { 
        String filepath = "example.txt"; 
        // Print filename and extension for the given filepath 
        printFileNameAndExtension(filepath); 

filepath = "/path/to/your/file/example.pdf"; 
        printFileNameAndExtension(filepath); 

filepath = "no_extension_file"; 
        printFileNameAndExtension(filepath); 
    } 

// Function to print filename and extension for the given filepath 
    public static void printFileNameAndExtension(String filepath) { 
        // 将文件路径转换为路径  ;
        Path path = Paths.get(filepath); 
        // 从路径中获取文件名 ;
        String fileName = path.getFileName().toString(); 
        // Print filename 
        System.out.println("Filename and Extension: " + fileName); 
        // 获取并打印文件名的扩展名 ;
        String extension = getFileExtension(fileName); 
        System.out.println("Extension: " + extension); 
    } 

// 从文件名获取扩展名的函数 ;
    public static String getFileExtension(String fileName) { 
        // 查找文件名中最后出现的".";
        int dotIndex = fileName.lastIndexOf('.'); 
        // 如果未找到'.',则返回 "无扩展名",否则返回'.'后的子串;
        return (dotIndex == -1) ? "No extension" : fileName.substring(dotIndex + 1); 
    } 

方法概述:

  • Paths.get(String) 方法:创建一个 Path 对象,表示位于指定路径的文件或目录。
  • Path.getFileName() 方法:检索此 Path 表示的文件或目录名称。
  • String.lastIndexOf(char) 方法:返回指定字符最后一次出现的索引,如果未找到该字符,则返回 1。
  • String.substring(int)方法:返回一个新字符串,该字符串是该字符串的子字符串,从指定索引开始。
用法:
  • 创建 Path 对象:使用 Paths.get(String) 创建表示文件或目录的 Path 对象。
  • 提取文件名:在 Path 对象上调用 getFileName() 以获取字符串形式的文件或目录名。
  • 查找扩展名:使用lastIndexOf('.') 查找文件名中最后一次出现的点(.),表示文件扩展名的开始。
  • 提取扩展名:应用 substring(dotIndex + 1) 获取从点后索引开始的子字符串,代表文件扩展名。
  • 处理无扩展名:检查lastIndexOf('.')是否返回1;如果为true,则没有扩展,并进行相应处理(例如,设置默认消息或进程)。

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK