Java-Nettoyer le chemin du fichier
Je veux nettoyer le chemin que j'utilise dans mon Application. Le chemin peut être modifié et parfois j'ai quelque chose comme ça:
C:/users/Username/Desktop/\..\..\..\Windows\Web\..\..\Program Files\..\Program Files\..\Python27\
Mais j'aimerais avoir quelque chose comme:
C:\Python27\
C'est un exemple!
Comment puis-je nettoyer le chemin pour obtenir uniquement la partie nécessaire?
Merci.
0
3 answers
Si fileName
est votre chaîne de nom de fichier, alors quelque chose comme:
String cleanedFilename = new File(fileName).getCanonicalPath();
Devrait le faire...
5
Author: Mathias Schwarz, 2012-04-18 12:48:59
Voici le code que je viens d'essayer.
new File("c:/temp/..").getCanonicalPath();
Il renvoie 'C:\', c'est vrai. Le parent de c:/temp
est en effet c:\
3
Author: AlexR, 2012-04-18 12:51:42
, Vous pouvez essayer d'utiliser le Fichier.Méthode getCanonicalPath ():
File file = new File("my/init/path");
String path = file.getCanonicalPath();
Je n'ai pas testé cependant, dites-nous en retour!
MODIFIER: @MathiasSchwarz a raison, utilisez getCanonicalPath() au lieu de getAbsolutePath() (link)
3
Author: sp00m, 2017-05-23 12:30:16