How to get path length in java?

How do you get the path length? Am I missing something? Oddly enough, int length () or some similar method is implemented in the Path class. Example: for a path

C:\foo\bar\anotherfolder\subfolder

method will return 4.

+4
source share
2 answers

Path.getNameCount () :

Returns the number of name elements in the path.

+16
source

you can use the total number of backslashes in a string that will give you the same answer. Like this

StringTokenizer stOR = new StringTokenizer ("C: \ foo \ bar \ otherfolder \ subfolder", "\");

Windows, . escape- (:\b\t\n\f\r\"\ '\)

int orCount = stOR.countTokens() - 1;

System.out.println(orCount);

+1

Source: https://habr.com/ru/post/1619531/


All Articles