Java File Object Create toFile(File dir, String[] path)

Here you can find the source of toFile(File dir, String[] path)

Description

Build a full path of file with given directory, and relative path.

License

Apache License

Parameter

Parameter Description
dir a parameter
path a parameter

Exception

Parameter Description
IOException an exception

Declaration

public static File toFile(File dir, String[] path) throws IOException 

Method Source Code

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

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

public class Main {
    /**//from   w  w w .  ja v  a2  s.  c  om
     * Build a full path of file with given directory, and relative path.
     * 
     * @param dir
     * @param path
     * @return
     * @throws IOException
     */
    public static File toFile(File dir, String[] path) throws IOException {
        String dirPath = dir.getCanonicalPath();
        StringBuilder sb = new StringBuilder(dirPath);
        for (int i = 0; i < path.length; i++) {
            sb.append(File.separator);
            sb.append(path[i]);
        }
        return new File(sb.toString());
    }

    /**
     * Concatenate strings with the given separator char.
     * 
     * @param strs
     * @param separator
     * @return
     */
    public static String toString(String[] strs, char separator) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < strs.length; i++) {
            sb.append(strs[i]);
            if (i + 1 < strs.length) {
                sb.append(separator);
            }
        }
        return sb.toString();
    }

    /**
     * Convert the bytes starting at the given index to a string of
     * the given length.
     * 
     * @param bytes
     * @param index
     * @param strLeng
     * @return
     */
    public static String toString(byte[] bytes, int index, int strLeng) {
        int typeBytes = Character.SIZE / Byte.SIZE;
        checkBounds(bytes, index, typeBytes * strLeng);
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < strLeng; i++) {
            int c = 0;
            for (int j = 0; j < typeBytes; j++) {
                int b = bytes[index++];
                b = (b < 0) ? b + 256 : b;
                int shiftBits = Byte.SIZE * j;
                c = (c << shiftBits) + b;
            }
            sb.append((char) c);
        }
        return sb.toString();
    }

    /**
     * Utility method used to bounds check the byte array
      * and requested offset & length values.
      * 
     * @param bytes
     * @param offset
     * @param length
     */
    public static void checkBounds(byte[] bytes, int offset, int length) {
        if (length < 0)
            throw new IndexOutOfBoundsException("Index out of range: " + length);
        if (offset < 0)
            throw new IndexOutOfBoundsException("Index out of range: " + offset);
        if (offset > bytes.length - length)
            throw new IndexOutOfBoundsException("Index out of range: " + (offset + length));
    }
}

Related

  1. toFile(byte[] bFile)
  2. toFile(Class cls)
  3. toFile(File baseDir, String path)
  4. toFile(File currentDirectory, String file)
  5. toFile(File dstPath, String fullClassName)
  6. toFile(File file, byte[] data)
  7. toFile(IFile file)
  8. toFile(InputStream in, File file)