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");
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!


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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
sumHeights2  challenge 7 78
countHi challenge 25 86
noX challenge 17 77
ArrayIndexOutOfBoundException 9 39
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

747 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now