Example usage for java.io File canRead

List of usage examples for java.io File canRead

Introduction

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

Prototype

public boolean canRead() 

Source Link

Document

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

Usage

From source file:se.inera.axel.shs.broker.messagestore.internal.FileMessageStoreServiceIT.java

@Test(groups = "largeTests", enabled = true)
@DirtiesContext/*from w  w  w  .  j a  v a  2s .c o m*/
public void saveMessage() {
    final ShsMessageEntry entry = make(a(ShsMessageEntryMaker.ShsMessageEntry));
    final se.inera.axel.shs.mime.ShsMessage shsMessage = make(a(ShsMessage));

    messageStore.save(entry, shsMessage);

    File f = new File(baseDir, entry.getId());
    assertTrue(f.exists());
    assertTrue(f.canRead());
}

From source file:MainClass.java

public void setFileStats(File dir) {
    String files[] = dir.list();/*ww w. j  a  va2 s . c  om*/
    data = new Object[files.length][titles.length];

    for (int i = 0; i < files.length; i++) {
        File tmp = new File(files[i]);
        data[i][0] = new Boolean(tmp.isDirectory());
        data[i][1] = tmp.getName();
        data[i][2] = new Boolean(tmp.canRead());
        data[i][3] = new Boolean(tmp.canWrite());
        data[i][4] = new Long(tmp.length());
        data[i][5] = new Date(tmp.lastModified());
    }

    fireTableDataChanged();
}

From source file:com.erudika.para.storage.LocalFileStore.java

@Override
public InputStream load(String path) {
    if (StringUtils.startsWith(path, File.separator)) {
        path = path.substring(1);/*from   ww w  . j ava 2  s.c om*/
    }
    if (!StringUtils.isBlank(path)) {
        try {
            File f = new File(folder + File.separator + path);
            return f.canRead() ? new BufferedInputStream(new FileInputStream(f)) : null;
        } catch (FileNotFoundException ex) {
            logger.error(null, ex);
        }
    }
    return null;
}

From source file:admincommands.SendRawPacket.java

@Override
public void executeCommand(Player admin, String[] params) {
    if (admin.getAccessLevel() < AdminConfig.COMMAND_SENDRAWPACKET) {
        PacketSendUtility.sendMessage(admin, "You dont have enough rights to execute this command");
        return;/*from  w  w w .j  a va  2 s . com*/
    }

    if (params.length != 1) {
        PacketSendUtility.sendMessage(admin, "Usage: //raw [name]");
        return;
    }

    File file = new File(ROOT, params[0] + ".txt");

    if (!file.exists() || !file.canRead()) {
        PacketSendUtility.sendMessage(admin, "Wrong file selected.");
        return;
    }

    try {
        @SuppressWarnings({ "unchecked" })
        List<String> lines = FileUtils.readLines(file);

        SM_CUSTOM_PACKET packet = null;

        for (String row : lines) {
            String[] tokens = row.substring(0, 48).trim().split(" ");
            int len = tokens.length;

            for (int i = 0; i < len; i++) {
                if (i == 0) {
                    packet = new SM_CUSTOM_PACKET(Integer.valueOf(tokens[i], 16));
                } else if (i > 2) {
                    packet.addElement(PacketElementType.C, "0x" + tokens[i]);
                }
            }
        }

        if (packet != null)
            PacketSendUtility.sendPacket(admin, packet);
    } catch (IOException e) {
        PacketSendUtility.sendMessage(admin, "An error has occurred.");
        logger.warn("IO Error.", e);
    }
}

From source file:admincommands.Raw.java

@Override
public void execute(Player admin, String... params) {
    if (params.length != 1) {
        PacketSendUtility.sendMessage(admin, "Usage: //raw [name]");
        return;/*from w  w  w .  j  a v  a  2s  .  c  o  m*/
    }

    File file = new File(ROOT, params[0] + ".txt");

    if (!file.exists() || !file.canRead()) {
        PacketSendUtility.sendMessage(admin, "Wrong file selected.");
        return;
    }

    try {
        List<String> lines = FileUtils.readLines(file);

        SM_CUSTOM_PACKET packet = null;
        PacketSendUtility.sendMessage(admin, "lines " + lines.size());
        boolean init = false;
        for (int r = 0; r < lines.size(); r++) {
            String row = lines.get(r);
            String[] tokens = row.substring(0, 48).trim().split(" ");
            int len = tokens.length;

            for (int i = 0; i < len; i++) {
                if (!init) {
                    if (i == 1) {
                        packet = new SM_CUSTOM_PACKET(Integer.decode("0x" + tokens[i] + tokens[i - 1]));
                        init = true;
                    }
                } else if (r > 0 || i > 4) {
                    packet.addElement(PacketElementType.C, "0x" + tokens[i]);
                }
            }
        }
        if (packet != null) {
            PacketSendUtility.sendMessage(admin, "Packet send..");
            PacketSendUtility.sendPacket(admin, packet);
        }
    } catch (Exception e) {
        PacketSendUtility.sendMessage(admin, "An error has occurred.");
        logger.warn("IO Error.", e);
    }
}

