java calculate pathname difference [duplicate] - java

This question already has answers here:
How to construct a relative path in Java from two absolute paths (or URLs)?
(23 answers)
Closed 6 years ago.
Is there an open source library that, given ...
/a/b/c
/a/b/c/d/e
would return ../..
or for that matter given
/a/b/c/d
/a/b/c/e
would return ../d
?

If you don't mind passing by converting your Strings into URI then this latter one has the method relativize which should do exactly what you want, take a look here.

Related

Java: How to check if a byte[] array exists in a file? [duplicate]

This question already has answers here:
Searching for a sequence of Bytes in a Binary File with Java
(5 answers)
Closed 5 years ago.
I come up an idea to read a byte[] array with the same size of the input, and check one by one. But it seems not very efficient. Is there a way to solve it by using rolling hash?
If you are using java 8 or above please check the
java.util.Optional<T>
The documentation is here
Optional
If I got what you mean correctly

Best way to get a sub-path of a file [duplicate]

This question already has answers here:
How to construct a relative path in Java from two absolute paths (or URLs)?
(23 answers)
Closed 6 years ago.
I have two File objects:
C:/basepath/
C:/basepath/directory/file.txt
Now I would like to subtract file 1 from file 2 so that I get directory/file.txt.
I don't want to use String.substring() since file paths may differ from input.
Use the features of java.nio.file.Path. You are looking to 'relativize'.

Multiple Http Servlets string path mapping [duplicate]

This question already has answers here:
Servlet and path parameters like /xyz/{value}/test, how to map in web.xml?
(7 answers)
Closed 6 years ago.
I'm want to map these two different servlets (using addServletWithMapping):
"/soccerapp/teams/*"
"/soccerapp/teams/*/players
but the second is obviously never reached, is any other special char that I can use in the string path to solve the problem?
The wildcard in the middle of the path is not evaluated. You can see this answer https://stackoverflow.com/a/15386616/1630604 for a complete description how the path are evaluated.
Anyway, you cannot map two Servlet to one URL.

List from encoded string [duplicate]

This question already has answers here:
How can I check if a single character appears in a string?
(16 answers)
Closed 9 years ago.
I have a string like :
"\"[\"a\",\"b\", \"c\"]\""
How to convert this to a list of strings
Could you suggest me a nice way of doing it in Java?
str.contains("c") does this job. However did not you think to consult String class documentation first?
Use contains():
if (str.contains("\"c\""))

How to get the name of the containing function [duplicate]

This question already has answers here:
Getting the name of the currently executing method
(23 answers)
Closed 8 years ago.
In C++, you can use __FUNCTION_NAME__ to get the name of the function that contains __FUNCTION_NAME__.
Is there an equivalent in Java? It could, in Java, be possible to do something with this and reflection. Is there something simpler though?
Thread.currentThread().getStackTrace()
will usually contain the method you’re calling it from but there are pitfalls
http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/StackTraceElement.html
Thread.currentThread().getStackTrace()[ste.length - 1 - depth].getMethodName();
depth = 0 (zero) will give current method
also
System.out.println((new Throwable()).getStackTrace()[0].toString());
Sample output:
com.junk.Junk3.main(Junk3.java:12)

Categories

Resources