Reputation: 649
I am trying to create a new directory using Java but I realized that the mkdir() don't work with strings that are made up of concat() method or using the '+' operand.
For example:
String keyword = "golden+retriever";
String folderName = removeChar(keyword);
String strDirectory = "C:/Users/Administrator/Desktop/"+folderName;
File newFolder = new File(strDirectory);
newFolder.mkdir();
The above code does not create the folder but it will work correctly if I were to use the directory without the '+' operand like this:
String strDirectory = "C:/Users/Administrator/Desktop/goldenretriever";
File newFolder = new File(strDirectory);
newFolder.mkdir();
Why is it so? Is there any ways to successfully create a directory using the '+' operand or the concat() method?
Update: The '+' in the String is not a typo. The removeChar() method simply removes the '+' in order to create a folder without special characters.
Below is the code for removeChar():
public static String removeChar(String s)
{
StringBuffer buff = new StringBuffer(s.length());
buff.setLength(s.length());
int current = 0;
for (int i=0; i<s.length(); i++)
{
char cur = s.charAt(i);
if(cur != '+')
{
buff.setCharAt(current++, cur);
}
}
return buff.toString();
}
Upvotes: 0
Views: 89
Reputation: 47593
Although the original poster has gone with another more efficient solution to solve the problem, I'd like to point out why their original code was failing.
They created a new StringBuffer
and made a copy of s
at the top. They set the initial length to be equal to the length of the original string s
. If they removed any number of pluses the new string is actually smaller than the original. In order to make sure the StringBuffer is of the correct length with no unused trailing characters you would have to set the length accordingly when finished.
To fix the original code would require changing:
return buff.toString();
To:
buff.setLength(current);
return buff.toString();
One could have reworked the code so the new StringBuffer buff
started out blank and new characters are simply appended as needed. That could have been done with something like:
public static String removeChar(String s)
{
StringBuffer buff = new StringBuffer();
for (int i=0; i<s.length(); i++)
{
char cur = s.charAt(i);
if(cur != '+')
buff.append(cur);
}
return buff.toString();
}
Upvotes: 0
Reputation: 1668
What's wrong with your code is that the removeChar
returns excess whitespace after the keyword. What you can do is trim it first before creating the file.
Try:
File newFolder = new File(strDirectory.trim());
And also I recommend you check if the folder exists first before creating it
if(!newFolder.exists()) {
newFolder.mkdir();
}
Upvotes: 0
Reputation: 31
Please make sure that your path is proper otherwise windows allowed + as name of folder. see for example if I have URL like : C:/TestWS/Test/TestingDir/ then your keyword
String keyword = "golden+retriever";
Java doesn't create all directories automatically so please make sure that this path already exist before creating final keyword dir : C:/TestWS/Test/TestingDir/
If TestingDir is not exist and trying to create "golden+retriever"; then java will not be going to create any directory.
Upvotes: 0