Example usage for java.io File getParent

List of usage examples for java.io File getParent

Introduction

In this page you can find the example usage for java.io File getParent.

Prototype

public String getParent() 

Source Link

Document

Returns the pathname string of this abstract pathname's parent, or null if this pathname does not name a parent directory.

Usage

From source file:com.homeproject.tafulop.fxtest.FileHandling.java

/**
 * Returns a File which points to the newly created resized file. Automatically created the necessary folders.
 * @param toResize The File that should be resized.
 * @return The File points to the new, resized file.
 *///from w  w  w.j  a v a2s.c  om
public static File getResizedFile(File toResize) {

    File resizedFolder = checkResizedFolder(toResize.getParent());
    File resizedFile = new File(resizedFolder.getAbsolutePath() + DIR_SEPARATOR + toResize.getName());
    return resizedFile;
}

From source file:Main.java

/**
 * return a file handle for a manifest file derived from the shared file name
 * //  w w w.j ava 2  s. c om
 * @param path the path to the shared file
 * @return a file handle to the manifest file
 */
public static File getManifestPath(String path) {
    File mManifestPath = new File(path);
    File mManifestFile = new File(mManifestPath.getParent(), ".manifest-" + mManifestPath.getName());
    return mManifestFile;
}

From source file:Main.java

public static void copyFile(File sourceFile, File destFile) throws IOException {

    new File(destFile.getParent()).mkdirs();

    if (!destFile.exists()) {
        destFile.createNewFile();//from  w  w  w  .  j  a v  a  2 s .com
    }

    FileChannel source = null;
    FileChannel destination = null;

    try {
        source = new FileInputStream(sourceFile).getChannel();
        destination = new FileOutputStream(destFile).getChannel();
        destination.transferFrom(source, 0, source.size());
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (source != null) {
            source.close();
        }
        if (destination != null) {
            destination.close();
        }
    }
}

From source file:Main.java

/**
 * Devuelve el fully qualified name de un Group a partir
 * del path de una pagina (preferentemente el absoluto)
 *//*from ww w  .j a va  2 s .  c o  m*/
public static String getImportGroup(String path) {
    File arch = new File(path);
    path = arch.getParent();

    String packAux = "";
    String CADENA_CORTE = "\\";
    String CADENA_CORTE2 = "/";
    String SOURCE = "src";
    String USE_CASES = "useCases";
    String PAGES = "pages";

    try {
        packAux = path.substring((path.indexOf(CADENA_CORTE)));
    } catch (StringIndexOutOfBoundsException e) {
        packAux = path.substring((path.indexOf(CADENA_CORTE2)));
    }

    packAux = packAux.replace("/", ".");
    packAux = packAux.replace("\\", ".");
    if (packAux.endsWith(".")) {
        packAux = (String) packAux.subSequence(0, packAux.lastIndexOf("."));

    }
    packAux = packAux.substring(CADENA_CORTE.length());
    String packageF = packAux;
    String groupImport = packageF.substring(packageF.lastIndexOf(SOURCE));
    groupImport = groupImport.replace(SOURCE + ".", "");
    groupImport = groupImport.replace(PAGES, "");
    String caseName = groupImport.substring(groupImport.lastIndexOf(USE_CASES));
    caseName = caseName.replace(".", "");
    caseName = caseName.replace(USE_CASES, "");
    caseName = capitalize(caseName);

    return groupImport + caseName + "Group";
}

From source file:Main.java

