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...
Related
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.
This question already has answers here:
Java : parse java source code, extract methods
(2 answers)
Closed 7 years ago.
I am working on a small program to compare two Java files. My goal is to compare the two files so that I can see what functions were added and deleted from one file to another (like a simple version control program). I am running into issues on how I should be handling these files. My current approach is to use a Scanner and use:
while(scanner.hasNext()) {
String function = scanner.next("((public|private|protected|static|final|native|synchronized|abstract|threadsafe|transient)+\\s)+[\\$_\\w\\<\\>\\[\\]]*\\s+[\\$_\\w]+\\([^\\)]*\\)?\\s*\\{?[^\\}]*\\}?");
System.out.println(function);
}
However this is not getting me any results for a file that I know has functions in it. Any tips or ideas on how to approach this?
You could use ANTLR Java grammar https://github.com/antlr/grammars-v4/blob/master/java8/Java8.g4 to get a full-blown Java parser and then use it to extract any information you need about Java files.
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.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Renaming a file using Java
renameFile: This method should take as input a String from and a String to. It should return a boolean. The method should find the index in drive of a file with name from. If no such index exists, the method should return false. It should also check that the file to does not exist. If the file to does exist, then the method should return false. If the file from exists and to does not, then the method should change the name of the file from from to be to. In this case, the method should return true
can someone show me how to do this?
Starting here you should be able to figure it out.
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.