Skip to main content
improved question
Source Link
halloei
  • 211
  • 1
  • 2
  • 9

I have ana configuration file which holds several file paths.

If these paths are relative, I want them to be relative to the path of the configuration file (and not to the path of the JVM processuser working directory ("user.dir")).

Since it's not possible to change the working directory with Java, I've written this method:

public static File newFile(String parent, String child) {
  File file = new File(child);

  if(file.isAbsolute()) {
    return file;
  }

  return new File(parent, child);
}

So

File importDirectory = newFile("/project/configuration", "../import");

creates the path /project/import and notThe folder of my config file would be /importparent here.

Is there a better solution than this?

I have an configuration file which holds several file paths.

If these paths are relative, I want them to be relative to the path of the configuration file (and not to the path of the JVM process).

Since it's not possible to change the working directory with Java, I've written this method:

public static File newFile(String parent, String child) {
  File file = new File(child);

  if(file.isAbsolute()) {
    return file;
  }

  return new File(parent, child);
}

So

File importDirectory = newFile("/project/configuration", "../import");

creates the path /project/import and not /import.

Is there a better solution than this?

I have a configuration file which holds several file paths.

If these paths are relative, I want them to be relative to the path of the configuration file (and not to the path of the user working directory ("user.dir")).

Since it's not possible to change the working directory with Java, I've written this method:

public static File newFile(String parent, String child) {
  File file = new File(child);

  if(file.isAbsolute()) {
    return file;
  }

  return new File(parent, child);
}

The folder of my config file would be parent here.

Is there a better solution than this?

edited tags
Link
200_success
  • 145.6k
  • 22
  • 191
  • 481
added an example
Source Link
halloei
  • 211
  • 1
  • 2
  • 9

I have an configuration file which holds several file paths.

If these paths are relative, I want them to be relative to the path of the configuration file (and not to the path of the JVM process).

Since it's not possible to change the working directory with Java, I've written this method:

public static File newFile(String parent, String child) {
  File file = new File(child);

  if(file.isAbsolute()) {
    return file;
  }

  return new File(parent, child);
}

So

File importDirectory = newFile("/project/configuration", "../import");

creates the path /project/import and not /import.

Is there a better solution than this?

I have an configuration file which holds several file paths.

If these paths are relative, I want them to be relative to the path of the configuration file (and not to the path of the JVM process).

Since it's not possible to change the working directory with Java, I've written this method:

public static File newFile(String parent, String child) {
  File file = new File(child);

  if(file.isAbsolute()) {
    return file;
  }

  return new File(parent, child);
}

Is there a better solution than this?

I have an configuration file which holds several file paths.

If these paths are relative, I want them to be relative to the path of the configuration file (and not to the path of the JVM process).

Since it's not possible to change the working directory with Java, I've written this method:

public static File newFile(String parent, String child) {
  File file = new File(child);

  if(file.isAbsolute()) {
    return file;
  }

  return new File(parent, child);
}

So

File importDirectory = newFile("/project/configuration", "../import");

creates the path /project/import and not /import.

Is there a better solution than this?

Tweeted twitter.com/#!/StackCodeReview/status/555273300189134848
Source Link
halloei
  • 211
  • 1
  • 2
  • 9
Loading