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:hivemall.mf.OnlineMatrixFactorizationUDTF.java

@Override
public StructObjectInspector initialize(ObjectInspector[] argOIs) throws UDFArgumentException {
    if (argOIs.length < 3) {
        throw new UDFArgumentException(
                "_FUNC_ takes 3 arguments: INT user, INT item, FLOAT rating [, CONSTANT STRING options]");
    }//from   w ww.  ja  v  a 2  s  . c o  m
    this.userOI = HiveUtils.asIntCompatibleOI(argOIs[0]);
    this.itemOI = HiveUtils.asIntCompatibleOI(argOIs[1]);
    this.ratingOI = HiveUtils.asDoubleCompatibleOI(argOIs[2]);

    processOptions(argOIs);

    this.model = new FactorizedModel(this, factor, meanRating, rankInit);
    this.count = 0L;
    this.lastWritePos = 0L;
    this.userProbe = new float[factor];
    this.itemProbe = new float[factor];

    if (mapredContext != null && iterations > 1) {
        // invoke only at task node (initialize is also invoked in compilation)
        final File file;
        try {
            file = File.createTempFile("hivemall_mf", ".sgmt");
            file.deleteOnExit();
            if (!file.canWrite()) {
                throw new UDFArgumentException("Cannot write a temporary file: " + file.getAbsolutePath());
            }
        } catch (IOException ioe) {
            throw new UDFArgumentException(ioe);
        } catch (Throwable e) {
            throw new UDFArgumentException(e);
        }
        this.fileIO = new NioFixedSegment(file, RECORD_BYTES, false);
        this.inputBuf = ByteBuffer.allocateDirect(65536); // 64 KiB
    }

    ArrayList<String> fieldNames = new ArrayList<String>();
    ArrayList<ObjectInspector> fieldOIs = new ArrayList<ObjectInspector>();
    fieldNames.add("idx");
    fieldOIs.add(PrimitiveObjectInspectorFactory.writableIntObjectInspector);
    fieldNames.add("Pu");
    fieldOIs.add(ObjectInspectorFactory
            .getStandardListObjectInspector(PrimitiveObjectInspectorFactory.writableFloatObjectInspector));
    fieldNames.add("Qi");
    fieldOIs.add(ObjectInspectorFactory
            .getStandardListObjectInspector(PrimitiveObjectInspectorFactory.writableFloatObjectInspector));
    if (useBiasClause) {
        fieldNames.add("Bu");
        fieldOIs.add(PrimitiveObjectInspectorFactory.writableFloatObjectInspector);
        fieldNames.add("Bi");
        fieldOIs.add(PrimitiveObjectInspectorFactory.writableFloatObjectInspector);
        if (updateMeanRating) {
            fieldNames.add("mu");
            fieldOIs.add(PrimitiveObjectInspectorFactory.writableFloatObjectInspector);
        }
    }
    return ObjectInspectorFactory.getStandardStructObjectInspector(fieldNames, fieldOIs);
}

From source file:com.emarsys.dyson.storage.DefaultDysonStorage.java

/**
 * Creates the directory with the passed filename if it's not
 * already present./*from ww  w  . j  a va 2  s.  com*/
 * 
 * @param pathToDir - the path to the directory
 * @throws DysonException - if it was not possible to create a
 *       writable directory with the passed path.
 */
protected void createDirsIfNotPresent(String pathToDir) throws DysonException {
    File dir = new File(pathToDir);
    if (!dir.exists()) {
        log.debug("creating dir(s) \'{}\'", pathToDir);
        dir.mkdirs();
        dir.setReadable(true);
        dir.setWritable(true);
    }

    boolean isWritableDirectoryPresent = dir.exists() && dir.isDirectory() && dir.canRead() && dir.canWrite();

    if (!isWritableDirectoryPresent) {
        throw new DysonException("Was not able to create directory \'" + pathToDir + "\'");
    }
}

From source file:com.wirelessmoves.cl.MainActivity.java

private void saveDataToFile(String LocalFileWriteBufferStr, String id) {
    /* write measurement data to the output file */
    try {/*from w w  w.ja va  2  s  . c  om*/
        File root = Environment.getExternalStorageDirectory();
        if (root.canWrite()) {
            File logfile = new File(root, filename);
            FileWriter logwriter = new FileWriter(logfile, true); /* true = append */
            BufferedWriter out = new BufferedWriter(logwriter);

            /* first, save debug info if activated */
            if (outputDebugInfo == true)
                out.write(id);

            /* now save the data buffer into the file */
            out.write(LocalFileWriteBufferStr);
            out.close();
        }
    } catch (IOException e) {
        /* don't do anything for the moment */
    }

}

From source file:cn.edu.sdust.silence.itransfer.filemanage.FileManager.java

/**
 * The full path name of the file to delete.
 * //from  w  w w  . ja  va2  s  .  com
 * @param path name
 * @return
 */
