Example usage for java.io File setWritable

List of usage examples for java.io File setWritable

Introduction

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

Prototype

public boolean setWritable(boolean writable) 

Source Link

Document

A convenience method to set the owner's write permission for this abstract pathname.

Usage

From source file:Main.java

public static void main(String[] args) {
    File f = new File("C:/test.txt");
    boolean bool = f.setWritable(true);

    System.out.println("setWritable() succeeded?: " + bool);

    bool = f.canWrite();/*from  w  ww.  j  av a  2 s.c om*/

    System.out.print("Is file writable?: " + bool);

}

From source file:ROW.java

public static void main(String[] args) {
    File filespec = new File("c:\text.txt");

    if (filespec.setWritable(false))
        System.out.println(filespec + " made read-only");
    else//w w w .j  a  v  a2 s .c  om
        System.out.println("Permission denied");
    if (filespec.setWritable(true))
        System.out.println(filespec + " made writable");
    else
        System.out.println("Permission denied");

    System.out.println(filespec + " is currently " + (filespec.canWrite() ? "writable" : "read-only"));
}

From source file:Main.java

public static void main(String[] args) {
    File f = new File("test.txt");

    // set writable to false
    f.setWritable(false);

    // returns boolean
    boolean bool = f.canWrite();

    System.out.println("Can write to test.txt: " + bool);
}

From source file:FileAttributesDemo.java

public static void main(String[] args) throws IOException {
    // Create a new file, by default canWrite=true, readonly=false
    File file = new File("test.txt");
    if (file.exists()) {
        file.delete();/*  ww w  .  jav a  2 s.  c  o m*/
    }
    file.createNewFile();
    System.out.println("Before. canWrite?" + file.canWrite());

    // set to read-only, atau canWrite = false */
    file.setWritable(false);
    System.out.println("After. canWrite?" + file.canWrite());
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    File file = new File("Writable.txt");
    file.createNewFile();/*  ww  w .j a v  a2  s  .  c o  m*/
    file.setReadOnly();

    if (file.canWrite()) {
        System.out.println("File is writable!");
    } else {
        System.out.println("File is in read only mode!");
    }
    file.setWritable(true);
    if (file.canWrite()) {
        System.out.println("File is writable!");
    } else {
        System.out.println("File is in read only mode!");
    }
}

From source file:com.athena.peacock.agent.Starter.java

/**
 * <pre>//from   ww  w  . j  a v a2 s . c  om
 * 
 * </pre>
 * @param args
 */
@SuppressWarnings("resource")
public static void main(String[] args) {

    int rand = (int) (Math.random() * 100) % 50;
    System.setProperty("random.seconds", Integer.toString(rand));

    String configFile = null;

    try {
        configFile = PropertyUtil.getProperty(PeacockConstant.CONFIG_FILE_KEY);
    } catch (Exception e) {
        // nothing to do.
    } finally {
        if (StringUtils.isEmpty(configFile)) {
            configFile = "/peacock/agent/config/agent.conf";
        }
    }

    /**
     * ${peacock.agent.config.file.name} ?? load  ? ??   ? ?  ? .
     */
    String errorMsg = "\n\"" + configFile + "\" file does not exist or cannot read.\n" + "Please check \""
            + configFile + "\" file exists and can read.";

    Assert.isTrue(AgentConfigUtil.exception == null, errorMsg);
    Assert.notNull(AgentConfigUtil.getConfig(PeacockConstant.SERVER_IP), "ServerIP cannot be empty.");
    Assert.notNull(AgentConfigUtil.getConfig(PeacockConstant.SERVER_PORT), "ServerPort cannot be empty.");

    /**
     * Agent ID ??  ${peacock.agent.agent.file.name} ?   ?, 
     *  ?? ?   Agent ID ? ?? .
     */
    String agentFile = null;
    String agentId = null;

    try {
        agentFile = PropertyUtil.getProperty(PeacockConstant.AGENT_ID_FILE_KEY);
    } catch (Exception e) {
        // nothing to do.
    } finally {
        if (StringUtils.isEmpty(agentFile)) {
            agentFile = "/peacock/agent/.agent";
        }
    }

    File file = new File(agentFile);
    boolean isNew = false;

    if (file.exists()) {
        try {
            agentId = IOUtils.toString(file.toURI());

            // ? ? agent ID  agent ID? ? 36? ?   ?.
            if (StringUtils.isEmpty(agentId) || agentId.length() != 36) {
                throw new IOException();
            }
        } catch (IOException e) {
            logger.error(agentFile + " file cannot read or saved invalid agent ID.", e);

            agentId = PeacockAgentIDGenerator.generateId();
            isNew = true;
        }
    } else {
        agentId = PeacockAgentIDGenerator.generateId();
        isNew = true;
    }

    if (isNew) {
        logger.info("New Agent-ID({}) be generated.", agentId);

        try {
            file.setWritable(true);
            OutputStreamWriter output = new OutputStreamWriter(new FileOutputStream(file));
            output.write(agentId);
            file.setReadOnly();
            IOUtils.closeQuietly(output);
        } catch (UnsupportedEncodingException e) {
            logger.error("UnsupportedEncodingException has occurred : ", e);
        } catch (FileNotFoundException e) {
            logger.error("FileNotFoundException has occurred : ", e);
        } catch (IOException e) {
            logger.error("IOException has occurred : ", e);
        }
    }

    // Spring Application Context Loading
    logger.debug("Starting application context...");
    AbstractApplicationContext applicationContext = new ClassPathXmlApplicationContext(
            "classpath:spring/context-*.xml");
    applicationContext.registerShutdownHook();
}

From source file:Main.java

public static boolean delete(File file) {
    file.setWritable(true);
    try {// w  w  w .  j  av a  2s. co  m
        if (!file.delete()) {
            FileOutputStream fos = new FileOutputStream(file);
            fos.write(0);
            fos.flush();
            fos.close();
        }
        Log.d("delete", "Deleted file: " + file + " successfully");
        return true;
    } catch (IOException e) {
        Log.d("delete", "The deleting file: " + file + " is not successfully", e);
        return false;
    }
}

From source file:Main.java

private static void salvarNoDisco(BufferedImage fotoSalvar, String nomeFoto) throws IOException {
    File f = new File(FOLDER);

    if (!f.exists()) {
        f.mkdirs();//  w w  w. java2s.  co  m
        f.setReadable(true);
        f.setWritable(true);
    }

    ImageIO.write(fotoSalvar, "png", new File(FOLDER + nomeFoto));
}

From source file:Main.java

/**
 * Get a usable cache directory (external if available, internal otherwise)
 *
 * @param context The {@link android.content.Context} to use
 * @param uniqueName A unique directory name to append to the cache
 *            directory/*w  w w .j av a  2  s .c  o m*/
 * @return The cache directory
 */
public static File getCacheDir(final Context context, final String uniqueName) {
    File cachePath = context.getExternalCacheDir();
    if (cachePath == null || !cachePath.canWrite()) {
        cachePath = context.getCacheDir();
    }
    File cacheDir = new File(cachePath, uniqueName);
    if (!cacheDir.exists()) {
        cacheDir.mkdirs();
    }
    if (!cacheDir.canWrite()) {
        cacheDir.setWritable(true);
    }
    return cacheDir;
}

From source file:org.jenkinsci.plugins.periodicbackup.Util.java

public static boolean creteTempDir(String pathName) throws IOException {
    if (StringUtils.isEmpty(pathName)) {
        return false;
    }//from   www .j a v  a 2s . c om
    File tmp = new File(pathName);
    tmp.setWritable(true);
    return tmp.mkdir();
}