0

I am trying to trim my directory which is in string format in java . I only want images/filename and want to remove directory before images.

Joel
  • 5,732
  • 4
  • 37
  • 65
  • 5
    What have you tried? Please provide [minimal, reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). – Andriy Budzinskyy Jan 17 '20 at 12:19
  • 1
    Welcome to stackoverflow. Please read [Asking Questions - Help Center](https://stackoverflow.com/help/asking) – Joel Jan 17 '20 at 12:24

2 Answers2

1

Split

This answer covers both forwardslashed directories and backslash directories.

Sample Input:

C:/hello/myAwesomeImage.png
C:\hello\myAwesomeImage.png

Forwardslashes

String [] myFile = "C:/hello/myAwesomeImage.png".split("/"); // split it
String myFileName = myFile[myFile.length - 1]; // Extract filename

Backslashes

String [] myFile = "C:\hello\myAwesomeImage.png".split("\\"); // split it
String myFileName = myFile[myFile.length - 1]; // Extract filename

Output: "myAwesomeImage.png"

Alternatively, if you don't want to split...

RegEx

Backslash + No Extension

String myFile = "C:\\hello\\myAwesomeImage.png".matches("[ \\w-]+?(?=\\.)");

Output: "myAwesomeImage"

Backslash + Extension

 String myFile = "C:\\hello\\myAwesomeImage.png".matches("[ \w-]+\....");

Output: "myAwesomeImage.png"

Forwardslash + Extension

String myFile = "C:/hello/myAwesomeImage.png".matches("[^\/]+(?=\.*$)");

Output: "myAwesomeImage.png"

Forwardslash + No Extension

String myFile = "C:/hello/myAwesomeImage.png".matches("[^\/]+(?=\..*$)");

Output: "myAwesomeImage"

Joel
  • 5,732
  • 4
  • 37
  • 65
0

Java has a powerful built-in File API. java.io.File is particularly useful.

I would recommend using this rather than string manipulation, Advantages include portability across operating systems which may use / or .

File myFile = new File("C:/hello/myAwesomeImage.png");
System.out.println(myFile.getName());
System.out.println(myFile.getParentFile().getName());
System.out.println(myFile.getParentFile().getParent());

Output

myAwesomeImage.png
hello
C:
Adam
  • 35,919
  • 9
  • 100
  • 137
  • I'd argue that opening a filestream and reading a files' content is less efficient than string-manipulation. Especially if you have tons of files that you need to process. I'd rather keep them as strings in that case. – Joel Jan 17 '20 at 13:11
  • The above code never actually "touches" the disk. I'd go for less error prone approach every time. – Adam Jan 17 '20 at 13:41
  • 1
    Error prone in what way? Like, the fact that a disk might get disconnected and the program crashes or that the file is unaccessable and the program crashes or that there's no readpermission on the file (i can go on). – Joel Jan 17 '20 at 13:51
  • Error prone in terms of implementation: "[^\/]+(?=\.*$)" vs getName(). Please note the above code using File API does not access disk unless you call certain functions. I use Linux not windows, and the above new File("C:/hello/myAwesomeImage.png") works just fine. – Adam Jan 17 '20 at 14:24
  • Yes it works, but to call it "less error prone" is a strange one, since there's a lot more things that can go wrong runtime-wise than with basic stringmanipulation that can be statically evaluated. – Joel Jan 17 '20 at 14:27