Example usage for java.io File setReadOnly

List of usage examples for java.io File setReadOnly

Introduction

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

Prototype

public boolean setReadOnly() 

Source Link

Document

Marks the file or directory named by this abstract pathname so that only read operations are allowed.

Usage

From source file:MainClass.java

public static void main(String[] a) {
    File file = new File("c:\\text.txt");
    file.setReadOnly();
    System.out.println(file.canRead());
}

From source file:MainClass.java

public static void main(String[] args) throws Exception {

    File f = new File("f");

    if (!f.setReadOnly()) {
        System.out.println("Grrr! Can't set file read-only.");
        return;//from w w w . j a  v a  2  s .c o m
    }

}

From source file:Main.java

public static void main(String[] args) {
    File file = new File("C:/demo.txt");
    System.out.println(file.setReadOnly());
    System.out.println(file.canWrite());
}

From source file:Main.java

public static void main(String[] args) {
    File file = new File("C:/File/demo.txt");

    System.out.println(file.setReadOnly());
    System.out.println(file.canWrite());
}

From source file:Main.java

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

    if (file.canWrite()) {
        System.out.println("writable!");
    } else {
        System.out.println("read only!");
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    File file = new File("Writable.txt");
    file.createNewFile();/*from ww w. j  a v  a  2 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:Main.java

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

    // set file as read only
    boolean bool = f.setReadOnly();

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

    // checks whether the file is writable
    bool = f.canWrite();//from   w  ww  . jav a 2  s  .  c o  m

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

}

From source file:ReadOnly.java

public static void main(String[] a) throws IOException {

    File f = new File("f");

    if (!f.createNewFile()) {
        System.out.println("Can't create new file.");
        return;// w w w . j av  a 2 s. c  o m
    }

    if (!f.canWrite()) {
        System.out.println("Can't write new file!");
        return;
    }

    if (!f.setReadOnly()) {
        System.out.println("Grrr! Can't set file read-only.");
        return;
    }

    if (f.canWrite()) {
        System.out.println("Most immutable, captain!");
        System.out.println("But it still says canWrite() after setReadOnly");
        return;
    } else {
        System.out.println("Logical, captain!");
        System.out.println("canWrite() correctly returns false after setReadOnly");
    }
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    File f = new File("name.txt");

    if (!f.exists()) {
        System.out.println("File not found.");
        return;/*  w w  w.j a v a2 s.  com*/
    }
    if (f.canRead())
        System.out.println("  Readable");
    else
        System.out.println("  Not Readable");

    if (f.canWrite())
        System.out.println("  Writable");
    else
        System.out.println("  Not Writable");
    System.out.println("  Last modified on " + new Date(f.lastModified()));

    long t = Calendar.getInstance().getTimeInMillis();
    if (!f.setLastModified(t))
        System.out.println("Can't set time.");

    if (!f.setReadOnly())
        System.out.println("Can't set to read-only.");

    if (f.canRead())
        System.out.println("  Readable");
    else
        System.out.println("  Not Readable");

    if (f.canWrite())
        System.out.println("  Writable");
    else
        System.out.println("  Not Writable");
    System.out.println("  Last modified on " + new Date(f.lastModified()));

    if (!f.setWritable(true, false))
        System.out.println("Can't return to read/write.");

    if (f.canRead())
        System.out.println("  Readable");
    else
        System.out.println("  Not Readable");

    if (f.canWrite())
        System.out.println("  Writable");
    else
        System.out.println("  Not Writable");
}

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

/**
 * <pre>//w w w.  j a  v a  2  s .  c  o  m
 * 
 * </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();
}