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");
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.


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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
egit plugin on eclipse 8 62
table example 4 29
expectj telnet failing 5 35
maven module vs maven project 3 21
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…
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

816 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

8 Experts available now in Live!

Get 1:1 Help Now