public int deleteTarget(String path) {
    File target = new File(path);

    if (target.exists() && target.isFile() && target.canWrite()) {
        target.delete();
        return 0;
    }

    else if (target.exists() && target.isDirectory() && target.canRead()) {
        String[] file_list = target.list();

        if (file_list != null && file_list.length == 0) {
            target.delete();
            return 0;

        } else if (file_list != null && file_list.length > 0) {

            for (int i = 0; i < file_list.length; i++) {
                File temp_f = new File(target.getAbsolutePath() + "/" + file_list[i]);

                if (temp_f.isDirectory())
                    deleteTarget(temp_f.getAbsolutePath());
                else if (temp_f.isFile())
                    temp_f.delete();
            }
        }
        if (target.exists())
            if (target.delete())
                return 0;
    }
    return -1;
}

From source file:cn.edu.sdust.silence.itransfer.ui.file.FileManager.java

/**
 * The full path name of the file to delete.
 *
 * @param path name//w ww. j  ava 2  s  . c om
 * @return
 */
public int deleteTarget(String path) {
    File target = new File(path);

    if (target.exists() && target.isFile() && target.canWrite()) {
        target.delete();
        return 0;
    } else if (target.exists() && target.isDirectory() && target.canRead()) {
        String[] file_list = target.list();

        if (file_list != null && file_list.length == 0) {
            target.delete();
            return 0;

        } else if (file_list != null && file_list.length > 0) {

            for (int i = 0; i < file_list.length; i++) {
                File temp_f = new File(target.getAbsolutePath() + "/" + file_list[i]);

                if (temp_f.isDirectory())
                    deleteTarget(temp_f.getAbsolutePath());
                else if (temp_f.isFile())
                    temp_f.delete();
            }
        }
        if (target.exists())
            if (target.delete())
                return 0;
    }
    return -1;
}

From source file:com.all.client.data.JAudioTaggerStrategy.java

@Override
public Track createTrack(TrackFile trackFile) throws InvalidFileException {
    LocalTrack track = new LocalTrack(trackFile.getFileName(), trackFile.getHashcode());
    File file = trackFile.getFile();
    track.setFileFormat(file.getName().substring(file.getName().lastIndexOf('.') + 1, file.getName().length())
            .toUpperCase());//  w w  w  .  j  ava  2s  .  co  m
    track.setSize(file.length());
    try {
        if (!file.canWrite()) {
            file.setWritable(true);
        }
        if (file.exists()) {
            audioFile = AudioFileIO.read(file);
        }

        metadataReader = new MetadataReader(audioFile);
        if (audioFile instanceof MP3File) {
            metadataReader = new Mp3MetadataReader(audioFile);
        }
        Tag tag = metadataReader.getTag();

        readAudioHeader(track);

        readTag(track, file, tag);

        // TODO Model a Business Exception to propagate
        // low level exceptions to show an appropriate
        // message to the user

    } catch (Exception e) {
        log.error(e.getMessage());
    }
    track.setFileName(trackFile.getFile().getName());
    return track;
}

From source file:com.money.manager.ex.sync.SyncManager.java

private File getExternalStorageDirectoryForSync() {
    // todo check this after refactoring the database utils.
    MmxDatabaseUtils dbUtils = new MmxDatabaseUtils(getContext());
    File folder = new File(dbUtils.getDefaultDatabaseDirectory());

    // manage folder
    if (folder.exists() && folder.isDirectory() && folder.canWrite()) {
        // create a folder for remote files
        File folderSync = new File(folder + "/sync");
        // check if folder exists otherwise create
        if (!folderSync.exists()) {
            if (!folderSync.mkdirs())
                return getContext().getFilesDir();
        }//from w ww . j  a  va  2  s  .c o m
        return folderSync;
    } else {
        return mContext.getFilesDir();
    }
}

From source file:io.manasobi.utils.FileUtils.java

/**
* ? ?? BufferedWriter . append true?   ??  ? ??.
* 
* @param file ? ?/*from   w  w w .  j  av  a 2s  .  c  o m*/
* @param charSet ??  ?
* @param append  ?? ? ???  
* @return ? ??  FileWriter
*/
public static BufferedWriter openBufferWriter(File file, String charSet, boolean append) {

    if (file.exists()) {

        if (file.isDirectory()) {
            return null;
        }

        if (!file.canWrite()) {
            return null;
        }

    } else {

        File parent = file.getParentFile();

        if (parent != null) {

            if (!parent.mkdirs() && !parent.isDirectory()) {
                return null;
            }
        }
    }

    BufferedWriter bufferedWriter = null;

    try {
        bufferedWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), charSet));
    } catch (Exception e) {
        e.printStackTrace();
    }

    return bufferedWriter;
}

From source file:com.akop.bach.ImageCache.java

public void clearCachedBitmap(String imageUrl, CachePolicy cachePol) {
    File file = getCacheFile(imageUrl, cachePol);

    if (file.canWrite()) {
        if (App.getConfig().logToConsole())
            App.logv("Purging %s from cache", imageUrl);

        file.delete();/*from   www  .  j  av a 2 s.  c  o  m*/
    }
}