A step back in a path hierarchy [duplicate] - java

This question already has answers here:
How to get just the parent directory name of a specific file
(10 answers)
Closed 7 years ago.
I have a path to a folder and want to change it. But first I want to get a step back.
String path = "C:\Users\Jurgen\Java\Project\Folder\inner_folder\";
How do I get a step back in a path hierarchy?
For example:
String path = "C:\Users\Jurgen\Java\Project\Folder\";

Extract a substring up until the last slash
String newPath = path.substring(0, path.lastIndexOf('\'));
Edit: (because I'm being challenged on this answer)
Some people will tell you that treating paths as strings is wrong, in this case it doesn't make a difference. The other option of creating a Path object then using it's .getParent() method or prior to Java 7, a File object.

Convert it to a Path with Paths.get(path); and use its getParent() method.

Related

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'.

Where is the default text file reader for java [duplicate]

This question already has answers here:
How does Java resolve a relative path in new File()?
(7 answers)
Closed 7 years ago.
I was given a piece of code that has this line:
GetBytes getInput = new GetBytes("myText.txt");
Which obviously reads a text file and tries to get its input.
I am using jdk1.8.0_20 on Windows 8 running inside eclipse.
In which folder should I put a file named myText.txt?
First:
GetBytes getInput = new GetBytes("myText.txt");
You forgot the last parenthesis there.
Second:
It goes in the same folder as your class is in.
GetBytes is a class that was given to you, I presume? The code in that class should specify which folder it looks in. Usually, the default is the same folder as the class. Since GetBytes is a custom class, it could be anywhere, though. If you can't figure it out, post the code for the GetBytes class.

check if user-entered filename/path is valid [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Is there a way in Java to determine if a path is valid without attempting to create a file?
I'm trying to let the user enter the path where he wants something to be saved.
so basically i open an editor, and let him enter the path...
but how can i check if the entered string is a valid path?
if the user forgets to type in a "/" at the end - its not a problem, i can manually check for that...
but i cant manually check for everything:
a space at the end (/folder /)
question marks.
greater than - less than symbols (/folder:->/
(back)slashes \folder\
and all that stuff
is there a convenient way in java to check for that?
Generic Java file is a directory or exists, from this answer:
File file = new File("c:\\cygwin\\cygwin.bat");
if (!file.isDirectory())
file = file.getParentFile();
if (file.exists()) {
...
}
However, Android (question tagged as android), I'll have to look into...

java calculate pathname difference [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.
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.

how can I create a temporary folder in java 6? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Create a temporary directory in Java
Duplicate: stackoverflow.com/questions/375910
Is there a way of creating a temporary folder in java ? I know of File's static method createTempFile, but this will only give me a temporary file.
I've never seen a good solution for this, but this is how I've done it.
File temp = File.createTempFile("folder-name","");
temp.delete();
temp.mkdir();
Any reason you can't use the directory defined by the java.io.tmpdir property?
ie
String dirName = System.getProperty("java.io.tmpdir");
I would check out this past question in SO for a solution. Or this one!
I write my own utility classes for creating temporary directories and for disposing them when they are not anymore needed. For example like this.

Categories

Resources