/* * [TestFileCombine.java] * * Summary: combining two filenames with java.io.File. * * Copyright: (c) 2013-2017 Roedy Green, Canadian Mind Products, http://mindprod.com * * Licence: This software may be copied and used freely for any purpose but military. * http://mindprod.com/contact/nonmil.html * * Requires: JDK 1.8+ * * Created with: JetBrains IntelliJ IDEA IDE http://www.jetbrains.com/idea/ * * Version History: * 1.0 2013-03-16 initial version */ package com.mindprod.example; import com.mindprod.common18.EIO; import java.io.File; import java.io.IOException; import static java.lang.System.*; /** * combining two filenames with java.io.File. * * @author Roedy Green, Canadian Mind Products * @version 1.0 2013-03-16 initial version * @since 2013-03-16 */ public final class TestFileCombine { /** * Experiment with various ways of combining file names * * @param args not used * * @throws java.io.IOException on I/O failure. */ public static void main( String[] args ) throws IOException { // file is not suitable for resolving relative or absolute offsets from a base filename. File root = new File( "E:/mindprod" ); File o1 = new File( root, "index.html" ); out.println( EIO.getCanOrAbsPath( o1 ) ); // prints: E:/mindprod/index.html (actually with backslashes) File o2 = new File( root, "/index.html" ); out.println( EIO.getCanOrAbsPath( o2 ) ); // prints: E:/mindprod/index.html File base = new File( "E:/mindprod/jgloss/encoding" ); File o3 = new File( base, "pad.html" ); out.println( EIO.getCanOrAbsPath( o3 ) ); // prints: E:/mindprod/jgloss/encoding/pad.html File o4 = new File( base, "../pad.html" ); out.println( EIO.getCanOrAbsPath( o4 ) ); // prints: E:/mindprod/jgloss/pad.html File o5 = new File( base, "/jgloss/pad.html" ); out.println( EIO.getCanOrAbsPath( o5 ) ); // prints:E:/mindprod/jgloss/encoding/jgloss/pad.html (ouch) // You might have naively hoped for: E:/mindprod/jgloss/pad.html // However, File has no idea that / on your website refers to E:/mindprod. File base2 = new File( "E:/mindprod/jgloss/encoding/utf8.html" ); File o6 = new File( base2, "pad.html" ); out.println( EIO.getCanOrAbsPath( o6 ) ); // prints: E:/mindprod/jgloss/encoding/utf8.html/pad.html (ouch) // You might have hoped for: E:/mindprod/jgloss/encoding/pad.html File o7 = new File( base2, "../pad.html" ); out.println( EIO.getCanOrAbsPath( o7 ) ); // prints: E:/mindprod/jgloss/encoding/pad.html (ouch) // You might have hoped for: E:/mindprod/jgloss/pad.html File o8 = new File( base2, "/jgloss/pad.html" ); out.println( EIO.getCanOrAbsPath( o8 ) ); // prints: E:/mindprod/jgloss/encoding/utf8.html/jgloss/pad.html (ouch) // You might have naively hoped for: E:/mindprod/jgloss/pad.html // However, File has no idea that / on your website refers to E:/mindprod. } }