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

public static void clearProjectData() {
    File pdf = new File(System.getProperty("msbSummary") + "." + System.getProperty("user.name"));

    if (pdf.exists() && pdf.canWrite()) {
        pdf.delete();/*from www. j  a v  a  2 s  . co m*/
    }
}

From source file:Main.java

public static String extSD() {
    String externalsd = "";

    if (!TextUtils.isEmpty(System.getenv("SECONDARY_STORAGE"))) {
        final String externalstorage[] = System.getenv("SECONDARY_STORAGE").split(":");
        for (final String dirs : externalstorage) {
            final File dir = new File(dirs);
            if (dir.exists() && dir.isDirectory() && dir.canRead() && dir.canWrite()) {
                externalsd = dirs;/*from w w w . j av  a  2 s .  c  om*/
                break;
            }
        }
    } else {
        final String supported[] = { "/mnt/extSdCard", "/storage/sdcard1", "/mnt/external_sd" };
        for (final String dirs : supported) {
            final File dir = new File(dirs);
            if (dir.exists() && dir.isDirectory() && dir.canRead() && dir.canWrite()) {
                externalsd = dirs;
                break;
            }
        }
    }
    return externalsd;
}

From source file:Main.java

public static boolean importDatabase(String dbName) {

    try {/* ww  w  .j  a  v  a 2 s. co  m*/
        File sd = Environment.getExternalStorageDirectory();
        File data = Environment.getDataDirectory();

        if (sd.canWrite()) {
            String currentDBPath = "//data//" + PACKAGENAME + "//databases//" + dbName;
            String backupDBPath = "/AttendanceManager/" + dbName;
            File backupDB = new File(data, currentDBPath);
            File currentDB = new File(sd, backupDBPath);

            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) {
        return false;
    }
    return true;
}

From source file:edu.kit.cockpit.valuationserver.sfmpersistency.FileUtil.java

/**
 * checks file for writing//from   ww w  . j  a v a2 s  .com
 * 
 * @param modelFile
 * @param modelFileName
 */
public static void checkFileForWriting(File file) throws IOException {
    checkFileForReading(file);
    if (!file.canWrite()) {
        String error = "File '" + file.getAbsolutePath() + "' is write protected";
        log.error(error);
        throw new IOException(error);
    }
}

From source file:eu.freme.eservices.epublishing.EPublishingService.java

private static boolean testTmp(final File tmpFolder) {
    return (tmpFolder.exists() && tmpFolder.isDirectory() && tmpFolder.canWrite() && tmpFolder.canExecute());
}

From source file:Main.java

/**
 * Helper method to retrieve the application internal storage cache
 * directory and make sure it exists.//from   w  w w.j  av  a  2s .co m
 */
@CheckForNull
static File getInternalAppCacheDir(@Nonnull Context context) {
    File intCacheDir = context.getCacheDir();
    if (intCacheDir != null && !intCacheDir.exists()) {
        if (!intCacheDir.mkdirs() || !intCacheDir.canWrite()) {
            intCacheDir = null;
        }
    }
    return intCacheDir;
}

From source file:Main.java

public static void saveDBInSdcard(Context pContext, String pDatabaseName) {
    try {//from ww w .  j a  v  a 2 s .c  om
        File sd = Environment.getExternalStorageDirectory();
        File data = Environment.getDataDirectory();

        if (sd.canWrite()) {
            String currentDBPath = "//data//" + pContext.getPackageName() + "//databases//" + pDatabaseName;
            String backupDBPath = pDatabaseName;
            File currentDB = new File(data, currentDBPath);
            File backupDB = new File(sd, backupDBPath);

            FileChannel src = new FileInputStream(currentDB).getChannel();
            FileChannel dst = new FileOutputStream(backupDB).getChannel();
            dst.transferFrom(src, 0, src.size());
            src.close();
            dst.close();
            Toast.makeText(pContext, backupDB.toString(), Toast.LENGTH_LONG).show();
        }
    } catch (Exception e) {
        Toast.makeText(pContext, e.toString(), Toast.LENGTH_LONG).show();
    }
}

From source file:io.stallion.services.PermaCache.java

public static String initCacheFolder() {
    File tmp = new File("/tmp/stallion-cache");
    if (tmp.isDirectory() && tmp.canWrite()) {
        return "/tmp/stallion-cache";
    }//from   www .  j  a v  a2 s.  c o m
    Boolean made = tmp.mkdirs();
    if (made) {
        return "/tmp/stallion-cache";
    }
    String appCachePath = Context.settings().getDataDirectory() + "/_cache";
    tmp = new File(appCachePath);
    if (tmp.isDirectory() && tmp.canWrite()) {
        return appCachePath;
    }
    made = tmp.mkdirs();
    if (made) {
        return appCachePath;
    }
    Log.warn("Could not create cache folder /tmp/stallion-cache or {0}!", appCachePath);
    return "";
}

From source file:Main.java

public static File[] getMountedVolumesAsFile() {
    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 File[] { primaryExternalStorage });
        } else {/* w w  w.  j a  va  2  s. co  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<File> data = new ArrayList<File>();

            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(file);
                    else {
                        if (!file.toString().contains("usb") || !file.toString().contains("USB")) {
                            data.add(file);
                        }
                    }
                }
            }
            return data.toArray(new File[data.size()]);
        }
    } else {
        // we cannot read the External Storage..., return null
        return null;
    }
}

From source file:Main.java

public static Bitmap saveBitmapToFile(Activity activity, Bitmap bitmap) {
    String mPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString();
    File imageFile = new File(mPath);
    boolean create = imageFile.mkdirs();
    boolean canWrite = imageFile.canWrite();
    Calendar cal = Calendar.getInstance();
    String date = cal.get(Calendar.YEAR) + "-" + cal.get(Calendar.MONTH) + "-" + cal.get(Calendar.DATE);

    String filename = null;//from   w  w w.  j  av  a 2s .  c o m
    int i = 0;
    while (imageFile.exists()) {
        i++;
        filename = date + "_mandelbrot" + i + ".png";
        imageFile = new File(mPath, filename);
        boolean canWrite2 = imageFile.canWrite();

    }

    try {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        /*resultB*/bitmap.compress(CompressFormat.PNG, 90, bos);
        byte[] bitmapdata = bos.toByteArray();

        //write the bytes in file
        FileOutputStream fos = new FileOutputStream(imageFile);
        fos.write(bitmapdata);
        fos.flush();
        fos.close();

        Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
        intent.setData(Uri.fromFile(imageFile));
        activity.sendBroadcast(intent);

        displaySuccesToast(activity);
    } catch (FileNotFoundException e) {
        displayFileError(activity);
        e.printStackTrace();
    } catch (IOException e) {
        displayFileError(activity);
        e.printStackTrace();
    }
    return bitmap;
}