Writing code in comment? Please use ide. Load Comments. What's New. Most popular in Java. More related articles in Java. Comparator Interface in Java with Examples System. Method Syntax public String getParent. This method returns a String which denotes the pathname string of the parent directory named by this abstract pathname, or null if this pathname does not name a parent. Below is a java code demonstrates the use of getParent method of File class.
The example presented might be simple however it shows the behaviour of the getParent method of File class. This example code just prints out the result of the getParent method. I understand why because at the top node there is no parent. How do I prevent my code from generating an error and gracefully displays a message if a user does navigate to the top level node. It is worth noting that this piece of code can still break your page if you enter "null" for your "from" or "to" argument since you are throwing a new IllegalArgumentException in that case.
I prefer to handle as much of the logic and error handling in the actual java backend and as little as possible on the jsp frontend. A possible improvement could be to have return just an empty list if there are invalid params. With this improvement, you shouldn't get any execeptions anymore. In the frontend you can now just check if your list is empty or not and act accordingly.
You need to employ a try Read up the documentation for try Add an if statement inside getParent method definition with condition that if the Node your getParent-ing is the top node, throw new exception. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow.
Learn more. No, they don't. If you'd care to point out why you think they do, someone can probably identify the mistake in your reasoning. Also, if you specify exactly what you'd like to see for output given some particular input, we can help you out there, too. Your question title seems strange, too, since your problem seems to be that it is returning the full path to a file. Edit: I think I understand the source of your confusion. A File represents a file system path in a platform-agnostic way.
It can be a path to a file or to a directory. It also always represents the same path, though not necessarily the same absolute path. This is a very fine distinction but a very important one. A File object representing a relative path is always relative. Given a File representing a relative path, you can get the current corresponding absolute path using getAbsolutePath. This doesn't, however, alter the fact that the File represents a relative path.
Further invocations of getAbsolutePath on the same File object may return different values. Consider, for example:. It should be clear now that the path a File represents is only that: a path. Further, a path can be composed of zero or more parts, and calling getParent on any File gives back the path of that File with the last path element removed unless there isn't a "last path element" to remove.
Thus the expected result of new File "foo". From the example and explanation above, you should be able to see that the way to get the containing directory when you've created relative-path File object is with. Additional note: Since File is Serializable, you could write a relative-path file to disk or send it across a network. That File, when deserialized in another JVM, will still represent a relative path and will be resolved against whatever the current working directory of that JVM happens to be.
0コメント