public static File createFileWithDirs(String filename) {
    try {//from  ww  w . j  a  v  a 2  s  .c  o  m
        File file = new File(filename);

        String path = file.getParent();
        if (path != null) {
            new File(path).mkdirs();
        }
        return file;
    } catch (Exception e) {
        System.err.println("Unable to create file: " + filename);
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

public static void compressDir(File file) throws IOException {
    FileOutputStream f = new FileOutputStream(file.getParent() + file.getName() + ".zip");
    CheckedOutputStream cs = new CheckedOutputStream(f, new Adler32());
    ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(cs));

    compressDir(file, out, file.getAbsolutePath());

    out.flush();/*from   www .  j a  v  a2s.c  om*/
    out.close();
}

From source file:Main.java

/**
 * This method allow to append a message to an error file
 *
 * @param filename//from   w w w  .  java2s. co m
 *            the error file to write
 * @param message
 *            the message to append to file
 */
public static void errorLog(String filename, String message) {
    try {
        File file = new File(filename);
        String path = file.getParent();
        if (path != null) {
            new File(path).mkdirs();
        }
        FileOutputStream fos = new FileOutputStream(file, true); // true->append
        String time = "" + (new java.sql.Timestamp(System.currentTimeMillis()));
        message = time + "  " + message + "\n";
        fos.write(message.getBytes());
        fos.close();
    } catch (Exception e) {
        System.err.println("Unable to write file: " + filename);
        e.printStackTrace();
    }
}

From source file:com.wavemaker.tools.ws.WSDLTest.java

private static List<com.wavemaker.tools.io.File> convertToFileSystemFileList(List<File> files) {
    List<com.wavemaker.tools.io.File> rtn = new ArrayList<com.wavemaker.tools.io.File>();
    for (File file : files) {
        rtn.add(new LocalFolder(file.getParent()).getFile(file.getName()));
    }//from w  w  w  .j av a2  s . com
    return rtn;
}

From source file:com.compomics.pladipus.core.control.util.JarLookupService.java

/**
 * This method looks for the appropriate jar in the given folder (excludes the lib folder)
 * @param regex the regex to match the file
 * @param searchRoot the root directory to start te search
 * @return a file matching the regex//from   w  ww  .  jav  a 2 s  . c  o  m
 * @throws UnspecifiedPladipusException
 */
public static File lookupFile(String regex, File searchRoot) throws UnspecifiedPladipusException {
    if (!searchRoot.isDirectory()) {
        throw new IllegalArgumentException(searchRoot + " is no directory.");
    }
    final Pattern p = Pattern.compile(regex);
    Iterator iterateFiles = FileUtils.iterateFiles(searchRoot, new String[] { "jar" }, true);
    List<File> matchingFiles = new ArrayList<>();
    while (iterateFiles.hasNext()) {
        File file = (File) iterateFiles.next();
        if (p.matcher(file.getName()).matches() & !file.getParent().equalsIgnoreCase("lib")) {
            matchingFiles.add(file);
        }
    }
    if (matchingFiles.size() > 1) {
        throw new UnspecifiedPladipusException("There are multiple file candidates (" + matchingFiles.size()
                + "), please ensure only a single version is present and try again");
    } else if (matchingFiles.isEmpty()) {
        throw new UnspecifiedPladipusException("There are no matching files present");
    }
    return matchingFiles.get(0);
}

From source file:eu.optimis.sm.gui.utils.ConfigManager.java

private static void createDefaultConfigFile(File fileObject) throws Exception {

    new File(fileObject.getParent()).mkdirs();
    new File(fileObject.getAbsolutePath()).delete();
    new File(fileObject.getAbsolutePath()).createNewFile();

    log.debug("Copying file " + fileObject.getName());
    InputStream streamIn = ConfigManager.class.getResourceAsStream("/" + fileObject.getName());
    FileOutputStream streamOut = new FileOutputStream(fileObject.getAbsolutePath());
    byte[] buf = new byte[8192];
    while (true) {
        int length = streamIn.read(buf);
        if (length < 0) {
            break;
        }/*  www .  j  av  a 2 s.c  om*/
        streamOut.write(buf, 0, length);
    }

    try {
        streamIn.close();
    } catch (IOException ex) {
        log.error("Couldn't close input stream");
        log.error(ex.getMessage());
    }
    try {
        streamOut.close();
    } catch (IOException ex) {
        log.error("Couldn't close file output stream");
        log.error(ex.getMessage());
    }
}