Example usage for java.io File listFiles

List of usage examples for java.io File listFiles

Introduction

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

Prototype

public File[] listFiles() 

Source Link

Document

Returns an array of abstract pathnames denoting the files in the directory denoted by this abstract pathname.

Usage

From source file:luceneprueba.utils.FileParser.java

static public void createFilesFromJSONArray() throws IOException {
    File dir = new File("files/input");
    File[] files = dir.listFiles();

    if (files == null) {
        System.out.println("No existe la carpeta \'input\' dentro de la carpeta files.");
        return;/*from   www  .j a  va2  s. c  o m*/
    }

    if (files.length == 0) {
        System.out.println("No hay ningun archivo en la carpeta \'input\' para ser indexado");
        return;
    }

    BufferedReader br;
    String fileContent;
    JSONArray jsonArray = null;
    JSONParser jsonParser = new JSONParser();
    int i = 1;
    FileWriter datosReviews = null;
    try {
        datosReviews = new FileWriter("files/output/datos_reviews.txt");
    } catch (IOException ex) {
        ex.printStackTrace(System.out);
    }
    for (File file : files) {
        if (file.isFile() && file.canRead() && file.getName().endsWith(".txt")) {
            System.out.println("Leyendo el archivo: " + file.getName());
            FileWriter contentReviews;
            try {
                br = new BufferedReader(new FileReader(file));
                fileContent = br.readLine();
                jsonArray = (JSONArray) jsonParser.parse(fileContent);
                Iterator it = jsonArray.iterator();
                DecimalFormat formato = new DecimalFormat("000000");
                while (it.hasNext()) {
                    JSONObject json = (JSONObject) it.next();
                    if (json.get("Genre") != null && json.get("Date") != null) {
                        contentReviews = new FileWriter(
                                "files/output/clasificador/review_clasificador_" + formato.format(i) + ".txt");
                        datosReviews.write(Integer.toString(i) + "_" + (String) json.get("Date") + "_"
                                + (String) json.get("Genre") + "_" + (String) json.get("Score") + "\n");
                        contentReviews.write(Integer.toString(i) + " " + (String) json.get("Review"));
                        i++;
                        contentReviews.close();
                    }
                }
                br.close();
            } catch (FileNotFoundException ex) {
                ex.printStackTrace(System.out);
            } catch (IOException | ParseException ex) {
                ex.printStackTrace(System.out);
            }
        }
    }
    datosReviews.close();
}

From source file:Main.java

private static void RecursionDeleteFile(File file) {
    if (file.isFile()) {
        file.delete();// w  w w  .j  a  v a  2  s. c  o m
        return;
    }
    if (file.isDirectory()) {
        File[] childFile = file.listFiles();
        if (childFile == null || childFile.length == 0) {
            file.delete();
            return;
        }
        for (File f : childFile) {
            RecursionDeleteFile(f);
        }
        file.delete();
    }
}

From source file:Main.java

public static void RecursionDeleteFile(File file) {
    if (file.isFile()) {
        file.delete();//from w  w w  .  j  av a2 s  .  c o m
        return;
    }
    if (file.isDirectory()) {
        File[] childFile = file.listFiles();
        if (childFile == null || childFile.length == 0) {
            file.delete();
            return;
        }
        for (File f : childFile) {
            RecursionDeleteFile(f);
        }
        file.delete();
    }
}

From source file:com.facebook.stetho.inspector.domstorage.SharedPreferencesHelper.java

public static List<String> getSharedPreferenceTags(Context context) {
    ArrayList<String> tags = new ArrayList<String>();

    String rootPath = context.getApplicationInfo().dataDir + "/shared_prefs";
    File root = new File(rootPath);
    if (root.exists()) {
        for (File file : root.listFiles()) {
            String fileName = file.getName();
            if (fileName.endsWith(PREFS_SUFFIX)) {
                tags.add(fileName.substring(0, fileName.length() - PREFS_SUFFIX.length()));
            }/*www.  ja  v a  2s. c o  m*/
        }
    }

    return tags;
}

From source file:Main.java

public static void delete(File file) {
    if (file.isFile()) {
        file.delete();//from  w w  w. ja  v  a2s.co m
        return;
    }
    if (file.isDirectory()) {
        File[] childFile = file.listFiles();
        if (childFile == null || childFile.length == 0) {
            file.delete();
            return;
        }
        for (File f : childFile) {
            delete(f);
        }
        file.delete();
    }
}

From source file:Main.java

public static void deleteFile(File file) {
    if (file.isFile()) {
        file.delete();//  w  w  w  . ja  va 2s.c o m
        return;
    }
    if (file.isDirectory()) {
        File[] childFile = file.listFiles();
        if (childFile == null || childFile.length == 0) {
            file.delete();
            return;
        }
        for (File f : childFile) {
            deleteFile(f);
        }
        file.delete();
    }
}

From source file:Main.java

public static void delFilesFromPath(File filePath) {
    if (filePath == null) {
        return;/*from w  ww .  j  ava  2s.c  o  m*/
    }
    if (!filePath.exists()) {
        return;
    }
    File[] files = filePath.listFiles();
    for (int i = 0; i < files.length; i++) {
        if (files[i].isFile()) {
            files[i].delete();
        } else {
            delFilesFromPath(files[i]);
            files[i].delete();
        }
    }
}

From source file:ch.ethz.topobench.ProduceLP.java

/**
 * Deletion of possibly multiple folder and
 * files within those folders (not recursively).
 *
 * Skips .gitignore and README files./*  w w  w.  j a v  a 2  s. c o  m*/
 *
 * Adapted from:
 * http://stackoverflow.com/questions/7768071/how-to-delete-directory-content-in-java
 *
 * @param folders    Folder to delete
 */
private static void removeFilesIn(String... folders) {
    for (String fol : folders) {
        File folder = new File(fol);
        File[] files = folder.listFiles();
        if (files != null) {
            for (File f : files) {
                if (f.isFile() && !f.getName().equals(".gitignore") && !f.getName().equals("README")) {
                    f.delete();
                }
            }
        }
    }
}

From source file:com.amazonaws.encryptionsdk.internal.TestIOUtils.java

public static void deleteDir(final File filePath) {
    if (filePath.exists()) {
        File[] files = filePath.listFiles();
        for (int i = 0; i < files.length; i++) {
            if (files[i].isFile()) {
                files[i].delete();//w  w w  .j  a  v a2 s  .co m
            } else {
                deleteDir(files[i]);
            }
        }
    }

    filePath.delete();
}

From source file:Main.java

public static boolean hasSDCard(Context context) {
    if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)) {
        String dir = Environment.getExternalStorageDirectory() + FILE_DIR;
        File f = new File(dir);
        if (!f.exists()) {
            f.mkdir();/*from  w  w  w  .j a  v a  2 s.  c  om*/
        } else {
            try {
                for (File t : f.listFiles()) {
                    t.delete();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return true;
    }
    return false;
}