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:br.com.ifpb.bdnc.projeto.geo.system.MultipartData.java

public boolean saveImage(String path, FileItemStream item, String nameToSave) {
    try {/*from w w  w  .j a  v a2  s.co m*/
        File f = new File(path + File.separator + folder);
        File parent = new File(f.getParent());

        if (!parent.exists())
            parent.mkdir();
        if (!f.exists())
            f.mkdir();

        System.out.println(f.getAbsolutePath());

        File savedFile = new File(f.getAbsoluteFile() + File.separator + nameToSave);

        FileOutputStream fos = new FileOutputStream(savedFile);
        InputStream is = item.openStream();

        int x = 0;
        byte[] b = new byte[1024];
        while ((x = is.read(b)) != -1) {
            fos.write(b, 0, x);
        }
        fos.flush();
        fos.close();

        return true;

    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return false;
}

From source file:gool.executor.cpp.CppCompiler.java

@Override
public String run(File file, List<File> classPath) throws FileNotFoundException {
    List<String> params = new ArrayList<String>();

    List<String> deps = new ArrayList<String>();
    if (classPath != null) {
        for (File dependency : classPath) {
            deps.add(dependency.getParent());
        }/*from www .j ava2s  .  co m*/
    }
    for (File dependency : getDependencies()) {
        deps.add(dependency.getParent());
    }

    Map<String, String> env = new HashMap<String, String>();

    params.addAll(Arrays.asList("./" + file.getName()));
    return Command.exec(getOutputDir(), params, env);
}

From source file:mSearch.filmlisten.WriteFilmlistJson.java

public void filmlisteSchreibenJson(String datei, ListeFilme listeFilme) {

    ZipOutputStream zipOutputStream = null;
    XZOutputStream xZOutputStream = null;
    JsonGenerator jg = null;//from  w  w  w. j  av a  2s. c o m

    FileWriter fileWriter = null;
    CSVPrinter csvFilePrinter = null;
    try {
        Log.sysLog("Filme schreiben (" + listeFilme.size() + " Filme) :");
        File file = new File(datei);
        File dir = new File(file.getParent());
        if (!dir.exists()) {
            if (!dir.mkdirs()) {
                Log.errorLog(915236478, "Kann den Pfad nicht anlegen: " + dir.toString());
            }
        }
        Log.sysLog("   --> Start Schreiben nach: " + datei);

        CSVFormat csvFileFormat = CSVFormat.DEFAULT.withDelimiter(';').withQuote('\'')
                .withRecordSeparator("\n");
        fileWriter = new FileWriter(datei);
        csvFilePrinter = new CSVPrinter(fileWriter, csvFileFormat);

        // Infos der Felder in der Filmliste
        csvFilePrinter.printRecord(DatenFilm.COLUMN_NAMES);

        //Filme schreiben
        DatenFilm datenFilm;
        Iterator<DatenFilm> iterator = listeFilme.iterator();
        while (iterator.hasNext()) {
            datenFilm = iterator.next();
            datenFilm.arr[DatenFilm.FILM_NEU] = Boolean.toString(datenFilm.isNew()); // damit wirs beim nchsten Programmstart noch wissen

            List<String> filmRecord = new ArrayList<String>();
            for (int i = 0; i < DatenFilm.JSON_NAMES.length; ++i) {
                int m = DatenFilm.JSON_NAMES[i];
                filmRecord.add(datenFilm.arr[m].replace("\n", "").replace("\r", ""));
            }
            csvFilePrinter.printRecord(filmRecord);
        }
        Log.sysLog("   --> geschrieben!");
    } catch (Exception ex) {
        Log.errorLog(846930145, ex, "nach: " + datei);
    } finally {
        try {
            fileWriter.flush();
            fileWriter.close();
            csvFilePrinter.close();
        } catch (Exception e) {
            Log.errorLog(732101201, e, "close stream: " + datei);
        }
    }
}

From source file:com.splunk.shuttl.archiver.LocalFileSystemPathsTest.java

@Test(groups = { "fast-unit" })
public void getArchiverDirectory_givenTestDirectory_dirIsChildToTestDirectory() {
    assertDoesNotExist(testDirectoryPath);
    File dir = localFileSystemPaths.getArchiverDirectory();
    assertEquals(testDirectoryPath, dir.getParent());
}

From source file:com.photon.phresco.impl.BuildInfoParameterImpl.java

private StringBuilder getBuildInfoPath(String rootModulePath, String subModuleName) throws PhrescoException {
    File pomFileLocation = Utility.getPomFileLocation(rootModulePath, subModuleName);
    StringBuilder builder = new StringBuilder(pomFileLocation.getParent());
    builder.append(File.separator);
    builder.append("do_not_checkin");
    builder.append(File.separator);
    builder.append("build");
    builder.append(File.separator);
    builder.append("build.info");
    return builder;
}

From source file:gool.executor.csharp.CSharpCompiler.java

@Override
public String run(File file, List<File> classPath) throws FileNotFoundException {
    String[] runTest = IS_WINDOWS ? new String[] { new File(getOutputDir(), file.getName()).getAbsolutePath() }
            : new String[] { "mono", file.getName() };
    List<String> params = new ArrayList<String>();

    List<String> deps = new ArrayList<String>();
    if (classPath != null) {
        for (File dependency : classPath) {
            deps.add(dependency.getParent());
        }//from   w w w.j  a  va 2 s.  c o  m
    }
    for (File dependency : getDependencies()) {
        deps.add(dependency.getParent());
    }

    Map<String, String> env = new HashMap<String, String>();
    env.put("MONO_PATH", StringUtils.join(deps, File.separator));

    params.addAll(Arrays.asList(runTest));
    return Command.exec(getOutputDir(), params, env);
}

From source file:FileCopy.java

public static void copy(String fromFileName, String toFileName) throws IOException {
    File fromFile = new File(fromFileName);
    File toFile = new File(toFileName);

    if (!fromFile.exists())
        throw new IOException("FileCopy: " + "no such source file: " + fromFileName);
    if (!fromFile.isFile())
        throw new IOException("FileCopy: " + "can't copy directory: " + fromFileName);
    if (!fromFile.canRead())
        throw new IOException("FileCopy: " + "source file is unreadable: " + fromFileName);

    if (toFile.isDirectory())
        toFile = new File(toFile, fromFile.getName());

    if (toFile.exists()) {
        if (!toFile.canWrite())
            throw new IOException("FileCopy: " + "destination file is unwriteable: " + toFileName);
        System.out.print("Overwrite existing file " + toFile.getName() + "? (Y/N): ");
        System.out.flush();//w  w  w.j a v a  2s . co m
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        String response = in.readLine();
        if (!response.equals("Y") && !response.equals("y"))
            throw new IOException("FileCopy: " + "existing file was not overwritten.");
    } else {
        String parent = toFile.getParent();
        if (parent == null)
            parent = System.getProperty("user.dir");
        File dir = new File(parent);
        if (!dir.exists())
            throw new IOException("FileCopy: " + "destination directory doesn't exist: " + parent);
        if (dir.isFile())
            throw new IOException("FileCopy: " + "destination is not a directory: " + parent);
        if (!dir.canWrite())
            throw new IOException("FileCopy: " + "destination directory is unwriteable: " + parent);
    }

    FileInputStream from = null;
    FileOutputStream to = null;
    try {
        from = new FileInputStream(fromFile);
        to = new FileOutputStream(toFile);
        byte[] buffer = new byte[4096];
        int bytesRead;

        while ((bytesRead = from.read(buffer)) != -1)
            to.write(buffer, 0, bytesRead); // write
    } finally {
        if (from != null)
            try {
                from.close();
            } catch (IOException e) {
                ;
            }
        if (to != null)
            try {
                to.close();
            } catch (IOException e) {
                ;
            }
    }
}

From source file:com.eriwen.gradle.Digest.java

private File digestedFile(final File file, final String digest) {
    return getProject().file(file.getParent() + File.separator + digest + "-" + file.getName());
}

From source file:com.metadave.stow.TestStow.java

@Test
public void testGen() throws Exception {
    InputStream is = this.getClass().getResourceAsStream("/Stow.stg");
    File f = File.createTempFile("stow", "test.stg");
    String root = f.getParent();
    String classdir = root + File.separator + "stowtest";

    String stgPath = f.getAbsolutePath();
    String stowStg = org.apache.commons.io.IOUtils.toString(is);

    org.apache.commons.io.FileUtils.writeStringToFile(f, stowStg);

    File d = new File(classdir);
    d.mkdirs();//from   w  ww  .  j  a v a  2s.  c  om

    Stow.generateObjects(stgPath, "stowtest", "Test", classdir);

    File testSTBean = new File(classdir + File.separator + "TestSTBean.java");
    assertTrue(testSTBean.exists());

    File testSTAccessor = new File(classdir + File.separator + "TestSTAccessor.java");
    assertTrue(testSTAccessor.exists());

    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    compiler.run(null, null, null, testSTBean.getAbsolutePath());
    compiler.run(null, null, null, testSTAccessor.getAbsolutePath());

    File testSTAccessorClass = new File(classdir + File.separator + "TestSTAccessor.class");
    assertTrue(testSTAccessorClass.exists());

    File testSTBeanClass = new File(classdir + File.separator + "TestSTBean.class");
    assertTrue(testSTBeanClass.exists());

    URLClassLoader classLoader = URLClassLoader.newInstance(new URL[] { new File(root).toURI().toURL() });
    Class<?> cls = Class.forName("stowtest.TestSTBean", true, classLoader);

    Constructor<?> c = cls.getConstructors()[0];
    STGroup stg = new STGroupFile(f.getAbsolutePath());
    Object o = c.newInstance(stg);

    Set<String> methods = new HashSet<String>();
    methods.add("addPackage");
    methods.add("addBeanClass");
    methods.add("addTemplateName");
    methods.add("addAccessor");

    int count = 0;
    for (Method m : o.getClass().getMethods()) {
        if (methods.contains(m.getName())) {
            count++;
        }
    }
    assertEquals("Look for 8 generated methods", 8, count);
}

From source file:com.sharinglabs.cordova.plugin.cache.Cache.java

private void clearApplicationData() {
    File cache = this.cordova.getActivity().getCacheDir();
    File appDir = new File(cache.getParent());
    Log.i(LOG_TAG, "Absolute path: " + appDir.getAbsolutePath());
    if (appDir.exists()) {
        String[] children = appDir.list();
        for (String s : children) {
            if (!s.equals("lib")) {
                deleteDir(new File(appDir, s));
                Log.i(LOG_TAG, "File /data/data/APP_PACKAGE/" + s + " DELETED");
            }//  w ww  .ja v a 2 s  .  c  o m
        }
    }
}