Returns the pathname of the parent of this file. This is the path up to but not including the last name. null is returned if there is no parent. @return this file's parent pathname or null.
()
| 696 | * @return this file's parent pathname or {@code null}. |
| 697 | */ |
| 698 | public String getParent() { |
| 699 | int length = path.length(), firstInPath = 0; |
| 700 | if (separatorChar == '\\' && length > 2 && path.charAt(1) == ':') { |
| 701 | firstInPath = 2; |
| 702 | } |
| 703 | int index = path.lastIndexOf(separatorChar); |
| 704 | if (index == -1 && firstInPath > 0) { |
| 705 | index = 2; |
| 706 | } |
| 707 | if (index == -1 || path.charAt(length - 1) == separatorChar) { |
| 708 | return null; |
| 709 | } |
| 710 | if (path.indexOf(separatorChar) == index |
| 711 | && path.charAt(firstInPath) == separatorChar) { |
| 712 | return path.substring(0, index + 1); |
| 713 | } |
| 714 | return path.substring(0, index); |
| 715 | } |
| 716 | |
| 717 | /** |
| 718 | * Returns a new file made from the pathname of the parent of this file. |
no test coverage detected