Example usage for java.io File canWrite

List of usage examples for java.io File canWrite

Introduction

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

Prototype

public boolean canWrite() 

Source Link

Document

Tests whether the application can modify the file denoted by this abstract pathname.

Usage

From source file:Main.java

private static boolean checkFsWritable() {
    // Create a temporary file to see whether a volume is really writeable.
    // It's important not to put it in the root directory which may have a
    // limit on the number of files.
    String directoryName = Environment.getExternalStorageDirectory().toString() + "/DCIM";
    File directory = new File(directoryName);
    if (!directory.isDirectory()) {
        if (!directory.mkdirs()) {
            return false;
        }/*w  w  w.  j a  va2 s. c o m*/
    }
    return directory.canWrite();
}

From source file:Main.java

public static void restoreDb(File currentDB, String restoreDBFileName) {
    try {//from   w  w  w . j  a  v a2s  . com
        backupDb(currentDB);
        File sd = Environment.getExternalStorageDirectory();
        File data = Environment.getDataDirectory();

        if (sd.canWrite()) {
            File f = new File(sd.getAbsolutePath() + "/AccountManagement");
            if (!f.exists()) {
                f.mkdir();
            }
            String restoreDBPath = "AccountManagement/" + restoreDBFileName;
            File restoreDB = new File(sd, restoreDBPath);

            if (currentDB.exists()) {
                FileChannel src = new FileInputStream(restoreDB).getChannel();
                FileChannel dst = new FileOutputStream(currentDB).getChannel();
                dst.transferFrom(src, 0, src.size());
                src.close();
                dst.close();
            }
        }
    } catch (Exception e) {
        System.out.println(e);
    }
}

From source file:Main.java

public static String[] getMountedVolumes() {
    final String state = Environment.getExternalStorageState();
    if (Environment.MEDIA_MOUNTED.equals(state) || Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) { // we can read the External Storage...           
        //Retrieve the primary External Storage:
        final File primaryExternalStorage = Environment.getExternalStorageDirectory();

        //Retrieve the External Storages root directory:
        String externalStorageRootDir = null;
        if ((externalStorageRootDir = primaryExternalStorage.getParent()) == null) { // no parent...
            return (new String[] { "ONLY A SINGLE VOLUME HAS BEEN DETECTED!",
                    (Environment.isExternalStorageRemovable() ? "(REMOVABLE SD-CARD)" : "(INTERNAL STORAGE)")
                            + " PRIMARY STORAGE: " + primaryExternalStorage });
        } else {//from   w  w  w . j  a v a 2s  .  c o  m
            final File externalStorageRoot = new File(externalStorageRootDir);
            final File[] files = externalStorageRoot.listFiles(new FilenameFilter() {

                @Override
                public boolean accept(File dir, String filename) {
                    // TODO Auto-generated method stub

                    File file = new File(dir, filename);
                    if (file.isDirectory() && file.canRead() && file.canWrite() && !file.isHidden()) {
                        return true;
                    }
                    return false;
                }

            }); //.listFiles();
            List<String> data = new ArrayList<String>();
            if (files.length > 1) {
                data.add("MULTIPLE VOLUMES HAS BEEN DETECTED!");
                data.add("Enumerating detected volumes . . .");
            } else {
                data.add("ONLY A SINGLE VOLUME HAS BEEN DETECTED!");
            }

            for (final File file : files) {
                if (file.isDirectory() && file.canRead() && file.canWrite() && !file.isHidden()
                        && (files.length > 0)) { // it is a real directory (not a USB drive)...
                    if (file.toString().equals(primaryExternalStorage.toString()))
                        data.add((Environment.isExternalStorageRemovable() ? "(REMOVABLE SD-CARD)"
                                : "(INTERNAL Memory)") + " PRIMARY STORAGE: " + file.getAbsolutePath());
                    else {
                        data.add(((file.toString().contains("usb") || file.toString().contains("USB"))
                                ? "MOUNTED USB"
                                : "MOUNTED") + " STORAGE: " + file.getAbsolutePath());
                    }
                }
            }
            return data.toArray(new String[data.size()]);
        }
    } else {
        // we cannot read the External Storage..., return null
        return null;
    }

}

From source file:Main.java

public static void backupDb(File currentDB) {
    try {/*  w  w  w  .j ava 2s  . c  o m*/

        File sd = Environment.getExternalStorageDirectory();
        File data = Environment.getDataDirectory();

        if (sd.canWrite()) {
            File f = new File(sd.getAbsolutePath() + "/AccountManagement");
            if (!f.exists()) {
                f.mkdir();
            }
            Date currentDate = new Date();
            String currentDateStr = newFormat1.format(currentDate);

            currentDateStr = currentDateStr.trim();
            System.out.println(
                    "---------------------------------------------------------------" + currentDateStr);
            String backupDBPath = "AccountManagement/backup_" + currentDateStr + ".db";
            File backupDB = new File(sd, backupDBPath);

            if (currentDB.exists()) {
                FileChannel src = new FileInputStream(currentDB).getChannel();
                FileChannel dst = new FileOutputStream(backupDB).getChannel();
                dst.transferFrom(src, 0, src.size());
                src.close();
                dst.close();
            }
        }
    } catch (Exception e) {
        System.out.println(e);
    }
}

