Say you have a large legacy ColdFusion on top of Java on top of Windows application. File access is done both via java.io.File and by CFFILE (which in turn also uses java.io.File), but not centralised in any way into a single file access library. Further, say you have file paths both hard-coded in the code, and also in a database.
In other words, assume the file paths themselves cannot change. They could be either local or remote Windows file paths:
Is there a way to run this application on Linux with minimal code changes? I'm looking for creative solutions that do not involve touching the legacy code.
Some ideas:
getAbsolutePath() : This file path method returns the absolute path of the file. If File is created with absolute pathname, it simply returns the pathname. If the file object is created using a relative path, the absolute pathname is resolved in a system-dependent way.
You can determine the absolute path of any file in Windows by right-clicking a file and then clicking Properties. In the file properties first look at the "Location:" which is the path to the file.
The method java. io. File. getAbsolutePath() is used to obtain the absolute path of a file in the form of a string.
To make our program platform independent, we should always use these separators to create file path or read any system variables like PATH, CLASSPATH.
Is there a way to override java.io.File to perform the file path translation with custom code? In this case, I would translate the remote paths to a mount point
Yes, you can perform your own implementation of java.io.File
and place it in a separate jar and then load it instead of the real java.io.File
.
To have the JVM load it you can either use the java.endorsed.dirs
property or java -Xbootclasspath/p:path
option in the java launcher ( java )
BUT!!!
Creating your own version of the java.io.File
class won't be as easy as modifying the legacy source code.
If you're afraid of breaking something you could first extract all your hardcoded paths to use a resource bundle:
So:
File file = new File("C:\\Users\\oreyes\\etc.txt");
Would be:
File file = new File( Paths.get( "user.dir.etc" ));
And Paths
would have a resource bundle internally
class Paths {
private static ResourceBundle rs = ResourceBundle.getBundle("file.paths");
public static String get( String key ) {
rs.getString( key );
}
}
You can have all this hardcoded paths extraction with an IDE ( usually an internationalization plugin )
Provide a different resource bundle for Linux and your done. Test and re-test and re-test
So, use provide your own java.io.File
only as a last resource.
The java.io.File
is not a final class, and can be extended. In your extension you could have one (or more) constructors that will translate the file paths. This would only require you to write the translator, and change every initialisation of File to you extended class.
Since your class would extend java.io.File
there is no other code change required other then changing the initialisation.
java.io.File file1 = new ClassThatExtendsFile("C:\temp\file.txt");
[edit]: or you could extend the CFFILE, and override it's constructors.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With