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:Main.java

/**
 * get the FileOutputStream/*from w w  w . ja v a2 s . com*/
 * @param filePath the filePath must contain head "/"
 * @return
 */
public static FileOutputStream getFileOutputStream(String filePath) {
    FileOutputStream fouts = null;
    File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + filePath.trim());

    if (file.exists() && file.isFile()) {
        try {
            fouts = new FileOutputStream(file);
            Log.d("Ragnarok", "get the fouts path = " + file.getAbsolutePath());
            return fouts;
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    } else {
        try {
            File fileDirs = new File(file.getParent());
            fileDirs.mkdirs();
            //Log.d(LOG_TAG, "make the fileDirs " + fileDirs.getPath());
            //file.createNewFile();   
            //Log.d("Ragnarok", "create a new file name " + file.getName());
            Log.d("Ragnarok", "file path " + file.getAbsolutePath());
            synchronized (file) {
                file.createNewFile();
                fouts = new FileOutputStream(file);
                return fouts;
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
    return null;
}

From source file:org.dkpro.tc.core.util.ReportUtils.java

public static void writeExcelAndCSV(TaskContext context, String contextLabel, FlexTable<String> table,
        String evalFileName, String suffixExcel, String suffixCsv) {
    StorageService store = context.getStorageService();
    context.getLoggingService().message(contextLabel, ReportUtils.getPerformanceOverview(table));
    // Excel cannot cope with more than 255 columns
    if (table.getColumnIds().length <= 255) {
        context.storeBinary(evalFileName + "_compact" + suffixExcel, table.getExcelWriter());
    }//from ww w.  j a v a  2s. c o  m
    context.storeBinary(evalFileName + "_compact" + suffixCsv, table.getCsvWriter());
    table.setCompact(false);
    // Excel cannot cope with more than 255 columns
    if (table.getColumnIds().length <= 255) {
        context.storeBinary(evalFileName + suffixExcel, table.getExcelWriter());
    }
    context.storeBinary(evalFileName + suffixCsv, table.getCsvWriter());

    // output the location of the batch evaluation folder
    // otherwise it might be hard for novice users to locate this
    File dummyFolder = store.locateKey(context.getId(), "dummy");
    // TODO can we also do this without creating and deleting the dummy folder?
    context.getLoggingService().message(contextLabel,
            "Storing detailed results in:\n" + dummyFolder.getParent() + "\n");
    dummyFolder.delete();
}

From source file:com.jkoolcloud.tnt4j.streams.configure.state.AbstractFileStreamStateHandler.java

/**
 * Loads XML persisted streamed files access state from system temp directory.
 *
 * @param streamName//from   w  w  w. j av a 2  s.co  m
 *            stream name
 *
 * @return loaded streamed files access state
 *
 * @throws IOException
 *             if persisted state file can't be loaded
 * @throws JAXBException
 *             if state unmarshaling fails
 */
private static FileAccessState loadStateFromTemp(String streamName) throws IOException, JAXBException {
    final File tempFile = File.createTempFile("CHECK_PATH", null); // NON-NLS
    String path = tempFile.getParent();
    tempFile.delete();

    return loadStateFile(path, streamName);

}

From source file:com.sunchenbin.store.feilong.core.io.FileUtil.java

/**
 * ??????, null.//  w  w  w . j  a  v  a2  s  .co  m
 *
 * @param path
 *            the path
 * @return the parent
 * @see java.io.File#getParent()
 */
public static String getParent(String path) {
    if (Validator.isNullOrEmpty(path)) {
        throw new NullPointerException("pathname can't be null/empty!");
    }
    File file = new File(path);
    return file.getParent();
}

From source file:WarUtil.java

/**
 * WAR???????????/* www  .  j  a v  a2 s . c  o  m*/
 * 
 * @param warFile
 * @param directory
 * @throws IOException
 */
public static void extractWar(File warFile, File directory) throws IOException {
    try {
        long timestamp = warFile.lastModified();
        File warModifiedTimeFile = new File(directory, LAST_MODIFIED_FILE);
        long lastModified = readLastModifiled(warModifiedTimeFile);

        if (timestamp == lastModified) {
            //      log.info("war file " + warFile.getName() + " not modified.");
            return;
        }
        if (directory.exists()) {
            //         IOUtil.forceRemoveDirectory(directory);
            directory.mkdir();
        }

        //         log.info("war extract start. warfile=" + warFile.getName());

        JarInputStream jin = new JarInputStream(new BufferedInputStream(new FileInputStream(warFile)));
        JarEntry entry = null;

        while ((entry = jin.getNextJarEntry()) != null) {
            File file = new File(directory, entry.getName());

            if (entry.isDirectory()) {
                if (!file.exists()) {
                    file.mkdirs();
                }
            } else {
                File dir = new File(file.getParent());
                if (!dir.exists()) {
                    dir.mkdirs();
                }

                FileOutputStream fout = null;
                try {
                    fout = new FileOutputStream(file);
                    ResourceUtil.copyStream(jin, fout);
                } finally {
                    fout.flush();
                    fout.close();
                    fout = null;
                }

                if (entry.getTime() >= 0) {
                    file.setLastModified(entry.getTime());
                }
            }
        }

        writeLastModifiled(warModifiedTimeFile, timestamp);

        //log.info("war extract success. lastmodified=" + timestamp);
    } catch (IOException ioe) {
        //log.info("war extract fail.");
        throw ioe;
    }
}

From source file:Main.java

public static String removeExtention(String filePath) {
    File f = new File(filePath);

    // if it's a directory, don't remove the extention
    if (f.isDirectory())
        return filePath;

    String name = f.getName();//from  ww  w  .j  a v  a  2  s. c  o  m

    // Now we know it's a file - don't need to do any special hidden
    // checking or contains() checking because of:
    final int lastPeriodPos = name.lastIndexOf('.');
    if (lastPeriodPos <= 0) {
        // No period after first character - return name as it was passed in
        return filePath;
    } else {
        // Remove the last period and everything after it
        File renamed = new File(f.getParent(), name.substring(0, lastPeriodPos));
        return renamed.getPath();
    }
}

From source file:com.liferay.util.FileUtil.java

public static void write(File file, String s) throws IOException {
    if (file.getParent() != null) {
        mkdirs(file.getParent());/*  www . j av  a2  s  . c o m*/
    }

    BufferedWriter bw = new BufferedWriter(new FileWriter(file));

    bw.flush();
    bw.write(s);

    bw.close();
}

From source file:edu.umass.cs.gnsserver.installer.EC2Runner.java

private static void writeGNSINstallerConf(String configName) {
    File jarPath = getJarPath();
    System.out.println("Jar path: " + jarPath);
    String confFileDirectory = jarPath.getParent() + FILESEPARATOR + "conf" + FILESEPARATOR + configName
            + "-init";
    //String confFileLocation = jarPath.getParent() + FILESEPARATOR + "conf" + FILESEPARATOR + "gnsInstaller" + FILESEPARATOR + configName + ".xml";
    WriteConfFile.writeConfFiles(configName, confFileDirectory, keyName, ec2UserName, "linux", "MONGO",
            hostTable);//  w  w w .  j  av a  2s.  com
    //WriteConfFile.writeXMLFile(confFileLocation, keyName, ec2UserName, "linux", dataStoreType.toString(), hostTable);
}

From source file:org.hardisonbrewing.s3j.FileSyncer.java

static String getBucketPath(File file) {

    StringBuffer stringBuffer = new StringBuffer();
    while (file != null && file.getParent() != null) {
        if (stringBuffer.length() > 0) {
            stringBuffer.insert(0, File.separator);
        }//from w  ww  .  java 2  s .c  o m
        stringBuffer.insert(0, file.getName());
        file = file.getParentFile();
    }

    String path = stringBuffer.toString();
    path = path.replace(' ', '_');
    return path;
}

From source file:com.drunkendev.io.recurse.tests.RecursionTest.java

/**
 * Given a {@link File} object, test if it is likely to be a symbolic link.
 *
 * @param   file/*from ww  w. j  a v  a 2 s  .co  m*/
 *          File to test for symbolic link.
 * @return  {@code true} if {@code file} is a symbolic link.
 * @throws  NullPointerException
 *          If {@code file} is null.
 * @throws  IOException
 *          If a symbolic link could not be determined. This is ultimately
 *          caused by a call to {@link File#getCanonicalFile()}.
 */
private static boolean isSymbolicLink(File file) throws IOException {
    if (file == null) {
        throw new NullPointerException("File must not be null");
    }
    File canon;
    if (file.getParent() == null) {
        canon = file;
    } else {
        File canonDir = file.getParentFile().getCanonicalFile();
        canon = new File(canonDir, file.getName());
    }
    return !canon.getCanonicalFile().equals(canon.getAbsoluteFile());
}