Update file path

Posted on 2011-03-01
Last Modified: 2012-08-13


I want to know if its possible to use dos commands in strings when specifying a file path.

The user to specifies an input directory.

I want to be able to switch the directory string in the code.

String inputdir = "data/results/visualisation";
String inputdir2 = "data/results/report";

I know that the report folder will always live in the same level as the inputdir visualisation.

I've tried alot of combinations nothing is working.

Question by:AndyC1000
  • 3
  • 3
LVL 92

Expert Comment

ID: 35013984
String inputdir2 = inputDir.replace("visualisation", "report");

LVL 47

Expert Comment

ID: 35013985

Not sure I understand what is your question - maybe this is what you want:
inputdir2 = (new File(inputdir)).getParentPath() + File.separator + "report"
LVL 92

Expert Comment

ID: 35013996
this is probably a little safer:

String inputdir2 = inputDir.replaceFirst("visualisation$", "report");
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 35014013
The program gets the first path from an argument.

I wont be able to define what do replace other than knowing that Report must be inserted into the same level as Visualisation (which could be abc etc).

I've tried but it doesn't work

inputdir += "\\..\\Report";

Open in new window

LVL 47

Expert Comment

ID: 35014025
So you mean that they not necessary would be at the end of the path? it may be

data/results/visualistaion/abc  --> data/results/report/abc  
LVL 92

Accepted Solution

objects earned 250 total points
ID: 35014028
try this:

String inputDir2 = new File(new File(inputDir).getParentFile(), "report").getPath();
LVL 47

Assisted Solution

for_yan earned 250 total points
ID: 35014096

If visualistaion and report could be in the middle of the path,
then you want to split with something like that:

 String[] subDirs = path.split(Pattern.quote(File.separator));

String s = 0;
for(int j=0; j<subDirs.length; j++){
if(subDirs[j].equals("visualisation"))s +=  File.separator + "results";
else s +=  File.separator  + subDirs[j];


Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
In this post we will learn different types of Android Layout and some basics of an Android App.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question