From source file:com.ikon.util.cl.FilesystemClassLoader.java

/**
 * Get resource input stream/*from  w w w . j  ava 2s .co  m*/
 */
@Override
public InputStream getResourceAsStream(String name) {
    log.debug("getResourceAsStream({})", name);
    File fr = new File(file, name);

    try {
        if (fr.exists() && fr.canRead()) {
            return new FileInputStream(fr);
        }
    } catch (FileNotFoundException e) {
        log.error(e.getMessage(), e);
    }

    return null;
}

From source file:com.nike.cerberus.operation.core.UploadCertFilesOperation.java

private String getFileContents(final Path path, final String filename) {
    Preconditions.checkNotNull(path);//from ww w  .  j  a v  a 2 s .  c o  m
    Preconditions.checkNotNull(filename);

    File file = new File(path.toString(), filename);
    if (file.exists() && file.canRead()) {
        try {
            return new String(Files.readAllBytes(file.toPath()), Charset.forName("UTF-8"));
        } catch (IOException e) {
            throw new IllegalStateException("Failed to read the following file: " + file.getAbsolutePath());
        }
    } else {
        throw new IllegalArgumentException("The file is not readable: " + file.getAbsolutePath());
    }
}

From source file:org.chimi.s4s.storage.localfs.LocalFSFileStorageTest.java

@Test
public void save() {
    File file = new File("src/test/resources/temp.sql");
    FileId fileId = storage.save(file);//  w w  w  . j av a 2  s  .  c  om

    // ?   
    String separator = Util.getFileSeparator();
    String filePath = FilePathUtil.getDirectoryPath(fileId) + separator + fileId.toString();
    File savedFile = new File("/s4s/storage", filePath);
    assertTrue(savedFile.exists());
    assertTrue(savedFile.isFile());
    assertTrue(savedFile.canRead());
    logger.info(savedFile.getAbsolutePath() + " created.");
}

From source file:de.u808.simpleinquest.indexer.impl.DefaultIndexer.java

public Document indexFile(File file) throws FileNotFoundException, IndexerException {
    Document document = null;//  w w  w.j a v a  2s.  c  o  m
    if (file.canRead()) {
        try {
            document = new Document();
            InputStream input = new FileInputStream(file);

            Metadata metadata = new Metadata();
            ContentHandler handler = new BodyContentHandler();
            new AutoDetectParser().parse(input, handler, metadata);

            document.add(new Field(Indexer.PATH_FIELD_NAME, file.getPath(), Field.Store.YES,
                    Field.Index.UN_TOKENIZED));
            document.add(new Field(Indexer.ID_FIELD_NAME, MD5Util.getInstance().getMD5Hex(file.getPath()),
                    Field.Store.YES, Field.Index.UN_TOKENIZED));

            document.add(new Field(Indexer.MODIFIED_FIELD_NAME,
                    DateTools.timeToString(file.lastModified(), DateTools.Resolution.MINUTE), Field.Store.YES,
                    Field.Index.UN_TOKENIZED));

            document.add(new Field(Indexer.CONTENT_FIELD_NAME, new StringReader(handler.toString())));
        } catch (Exception e) {
            if (e instanceof TikaException) {
                Throwable t = e.getCause();
                if (t != null && t.getMessage() != null) {
                    if (t.getMessage().startsWith("Error decrypting document")) {
                        log.debug("Cant index encrypted document.");
                        return document;
                    }
                }
            }
            throw new IndexerException(e.getMessage(), e);
        }
    } else {
        log.debug("Cant read file: " + file.getName());
    }
    return document;
}

From source file:com.linkedin.databus2.schemas.FileSystemVersionedSchemaSetProvider.java

private void loadSchemas(File f, VersionedSchemaSet schemas) throws IOException {
    if (!f.exists() || !f.canRead()) {
        throw new IllegalArgumentException("File does not exist or cannot be read: " + f.getAbsolutePath());
    }//from  w  w  w. j  a  v a  2 s . com

    if (f.isDirectory()) {
        File[] files = f.listFiles();
        if (files != null) {
            for (File child : files)
                loadSchemas(child, schemas);
        }
    } else if (f.getName().endsWith(schemaSuffix)) {
        VersionedSchemaId verSchemaId = parseSchemaVersion(f.getName());

        if (null != verSchemaId) {
            logger.info("Loading schema " + verSchemaId + " from file: " + f);
            InputStream input = new FileInputStream(f);
            try {
                String schemaJson = IOUtils.toString(input);
                schemas.add(new VersionedSchema(verSchemaId, Schema.parse(schemaJson), null));
            } finally {
                IOUtils.closeQuietly(input);
            }
        }
    } else {
        logger.debug(
                "Ignoring file " + f + " as it does not end with the required suffix '" + schemaSuffix + "'.");
    }
}