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:com.opentable.config.util.FileConfigStrategy.java

@Override
public AbstractConfiguration load(final String configName, final String configPath)
        throws ConfigurationException {
    if (!(directoryLocation.exists() && directoryLocation.isDirectory() && directoryLocation.canRead())) {
        return null;
    }//from   ww w  .j  a v a2s.c  om
    // A property configuration lives in a configuration directory and is called
    // "config.properties"
    final File[] propertyFiles = new File[] {
            new File(directoryLocation, configPath + File.separator + "config.properties"),
            new File(directoryLocation, configName + ".properties") };

    for (final File propertyFile : propertyFiles) {
        if (propertyFile.exists() && propertyFile.isFile() && propertyFile.canRead()) {
            LOG.trace("Trying to load '{}'...", propertyFile);
            try {
                final AbstractConfiguration config = new PropertiesConfiguration(propertyFile);
                LOG.trace("... succeeded");
                return config;
            } catch (ConfigurationException ce) {
                LOG.trace("... failed", ce);
            }
        } else {
            LOG.debug("'{}' does not exist", propertyFile);
        }
    }
    return null;
}

From source file:com.crossover.trial.weather.util.AirportLoader.java

@Override
public void run(ApplicationArguments args) throws Exception {
    if (args.containsOption("airports") && !args.getOptionValues("airports").isEmpty()) {
        File dataFile = new File(args.getOptionValues("airports").get(0));
        if (dataFile.canRead() && dataFile.isFile() && dataFile.exists()) {
            log.info("Loading airports from [{}]", dataFile);

            try (Stream<String> lines = Files.lines(dataFile.toPath())) {
                lines.map(Row::new).filter(Row::expectedTotalColumns).filter(Row::expectedColumnsFilled)
                        .map(Row::parse).filter(airport -> airport != null).forEach(airportRepository::save);
            }//from  w  w  w  . ja va 2s.co  m

        } else
            log.error("Specified airports data file is not readable [{}]", dataFile.toPath());

    }
}

From source file:com.adaptris.core.services.ScriptingService.java

@Override
protected void initService() throws CoreException {
    if (isEmpty(getScriptFilename())) {
        throw new CoreException("script filename is null");
    }//from   w w  w. j  a  v  a 2s.c  om
    File f = new File(getScriptFilename());
    if (!f.exists() || !f.isFile() || !f.canRead()) {
        throw new CoreException(getScriptFilename() + " is not accessible");
    }
    super.initService();
}

From source file:com.nike.cerberus.command.validator.UploadCertFilesPathValidator.java

@Override
public void validate(final String name, final Path value) throws ParameterException {

    if (value == null) {
        throw new ParameterException("Value must be specified.");
    }//from  ww  w . j  ava2  s  .c  o m

    final File certDirectory = value.toFile();
    final Set<String> filenames = Sets.newHashSet();

    if (!certDirectory.canRead()) {
        throw new ParameterException("Specified path is not readable.");
    }

    if (!certDirectory.isDirectory()) {
        throw new ParameterException("Specified path is not a directory.");
    }

    final FilenameFilter filter = new RegexFileFilter("^.*\\.pem$");
    final File[] files = certDirectory.listFiles(filter);
    Arrays.stream(files).forEach(file -> filenames.add(file.getName()));

    if (!filenames.containsAll(EXPECTED_FILE_NAMES)) {
        final StringJoiner sj = new StringJoiner(", ", "[", "]");
        EXPECTED_FILE_NAMES.stream().forEach(sj::add);
        throw new ParameterException("Not all expected files are present! Expected: " + sj.toString());
    }
}

From source file:jfs.sync.encrypted.EncryptedFileStorageAccess.java

protected String filePermissionsString(File file) {
    return (file.canRead() ? "r" : "-") + (file.canWrite() ? "w" : "-") + (file.canExecute() ? "x" : "-");
}

From source file:no.dusken.common.view.FileView.java

protected void renderMergedOutputModel(Map map, HttpServletRequest request, HttpServletResponse response)
        throws IOException {
    // get the file from the map
    File file = (File) map.get("file");

    // check if it exists
    if (file == null || !file.exists() || !file.canRead()) {
        log.warn("Error reading: {}", file);
        try {/* w  ww .  ja v  a2s .  c o  m*/
            response.sendError(404);
        } catch (IOException e) {
            log.error("Could not write to response", e);
        }
        return;
    }
    // give some info in the response
    String mimeType;
    if (file.getName().contains(".image")) {
        mimeType = "image/jpeg";

    } else {
        mimeType = getServletContext().getMimeType(file.getAbsolutePath());
    }
    response.setContentType(mimeType);
    // files does not change so often, allow three days caching
    response.setHeader("Cache-Control", "public, max-age=2505600");
    response.setContentLength((int) file.length());

    // start writing to the response
    OutputStream outputStream = response.getOutputStream();
    InputStream inputStream = new FileInputStream(file);
    IOUtils.copy(inputStream, outputStream);
}

From source file:net.urlgrey.mythpodcaster.transcode.ClipLocatorImpl.java

@Override
public File locateThumbnailForOriginalClip(String filename) {
    final File clipFile = this.locateOriginalClip(filename);
    if (clipFile != null) {
        final File clipThumbnailFile = new File(clipFile.getParentFile(), clipFile.getName() + PNG_EXTENSION);
        if (clipThumbnailFile.canRead()) {
            return clipThumbnailFile;
        }//from w  w w  .  j a v a2  s.  c  o  m
    }

    return null;
}

From source file:com.meltmedia.cadmium.servlets.shiro.PersistablePropertiesRealm.java

/**
 * loads a properties file named {@link REALM_FILE_NAME} in the directory passed in.
 *///from ww w.  j av a  2  s .c  om
public void loadProperties(File contentDir) {
    String resourceFile = null;
    if (contentDir.exists() && contentDir.isDirectory()) {
        File propFile = new File(contentDir, REALM_FILE_NAME);
        if (propFile.exists() && propFile.canRead()) {
            resourceFile = propFile.getAbsoluteFile().getAbsolutePath();
        }
    } else if (contentDir.exists() && contentDir.isFile() && contentDir.canRead()) {
        resourceFile = contentDir.getAbsoluteFile().getAbsolutePath();
    }
    if (StringUtils.isNotBlank(resourceFile)) {
        this.setResourcePath("file:" + resourceFile);
        this.destroy();
        this.init();
    }
}

From source file:it.jnrpe.server.config.CJNRPEConfiguration.java

private CJNRPEConfiguration(File fileName) {
    if (!fileName.exists() || !fileName.canRead()) {
        // TODO: throw an exception
        m_Logger.fatal("UNABLE TO READ CONFIGURATION FILE " + fileName.getAbsolutePath());
        System.exit(-1);/*  w  w w . j a v  a2  s . c  o m*/
    }

    try {
        Digester digester = DigesterLoader.createDigester(
                new InputSource(System.class.getResourceAsStream("/it/jnrpe/server/config/digester.xml")));
        // turn on XML schema validation
        digester.setFeature("http://xml.org/sax/features/validation", true);
        digester.setFeature("http://apache.org/xml/features/validation/schema", true);
        digester.setFeature("http://xml.org/sax/features/namespaces", true);
        digester.setEntityResolver(new CConfigValidationEntityResolver());
        digester.setErrorHandler(new CConfigErrorHandler());

        m_Configuration = (CConfiguration) digester.parse(fileName);
    } catch (Exception e) {
        // TODO: throw an exception
        m_Logger.fatal("UNABLE TO PARSE CONFIGURATION : " + e.getMessage());
        System.exit(-1);
    }
}

From source file:org.ala.util.RepositoryFileUtils.java

/**
 * For a given (absolute) file name string, read the file and return as a list
 * of String[] arrays (2 column for dc files and 3 columns for rdf files)
 *
 * @param fileName//from  w  w  w . j  a  v  a 2  s  .  c o m
 * @return
 * @throws Exception
 */
public List<String[]> readRepositoryFile(String fileName) throws Exception {
    List<String[]> lines = new ArrayList<String[]>();
    File file = new File(fileName);
    if (file.canRead()) {
        lines = readRepositoryFile(file);
    }
    return lines;
}