Risposte:
Se tu, come me, preferisci utilizzare un codice di libreria in cui probabilmente hanno pensato a tutti i casi speciali, come ad esempio cosa succede se passi null o punti nel percorso ma non nel nome del file, puoi usare quanto segue:
import org.apache.commons.io.FilenameUtils;
String fileNameWithOutExt = FilenameUtils.removeExtension(fileNameWithExt);
java.nio.file.Filese Path- come la risoluzione di directory di base, la copia / spostamento di una riga di file, ottenendo solo il nome del file ecc.
Il modo più semplice è usare un'espressione regolare.
fileNameWithOutExt = "test.xml".replaceFirst("[.][^.]+$", "");
L'espressione sopra rimuoverà l'ultimo punto seguito da uno o più caratteri. Ecco un test unitario di base.
public void testRegex() {
assertEquals("test", "test.xml".replaceFirst("[.][^.]+$", ""));
assertEquals("test.2", "test.2.xml".replaceFirst("[.][^.]+$", ""));
}
org.apache.commons. Per quanto ne so, questo è l'unico modo per farlo su Android.
Vedi il seguente programma di test:
public class javatemp {
static String stripExtension (String str) {
// Handle null case specially.
if (str == null) return null;
// Get position of last '.'.
int pos = str.lastIndexOf(".");
// If there wasn't any '.' just return the string as is.
if (pos == -1) return str;
// Otherwise return the string, up to the dot.
return str.substring(0, pos);
}
public static void main(String[] args) {
System.out.println ("test.xml -> " + stripExtension ("test.xml"));
System.out.println ("test.2.xml -> " + stripExtension ("test.2.xml"));
System.out.println ("test -> " + stripExtension ("test"));
System.out.println ("test. -> " + stripExtension ("test."));
}
}
che produce:
test.xml -> test
test.2.xml -> test.2
test -> test
test. -> test
foo.tar.gz? Vedo perché .tar.gzsarebbe quello che vorresti.
foo.tar.gzè una versione gzip di foo.tarcosì si potrebbe anche sostenere che gzera l'estensione. Tutto dipende da come si definisce l'estensione.
.gitignore?
Ecco l'ordine delle liste consolidato secondo le mie preferenze.
Usando i comuni di Apache
import org.apache.commons.io.FilenameUtils;
String fileNameWithoutExt = FilenameUtils.getBaseName(fileName);
OR
String fileNameWithOutExt = FilenameUtils.removeExtension(fileName);
Utilizzo di Google Guava (se già in uso)
import com.google.common.io.Files;
String fileNameWithOutExt = Files.getNameWithoutExtension(fileName);
O usando Core Java
1)
String fileName = file.getName();
int pos = fileName.lastIndexOf(".");
if (pos > 0 && pos < (fileName.length() - 1)) { // If '.' is not the first or last character.
fileName = fileName.substring(0, pos);
}
2)
if (fileName.indexOf(".") > 0) {
return fileName.substring(0, fileName.lastIndexOf("."));
} else {
return fileName;
}
3)
private static final Pattern ext = Pattern.compile("(?<=.)\\.[^.]+$");
public static String getFileNameWithoutExtension(File file) {
return ext.matcher(file.getName()).replaceAll("");
}
API Liferay
import com.liferay.portal.kernel.util.FileUtil;
String fileName = FileUtil.stripExtension(file.getName());
Se il tuo progetto utilizza Guava (14.0 o versioni successive), puoi andare con Files.getNameWithoutExtension().
(Essenzialmente uguale a quello FilenameUtils.removeExtension()di Apache Commons IO, come suggerisce la risposta più votata . Volevo solo sottolineare che Guava fa questo. Personalmente non volevo aggiungere dipendenza a Commons — che ritengo sia un po 'una reliquia— solo per questo.)
FilenameUtils.getBaseName()
Di seguito è riportato il riferimento da https://android.googlesource.com/platform/tools/tradefederation/+/master/src/com/android/tradefed/util/FileUtil.java
/**
* Gets the base name, without extension, of given file name.
* <p/>
* e.g. getBaseName("file.txt") will return "file"
*
* @param fileName
* @return the base name
*/
public static String getBaseName(String fileName) {
int index = fileName.lastIndexOf('.');
if (index == -1) {
return fileName;
} else {
return fileName.substring(0, index);
}
}
Se non ti piace importare l'intero apache.commons, ho estratto la stessa funzionalità:
public class StringUtils {
public static String getBaseName(String filename) {
return removeExtension(getName(filename));
}
public static int indexOfLastSeparator(String filename) {
if(filename == null) {
return -1;
} else {
int lastUnixPos = filename.lastIndexOf(47);
int lastWindowsPos = filename.lastIndexOf(92);
return Math.max(lastUnixPos, lastWindowsPos);
}
}
public static String getName(String filename) {
if(filename == null) {
return null;
} else {
int index = indexOfLastSeparator(filename);
return filename.substring(index + 1);
}
}
public static String removeExtension(String filename) {
if(filename == null) {
return null;
} else {
int index = indexOfExtension(filename);
return index == -1?filename:filename.substring(0, index);
}
}
public static int indexOfExtension(String filename) {
if(filename == null) {
return -1;
} else {
int extensionPos = filename.lastIndexOf(46);
int lastSeparator = indexOfLastSeparator(filename);
return lastSeparator > extensionPos?-1:extensionPos;
}
}
}
Mentre sono un grande sostenitore del riutilizzo delle librerie, il JAR org.apache.commons.io è di 174 KB, che è notevolmente grande per un'app mobile.
Se scarichi il codice sorgente e dai un'occhiata alla loro classe FilenameUtils, puoi vedere che ci sono molte utilità extra e fa fronte ai percorsi Windows e Unix, il che è delizioso.
Tuttavia, se si desidera solo un paio di metodi di utilità statici da utilizzare con percorsi in stile Unix (con un separatore "/"), è possibile che il codice riportato di seguito sia utile.
Il removeExtensionmetodo conserva il resto del percorso insieme al nome file. C'è anche un simile getExtension.
/**
* Remove the file extension from a filename, that may include a path.
*
* e.g. /path/to/myfile.jpg -> /path/to/myfile
*/
public static String removeExtension(String filename) {
if (filename == null) {
return null;
}
int index = indexOfExtension(filename);
if (index == -1) {
return filename;
} else {
return filename.substring(0, index);
}
}
/**
* Return the file extension from a filename, including the "."
*
* e.g. /path/to/myfile.jpg -> .jpg
*/
public static String getExtension(String filename) {
if (filename == null) {
return null;
}
int index = indexOfExtension(filename);
if (index == -1) {
return filename;
} else {
return filename.substring(index);
}
}
private static final char EXTENSION_SEPARATOR = '.';
private static final char DIRECTORY_SEPARATOR = '/';
public static int indexOfExtension(String filename) {
if (filename == null) {
return -1;
}
// Check that no directory separator appears after the
// EXTENSION_SEPARATOR
int extensionPos = filename.lastIndexOf(EXTENSION_SEPARATOR);
int lastDirSeparator = filename.lastIndexOf(DIRECTORY_SEPARATOR);
if (lastDirSeparator > extensionPos) {
LogIt.w(FileSystemUtil.class, "A directory separator appears after the file extension, assuming there is no file extension");
return -1;
}
return extensionPos;
}
public static String getFileExtension(String fileName) {
if (TextUtils.isEmpty(fileName) || !fileName.contains(".") || fileName.endsWith(".")) return null;
return fileName.substring(fileName.lastIndexOf(".") + 1);
}
public static String getBaseFileName(String fileName) {
if (TextUtils.isEmpty(fileName) || !fileName.contains(".") || fileName.endsWith(".")) return null;
return fileName.substring(0,fileName.lastIndexOf("."));
}
Si sta utilizzando il modo più semplice per ottenere il nome dal percorso relativo o dal percorso completo
import org.apache.commons.io.FilenameUtils;
FilenameUtils.getBaseName(definitionFilePath)
Puoi dividerlo per "." e sull'indice 0 è il nome del file e su 1 l'estensione, ma propenderei per la migliore soluzione con FileNameUtils di apache.commons-io come è stato menzionato nel primo articolo. Non deve essere rimosso, ma è sufficiente:
String fileName = FilenameUtils.getBaseName("test.xml");
Utilizzare FilenameUtils.removeExtensionda Apache Commons IO
Esempio:
È possibile fornire il nome completo del percorso o solo il nome del file .
String myString1 = FilenameUtils.removeExtension("helloworld.exe"); // returns "helloworld"
String myString2 = FilenameUtils.removeExtension("/home/abc/yey.xls"); // returns "yey"
Spero che questo ti aiuti ..
Mantenendolo semplice, utilizzare il metodo String.replaceAll () di Java come segue:
String fileNameWithExt = "test.xml";
String fileNameWithoutExt
= fileNameWithExt.replaceAll( "^.*?(([^/\\\\\\.]+))\\.[^\\.]+$", "$1" );
Questo funziona anche quando fileNameWithExt include il percorso completo.
È possibile utilizzare la funzione di divisione java per dividere il nome file dall'estensione, se si è sicuri che nel file sia presente un solo punto che per estensione.
File filename = new File('test.txt');
File.getName().split("[.]");
così split[0]restituirà "test" e dividere [1] restituirà "txt"
Prova il codice qui sotto. Utilizzo delle funzioni di base di Java. Si prende cura di Strings con estensione e senza estensione (senza il '.'carattere). '.'Viene anche trattato il caso del multiplo .
String str = "filename.xml";
if (!str.contains("."))
System.out.println("File Name=" + str);
else {
str = str.substring(0, str.lastIndexOf("."));
// Because extension is always after the last '.'
System.out.println("File Name=" + str);
}
Puoi adattarlo per lavorare con le nullstringhe.
.nome file o un file è un backup e ha un nome simile document.docx.backup, ecc.). È molto più affidabile utilizzare una libreria esterna che si occupa di tutte queste situazioni eccezionali per te.