Generate a Relative Path

Generate a Relative Path

Sometimes in an application you will have a “home” directory associated with the application, and then some files associated with that home directory. To have the files in an independent position relative to the root of the file system, you may want to use relative paths to the files rather than absolute or canonical paths. If your input specifies files in absolute form, you can use the following code to generate the relative path from a ‘home’ directory to a file, whether it resides under that directory or in some other place in the file system. Examples:

 home = "/a/b/c"file = "/a/b/c/d/e.txt"relative path = "d/e.txt"home = "/a/b/c"file = "/a/d/f/g.txt"relative path = "../../d/f/g.txt"--------------------------------------------------------------------------import java.io.*;import java.util.*;/** * this class provides functions used to generate a relative path * from two absolute paths * @author David M. Howard */public class RelativePath {	/**	 * break a path down into individual elements and add to a list.	 * example : if a path is /a/b/c/d.txt, the breakdown will be [d.txt,c,b,a]	 * @param f input file	 * @return a List collection with the individual elements of the path inreverse order	 */	private static List getPathList(File f) {		List l = new ArrayList();		File r;		try {			r = f.getCanonicalFile();			while(r != null) {				l.add(r.getName());				r = r.getParentFile();			}		}		catch (IOException e) {			e.printStackTrace();			l = null;		}		return l;	}	/**	 * figure out a string representing the relative path of	 * 'f' with respect to 'r'	 * @param r home path	 * @param f path of file	 */	private static String matchPathLists(List r,List f) {		int i;		int j;		String s;		// start at the beginning of the lists		// iterate while both lists are equal		s = "";		i = r.size()-1;		j = f.size()-1;		// first eliminate common root		while((i >= 0)&&(j >= 0)&&(r.get(i).equals(f.get(j)))) {			i--;			j--;		}		// for each remaining level in the home path, add a ..		for(;i>=0;i--) {			s += ".." + File.separator;		}		// for each level in the file path, add the path		for(;j>=1;j--) {			s += f.get(j) + File.separator;		}		// file name		s += f.get(j);		return s;	}	/**	 * get relative path of File 'f' with respect to 'home' directory	 * example : home = /a/b/c	 *           f    = /a/d/e/x.txt	 *           s = getRelativePath(home,f) = ../../d/e/x.txt	 * @param home base path, should be a directory, not a file, or it doesn'tmake sense	 * @param f file to generate path for	 * @return path from home to f as a string	 */	public static String getRelativePath(File home,File f){		File r;		List homelist;		List filelist;		String s;		homelist = getPathList(home);		filelist = getPathList(f);		s = matchPathLists(homelist,filelist);		return s;	}	/**       * test the function       */	public static void main(String args[]) {		if (args.length != 2) {			System.out.println("RelativePath  ");			return;		}		System.out.println("home = " + args[0]);		System.out.println("file = " + args[1]);		System.out.println("path = " + getRelativePath(new File(args[0]),newFile(args[1])));	}}
Share the Post:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes