区分大小写的文件扩展名和存在检查 [英] Case sensitive file extension and existence checking

查看:328
本文介绍了区分大小写的文件扩展名和存在检查的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要检查文件是否存在。这可以通过 File#exists()方法完成。但是这种存在检查是区分大小写的。我的意思是,如果我在代码中有一个文件名some_image_file.jpg,但是实际上如果文件是some_image_file.JPG,则此方法表示该文件不存在。

I need to check whether or not a file exists. Which can be accomplished by File#exists() method. But this existence checking is case sensitive. I mean if I have a file name some_image_file.jpg in code but if physically the file is some_image_file.JPG then this method says that the file doesn't exists. How can I check the file existence with case insensitivity to the extension and get the actual file name?

在我的情况下,我有一个excel文件,如何检查扩展名不区分大小写的文件是否存在并获得实际的文件名?每行包含文件和文件名的元数据。在某些情况下,我只有文件名,而在其他情况下,我可以拥有完整路径。我将一行表示为文档。

In my scenario, I have a excel file. Each row contains metadata for files and the filename. In some cases I have only the filename or other cases I can have full path. I am denoting a row as a document.

这些文件放在服务器中。我的工作是

These files are placed in the server. My job is to


  • 逐行读取excel文件并列出所有文档。

  • 取出文件名或文件路径。

  • 创建文件的完整路径。

  • 检查文件是否存在。

  • 验证文档中提供的其他元数据/信息。

  • 上传文件。

  • Read the excel file row by row and list all the documents.
  • Take out the filename or filepath.
  • Create the full path of the file.
  • Check if the file exists or not.
  • Validate other metadata/information provided in the document.
  • Upload the file.

如果文件不存在或某些元数据无效,我的应用程序将引发异常。

My application throws exception in case the file doesn't exists or if some metadata are invalid.

excel文件由客户,他们写了一些错误的文件名,我的意思是,如果文件实际具有小写扩展名,则以大写形式写扩展名,反之亦然。

The excel file is written by the customer and they wrote some file name wrong, I mean if the file physically have the extension in lower case, they have written the extension in upper case, also the converse is true.

我正在Unix服务器上运行应用程序。

I am running the application in unix server.

由于文件扩展名不匹配,因此 File#exists()给出错误信息,最终我的代码引发异常。

As the file extensions are not matching so the File#exists() is giving false and eventually my code is throwing exception.

放置文件的文件夹可以包含30000个或更多文件。

The folders where the files are placed can have 30000 or more files.

我想要的是


  • 获取文件的完整路径。

  • 检查文件是否存在。

  • 如果文件不存在,则

  • 检查文件是否存在b y转换扩展名的大小写。

  • 如果大小写转换后不存在扩展名,则抛出异常。

  • 如果存在,则返回实际文件名或文件路径。

  • To take the full path of the file.
  • Check if the file exists or not.
  • If it does not exists then
  • Check the file existence by converting the case of the extension.
  • If it doesn't exist after the case conversion then throw exception.
  • If it exists, then return the actual file name or file path.

如果文件名具有文件扩展名,例如 .jpg ,不知道该怎么办!我应该通过更改大小写来检查它吗?

If the file name has file extension something like .Jpg, don't know what to do! Should I check it by permuting it by changing the case?

推荐答案

这样我已经解决了问题:

This way I had solved the problem:

public String getActualFilePath() {
    File givenFile = new File(filePath);
    File directory = givenFile.getParentFile();

    if(directory == null || !directory.isDirectory()) {
        return filePath;
    }


    File[] files = directory.listFiles();
    Map<String, String> fileMap = new HashMap<String, String>();

    for(File file : files) {                        
        if(file.isDirectory()){
            continue;
        }

        String absolutePath = file.getAbsolutePath();
        fileMap.put(absolutePath, StringUtils.upperCase(absolutePath));
    }

    int noOfOcc = 0;
    String actualFilePath = "";

    for(Entry<String, String> entry : fileMap.entrySet()) {
        if(filePath.toUpperCase().equals(entry.getValue())) {
            actualFilePath = entry.getKey();
            noOfOcc++;
        }
    }

    if(noOfOcc == 1) {
        return actualFilePath;
    }

    return filePath;
}

此处 filePath 是文件的完整路径。

Here filePath is the full path to the file.

这篇关于区分大小写的文件扩展名和存在检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