Java 从完整路径获取没有扩展名的文件名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16273810/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 06:08:19  来源:igfitidea点击:

Get filename without extension from full path

javafilenames

提问by dedmar

I am making a program to store data from excel files in database. I would like the user to give in console the full path of the file and after the program to take only the file name to continue.

我正在制作一个程序来将excel文件中的数据存储在数据库中。我希望用户在控制台中给出文件的完整路径,并在程序之后只取文件名继续。

The code for loading the full path is:

加载完整路径的代码是:

String strfullPath = "";
Scanner scanner = new Scanner(System.in);
System.out.println("Please enter the fullpath of the file");
strfullPath = scanner.nextLine();
String file = strfullPath.substring(strfullPath.lastIndexOf('/') + 1);
System.out.println(file.substring(0, file.indexOf('.')));

After that I would like to have: String filename = .......

在那之后,我想要: String filename = .......

The full path that the user would type would be like this: C:\\Users\\myfiles\\Documents\\test9.xls

用户输入的完整路径如下: C:\\Users\\myfiles\\Documents\\test9.xls

The filename that I would create would take only the name without the .xls! Could anyone help me how I would do this?

我将创建的文件名将只采用没有.xls! 谁能帮助我如何做到这一点?

How i would do it if i would like to take as filename "test9.xls" ? –

如果我想将文件名“test9.xls”作为文件名,我会怎么做?——

回答by Lukas Eichler

You can call the file.getName()method that returns the name of the file as String. Then you cut the extension.

您可以调用将file.getName()文件名作为字符串返回的方法。然后你切断了扩展。

String fileName = file.getName();
fileName = fileName.substring(0, fileName.lastIndexOf(".")+1);

回答by CloudyMarble

You can do it like this:

你可以这样做:

String fname = file.getName();
int pos = fname.lastIndexOf(".");
if (pos > 0) {
    fname = fname.substring(0, pos);
}

or you can use the apache.commons.io.FilenameUtils:

或者您可以使用apache.commons.io.FilenameUtils

String fileNameWithOutExt = FilenameUtils.removeExtension(fileNameWithExt);

回答by Duncan Jones

You could use the Fileclass to get the file name:

您可以使用File该类来获取文件名:

File userFile = new File(strfullPath);
String filename = userFile.getName();

Using a Fileobject has numerous benefits, including the ability to test the file exists:

使用File对象有很多好处,包括能够测试文件是否存在:

if (userFile.isFile()) {
  // Yay, it's a valid file (not a directory and not an invalid path)
}

You also need to check the file has an extension before you try and strip it:

在尝试删除文件之前,您还需要检查文件是否具有扩展名:

if (filename.indexOf(".") > 0) {
    filename = filename.substring(0, filename.lastIndexOf("."));
}

回答by Oibaf it

I usually use this solution described in other post:

我通常使用其他帖子中描述的这个解决方案:

import org.apache.commons.io.FilenameUtils;

String basename = FilenameUtils.getBaseName(fileName);

回答by Maheshbabu Jammula

if (!filename.equals(""))    
{
        String [] fileparts = filename.split("\.");
        String filename = fileparts[0]; //Get first part
}