Example usage for java.io File isDirectory

List of usage examples for java.io File isDirectory

Introduction

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

Prototype

public boolean isDirectory() 

Source Link

Document

Tests whether the file denoted by this abstract pathname is a directory.

Usage

From source file:Main.java

private static void copyFileOrDirectory(File source, File destination) throws IOException {
    if (source.isDirectory()) {
        if (!destination.exists()) {
            destination.mkdirs();/*from   w  ww  .j av  a2 s  .  c  o  m*/
        }

        File[] files = source.listFiles();
        for (File f : files) {
            copyFileOrDirectory(f, new File(destination, f.getName()));
        }
    } else {
        copyFile(source, destination);
    }
}

From source file:Main.java

/**
 * Delete the earliest file in the specified directory
 * /*  ww  w.ja v a 2  s  . c om*/
 * @param dir
 *            The specified directory
 * @param exceptFile
 *            Exclude the file name
 */
public static final void deleteEarliestFile(File dir, String exceptFile) {
    if (dir != null && dir.isDirectory()) {
        File earlyFile = null;
        File[] files = dir.listFiles();
        if (files.length == 0)
            return;
        for (int i = 0; i < files.length; i++) {
            File f = files[i];
            if (f.getName().equals(exceptFile))
                continue;
            if (earlyFile == null) {
                earlyFile = files[i];
                continue;
            }
            if (earlyFile.lastModified() > f.lastModified()) {
                earlyFile = f;
            }
        }
        if (earlyFile != null)
            earlyFile.delete();
    }
}

From source file:Main.java

public static void copyToMemory(Context context, String srcFilePath, String dictFileName) throws IOException {
    File srcFile = new File(srcFilePath);
    if (!srcFile.exists() || srcFile.isDirectory()) {
        return;//w w w  . j  a  v  a2  s  .c o  m
    }
    BufferedInputStream inBufferedInputStream = new BufferedInputStream(new FileInputStream(srcFile));
    FileOutputStream fos = context.openFileOutput(dictFileName, 0);
    BufferedOutputStream bos = new BufferedOutputStream(fos);
    byte[] b = new byte[1024 * 4];
    int len;
    while ((len = inBufferedInputStream.read(b)) != -1) {
        bos.write(b, 0, len);
        bos.flush();
    }
    inBufferedInputStream.close();
    bos.close();
}

From source file:Main.java

private static void innerListFiles(File dir, Collection<File> accumulator, FileFilter filter) {

    String[] filenames = dir.list();

    if (filenames != null) {
        for (int i = 0; i < filenames.length; i++) {
            File file = new File(dir, filenames[i]);
            if (file.isDirectory()) {
                innerListFiles(file, accumulator, filter);
            } else {
                if (filter != null && filter.accept(file)) {
                    accumulator.add(file);
                }/*from w w  w . j a v a2  s. com*/
            }
        }
    }
}

From source file:Main.java

public static boolean delete(String path) {
    if (path == null || path.isEmpty())
        return false;
    File f = new File(path);
    if (f.exists()) {
        if (f.isDirectory()) {
            return deleteDirectory(f);
        } else {//from   w ww .  jav  a  2 s.  c o  m
            return f.delete();
        }
    }
    return false;
}

From source file:eu.ggnet.dwoss.util.TempUtil.java

private static File tryPath(File path, String subPath) {
    if (path.isDirectory() && path.canWrite()) {
        File outputPath = new File(path, subPath);
        if (!outputPath.exists()) {
            if (!outputPath.mkdirs()) {
                return null;
            }//  w  w  w .jav a 2 s.  co m
        } else if (!(outputPath.isDirectory() && outputPath.canWrite())) {
            return null;
        }
        return outputPath;
    }
    return null;
}

From source file:Main.java

public static void startRecord() {
    if (!haveStarted) {

        haveStarted = true;/*ww w . j  a  va2s  . c  om*/

        mRecorder = new MediaRecorder();
        mRecorder.setAudioSource(MediaRecorder.AudioSource.VOICE_CALL);
        mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);

        File pp = new File(path);
        if (!pp.isDirectory() && !pp.exists()) {
            pp.mkdir();
        }

        currentFilePath = path + new SimpleDateFormat("yyyyMMddHHmmss").format(System.currentTimeMillis())
                + ".mp3";
        File saveFilePath = new File(currentFilePath);

        mRecorder.setOutputFile(saveFilePath.getAbsolutePath());

        mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

        try {
            mRecorder.prepare();
        } catch (IOException e) {
            Log.e(TAG, "prepare() failed");
        }
        mRecorder.start();
    }
}

From source file:Main.java

public static void getFileList(final List<File> fileList, final File root, FileFilter filter) {
    final File[] list = root.listFiles(filter);

    if (list == null) {
        return;/*from   w w  w  .j  av a 2  s .c om*/
    }

    for (final File f : list) {
        if (f.isDirectory()) {
            fileList.add(f);
            getFileList(fileList, f, filter);
        } else {
            fileList.add(f);
        }
    }
}

From source file:Main.java

private static void zipFile(ZipOutputStream zipOutputStream, String sourcePath) throws IOException {

    java.io.File files = new java.io.File(sourcePath);
    java.io.File[] fileList = files.listFiles();

    String entryPath = "";
    BufferedInputStream input = null;
    for (java.io.File file : fileList) {
        if (file.isDirectory()) {
            zipFile(zipOutputStream, file.getPath());
        } else {/*ww  w.j ava  2 s. co m*/
            byte data[] = new byte[BUFFER_SIZE];
            FileInputStream fileInputStream = new FileInputStream(file.getPath());
            input = new BufferedInputStream(fileInputStream, BUFFER_SIZE);
            entryPath = file.getAbsolutePath().replace(parentPath, "");

            ZipEntry entry = new ZipEntry(entryPath);
            zipOutputStream.putNextEntry(entry);

            int count;
            while ((count = input.read(data, 0, BUFFER_SIZE)) != -1) {
                zipOutputStream.write(data, 0, count);
            }
            input.close();
        }
    }

}

From source file:Main.java

static private void addFileToZip(String path, String srcFile, ZipOutputStream zip) throws IOException {

    File folder = new File(srcFile);
    if (folder.isDirectory()) {
        addFolderToZip(path, srcFile, zip, false);
    } else {/*from w  w w. ja  v  a  2s.co m*/
        byte[] buf = new byte[1024];
        int len;
        FileInputStream in = new FileInputStream(srcFile);
        try {
            zip.putNextEntry(new ZipEntry(path + "/" + folder.getName()));
            while ((len = in.read(buf)) > 0) {
                zip.write(buf, 0, len);
            }
        } finally {
            in.close();
        }
    }
}