Java URL to File Name getFileAbsoluteURL(String[] directoryPaths, String fileName)

Here you can find the source of getFileAbsoluteURL(String[] directoryPaths, String fileName)

Description

get File Absolute URL

License

LGPL

Declaration

public static String getFileAbsoluteURL(String[] directoryPaths, String fileName) throws IOException 

Method Source Code


//package com.java2s;
//License from project: LGPL 

import java.io.File;
import java.io.IOException;

import java.net.MalformedURLException;

public class Main {
    public static String getFileAbsoluteURL(String[] directoryPaths, String fileName) throws IOException {
        File fileToReturn = null;
        String fullPathNameToReturn = null;
        int numFound = 0;

        // Check for Absolute path
        File file = new File(fileName);
        if (file.isAbsolute() && file.exists() && file.isFile() && file.canRead()) {
            numFound = 1;/*from  www.jav  a  2s.  c  o  m*/
            fileToReturn = file;
            try {
                fullPathNameToReturn = file.toURI().toURL().toExternalForm();
            } catch (MalformedURLException e) {
                e.printStackTrace();
            }
        } else if (directoryPaths != null && directoryPaths.length != 0) {
            for (String pathPrefix : directoryPaths) {
                file = new File(pathPrefix, fileName);
                if (file.exists() && file.isFile() && file.canRead()) {
                    if (fileToReturn == null) {
                        fileToReturn = file;
                    }
                    numFound++;
                }
            }
        }
        if (fileToReturn != null) {
            try {
                fullPathNameToReturn = fileToReturn.toURI().toURL().toExternalForm();
            } catch (MalformedURLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        if (numFound == 0) {
            String errmsg = "Fatal error: Unable to find specified properties file: " + fileName;
            // logger.error(errmsg);
            throw new IOException(errmsg);
        } else if (numFound == 1) {
            // logger.debug("Opening file: " + fileToReturn.getAbsolutePath());
        } else if (numFound > 1) {
            // logger.info("Opening file (instead of "+(numFound-1)+
            // " other options): " + fileToReturn.getAbsolutePath());
        }
        return (fullPathNameToReturn);
    }
}

Related

  1. getFile(URL url)
  2. getFile(URL url)
  3. getFile(URL url)
  4. getFile(URL url)
  5. getFile(URL url, String description)
  6. getFileDateTime(URL url)
  7. getFileExtensionByURL(URL url)
  8. getFileFor(URL url1)
  9. getFileForURL(String url)