From source file:Main.java

/**
 * Create a backup of the database./*from  w  ww. j  a  v a 2s .  com*/
 *
 * @param context who calls the function.
 * @param dbName  Name of the database to backup. (If empty, radiomap.db is used).
 */
public static void exportDb(Context context, String dbName) {
    try {
        if (dbName.equals(""))
            dbName = "radiomap.db";

        File sd = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
        File data = Environment.getDataDirectory();

        if (sd.canWrite()) {
            String currentDBPath = "//data//" + context.getApplicationContext().getPackageName()
                    + "//databases//" + dbName;
            File currentDB = new File(data, currentDBPath);
            File backupDB = new File(sd, dbName);

            FileChannel src = new FileInputStream(currentDB).getChannel();
            FileChannel dst = new FileOutputStream(backupDB).getChannel();
            dst.transferFrom(src, 0, src.size());
            src.close();
            dst.close();

            Toast.makeText(context, "Datenbank nach Downloads exportiert!", Toast.LENGTH_LONG).show();
        }
    } catch (Exception e) {
        Log.e("Utils", e.getMessage());
        Toast.makeText(context, "Exportieren fehlgeschlagen!", Toast.LENGTH_LONG).show();
    }
}

From source file:Main.java

/**
 * Create a backup of the database.//ww  w  .  jav a 2 s  . com
 *
 * @param context who calls the function.
 * @param dbName  Name of the database to backup. (If empty, radiomap.db is used).
 */
public static void importDb(Context context, String dbName) {
    try {
        if (dbName.equals(""))
            dbName = "radiomap.db";

        File sd = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
        File data = Environment.getDataDirectory();

        if (sd.canWrite()) {
            String currentDBPath = "//data//" + context.getApplicationContext().getPackageName()
                    + "//databases//" + dbName;
            File backupDB = new File(data, currentDBPath);
            File currentDB = new File(sd, dbName);

            FileChannel src = new FileInputStream(currentDB).getChannel();
            FileChannel dst = new FileOutputStream(backupDB).getChannel();
            dst.transferFrom(src, 0, src.size());
            src.close();
            dst.close();

            Toast.makeText(context, "Datenbank von Downloads importiert!", Toast.LENGTH_LONG).show();
        }
    } catch (Exception e) {
        Log.e("Utils", e.getMessage());
        Toast.makeText(context, "Import fehlgeschlagen!", Toast.LENGTH_LONG).show();
    }
}

From source file:edu.kit.dama.staging.util.StagingUtils.java

/**
 * Checking directory for beeing a directory, readable and writable.
 *
 * @param pDir The directory to check./*from www . j a v  a2  s  .co  m*/
 *
 * @return TRUE if pDir is a directory, readable and writable.
 */
private static boolean isAccessibleDirectory(File pDir) {
    return pDir != null && pDir.isDirectory() && pDir.canRead() && pDir.canWrite();
}

From source file:com.frostwire.android.gui.util.FileUtils.java

public static void deleteFolderRecursively(File folder) {
    if (folder != null && folder.isDirectory() && folder.canWrite()) {
        //delete your contents and recursively delete sub-folders
        File[] listFiles = folder.listFiles();

        if (listFiles != null) {
            for (File f : listFiles) {
                if (f.isFile()) {
                    f.delete();/* w  w w . ja v a  2 s  .c om*/
                } else if (f.isDirectory()) {
                    deleteFolderRecursively(f);
                }
            }
            folder.delete();
        }
    }
}

From source file:Main.java

/** 
 * Write the <code>Document</code> in memory out to a standard XML file.
 * @param  doc  the <code>Document</code> object for the XML file to list
 * @param  strFilePath  the XML file to write to
 * @throws custom custom exception if file is not writeable
 * @throws ioe IOException if an error occurs on file creation
 * @throws fnfe FileNotFoundException if PrintWriter constructor fails
 *///from   w w  w.  j av a 2s  .co m
public static void WriteXMLFile2(Document doc, String strFilePath) throws Exception {
    // open file for writing
    File file = new File(strFilePath);

    // ensure that file is writeable
    try {
        file.createNewFile();
        if (!file.canWrite()) {
            throw new Exception("FileNotWriteable");
        }
    } catch (IOException ioe) {
        throw ioe;
    }

    // create a PrintWriter to write the XML file to
    PrintWriter pw = null;
    try {
        pw = new PrintWriter(file);
    } catch (FileNotFoundException fnfe) {
        throw fnfe;
    }

    // write out the serialized form
    pw.print(getXMLListing(doc));
    pw.close();
}

From source file:net.idlesoft.android.apps.github.utils.GravatarCache.java

private static File ensure_directory(final String path) throws IOException {
    File root = Environment.getExternalStorageDirectory();
    if (!root.canWrite()) {
        throw new IOException("External storage directory is not writable");
    }/*from   w  ww .j  a  v  a2s  .  co  m*/
    final String[] parts = path.split("/");
    for (final String part : parts) {
        final File f = new File(root, part);
        if (!f.exists()) {
            final boolean created = f.mkdir();
            if (!created) {
                throw new IOException("Unable to create directory " + part);
            }
        } else {
            if (!f.isDirectory()) {
                throw new IOException("Unable to create directory " + part);
            }
        }
        root = f;
    }
    return root;
}