Example usage for org.apache.commons.io FileUtils readFileToString

List of usage examples for org.apache.commons.io FileUtils readFileToString

Introduction

In this page you can find the example usage for org.apache.commons.io FileUtils readFileToString.

Prototype

public static String readFileToString(File file, String encoding) throws IOException 

Source Link

Document

Reads the contents of a file into a String.

Usage

From source file:jsentvar.RDFReaderTest.java

/**
 * Test of reader method, of class RDFReader.
 * Realy, it doesn't work (I mean, test)
 * @throws java.io.IOException//from   w  w w  .  ja  va  2s  .co m
 */
@Test
public void testReader() throws IOException {
    //miniReasoner.owl
    System.out.println("Testing RDFReader.reader");
    String inputFileName = "resources/test/miniReasoner.owl";
    RDFReader instance = new RDFReader();

    String expResult = FileUtils.readFileToString(new File("resources/test/mini_model.txt"), "utf8").trim();
    Model model = instance.reader(inputFileName);

    //FileUtils.writeStringToFile(new File("resources/test/mini_model2.txt"),model.toString());

    //String result = FileUtils.readFileToString(new File("resources/test/mini_model2.txt")).trim();
    String result = model.toString().trim();
    int diff = result.compareTo(expResult);
    System.out.println(expResult);
    System.out.println(result);
    System.out.println(diff);
    assertEquals(expResult, result);
    // TODO review the generated test code and remove the default call to fail.
    //fail("This is not the expected result");
}

From source file:eu.delving.schema.util.FileSystemFetcher.java

public String getFileContents(String path) {
    if (schemas == null)
        findSchemasDirectory();//from ww  w.java2 s.  com
    try {
        return FileUtils.readFileToString(new File(schemas, path), "UTF-8");
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.synopsys.integration.blackduck.codelocation.bdioupload.UploadCallable.java

@Override
public UploadOutput call() {
    try {//from   w  w w.j av  a2s. c  om
        String jsonPayload;
        try {
            jsonPayload = FileUtils.readFileToString(uploadTarget.getUploadFile(), StandardCharsets.UTF_8);
        } catch (IOException e) {
            return UploadOutput.FAILURE(uploadTarget.getCodeLocationName(), "Failed to initially read file: "
                    + uploadTarget.getUploadFile().getAbsolutePath() + " because " + e.getMessage(), e);
        }

        String uri = blackDuckService.getUri(BlackDuckService.BOMIMPORT_PATH);
        Request request = RequestFactory.createCommonPostRequestBuilder(jsonPayload).uri(uri)
                .mimeType(uploadTarget.getMediaType()).build();
        try (Response response = blackDuckService.execute(request)) {
            String responseString = response.getContentString();
            return UploadOutput.SUCCESS(uploadTarget.getCodeLocationName(), responseString);
        } catch (IOException e) {
            return UploadOutput.FAILURE(uploadTarget.getCodeLocationName(), e.getMessage(), e);
        }
    } catch (Exception e) {
        return UploadOutput.FAILURE(uploadTarget.getCodeLocationName(), "Failed to upload file: "
                + uploadTarget.getUploadFile().getAbsolutePath() + " because " + e.getMessage(), e);
    }
}

From source file:de.qaware.cloud.deployer.commons.config.util.FileUtil.java

/**
 * Reads the content of the specified file into a string.
 *
 * @param file The file whose content will be returned.
 * @return The content of the file.//from  w w  w  .  j a  v a  2s  .  c  o  m
 * @throws ResourceConfigException If a problem with the file occurs.
 */
public static String readFileContent(File file) throws ResourceConfigException {
    try {
        if (file == null) {
            throw new ResourceConfigException(
                    COMMONS_MESSAGE_BUNDLE.getMessage("DEPLOYER_COMMONS_ERROR_INVALID_FILENAME"));
        } else if (!file.exists()) {
            throw new ResourceConfigException(
                    COMMONS_MESSAGE_BUNDLE.getMessage("DEPLOYER_COMMONS_ERROR_MISSING_FILE", file.getName()));
        }
        return FileUtils.readFileToString(file, Charset.defaultCharset()).trim();
    } catch (IOException e) {
        throw new ResourceConfigException(e.getMessage(), e);
    }
}

From source file:com.wavemaker.tools.data.SpringCfgGeneratorTest.java

public void testGenerateSpringConfig() throws Exception {

    File f = IOUtils.createTempDirectory();
    try {/*from w  ww.  j a v a  2 s . com*/
        SpringUtils.initSpringConfig();
        SpringCfgGenerator g = new SpringCfgGenerator();
        Folder folder = new LocalFolder(f);
        g.setDestDir(folder);
        g.setClassName("Foo");
        g.setPackage("com.blah");
        g.setExporterFactory(new LocalExporterFactory());
        g.run();

        File s = new File(f, "foo" + DataServiceConstants.SPRING_CFG_EXT);

        Beans b = SpringConfigSupport.readBeans(FileUtils.readFileToString(s, "UTF-8"));

        assertEquals("beans: " + b.getBeanList(), 10, b.getBeanList().size());
    } finally {
        IOUtils.deleteRecursive(f);
    }
}

From source file:com.chrisdoyle.validation.tests.Test_Issue46.java

/**
 * this test is commented out because gradle is inconsistent with provided/compile only dependencies
 * it's only supported with certain versions and on certain versions of the android plugin
 * @throws Exception//w  w  w.j a v a 2 s.  co m
 */
@Ignore
@Test
public void pomScopeDependency() throws Exception {
    //name, description etc, are now covered under {@link com.chrisdoyle.validation.tests.Test_Issues_23_27#checkPomNameDescriptionUrlLicenseEtcAreDefined}

    String[] search = new String[] {
            //compile
            "<dependency>\\s*<groupId>org.apache.commons</groupId>\\s*<artifactId>commons-lang3</artifactId>\\s*<scope>compile</scope>",
            //test
            "<dependency>\\s*<groupId>junit</groupId>\\s*<artifactId>junit</artifactId>\\s*<scope>test</scope>",
            //provided
            "<dependency>\\s*<groupId>commons-io</groupId>\\s*<artifactId>commons-io</artifactId>\\s*<scope>provided</scope>"

    };

    for (int i = 0; i < Main.allPoms.length; i++) {
        if (Main.allPoms[i].contains("hello-world-lib/")) {
            File f = new File(Main.allPoms[i]);

            String str = FileUtils.readFileToString(f, "utf-8");
            for (int k = 0; k < search.length; k++) {
                Pattern p = Pattern.compile(search[k]);
                Matcher matcher = p.matcher(str);
                Assert.assertTrue(search[k] + " not found in " + f.getAbsolutePath(), matcher.find());
            }
        }
    }
}

From source file:aiai.apps.sign_env.SignEnv.java

@Override
public void run(String... args) throws IOException, GeneralSecurityException {

    if (args.length < 2) {
        System.out.println("SignEnv <file with env string> <private key file>");
        return;//w w w .j a  v a 2s  .c o m
    }

    File srcFile = new File(args[0]);
    if (!srcFile.exists()) {
        System.out.println("Source file with environment doesn't exist");
        return;
    }

    File privateKeyFile = new File(args[1]);
    if (!privateKeyFile.exists()) {
        System.out.println("Private key file wasn't found. File: " + args[1]);
        return;
    }
    String privateKeyStr = FileUtils.readFileToString(privateKeyFile, StandardCharsets.UTF_8);
    PrivateKey privateKey = SecUtils.getPrivateKey(privateKeyStr);

    // Process
    String env = FileUtils.readFileToString(srcFile, StandardCharsets.UTF_8).trim();
    String signature = SecUtils.getSignature(env, privateKey, true);

    System.out.println("Env:\n" + env);
    System.out.println("\nSignature:\n" + signature);
}

From source file:de.wintercloud.CompileJinjaMojo.java

public void execute() throws MojoExecutionException {
    try {//from w w  w  . j a v  a  2  s . co  m
        // Load the parameters
        Yaml yaml = new Yaml();
        Map<String, Object> context = (Map<String, Object>) yaml
                .load(FileUtils.readFileToString(varFile, (Charset) null));

        // Load template
        Jinjava jinjava = new Jinjava();
        String template = FileUtils.readFileToString(templateFile, (Charset) null);

        // Render and save
        String rendered = jinjava.render(template, context);
        FileUtils.writeStringToFile(outputFile, rendered, (Charset) null);
    } catch (IOException e) {
        // Print error and exit with -1
        throw new MojoExecutionException(e.getLocalizedMessage(), e);
    }
}

From source file:io.wcm.devops.conga.plugins.aem.tooling.crypto.cli.AnsibleVaultTest.java

@Test
public void testEncryptDecrypt() throws Exception {
    FileUtils.write(testFile, TEST_CONTENT, StandardCharsets.UTF_8);

    // encrypt file
    AnsibleVault.encrypt(testFile);/*from  w ww .ja  v a2 s  . c  o  m*/
    String content = FileUtils.readFileToString(testFile, StandardCharsets.UTF_8);
    assertNotEquals(TEST_CONTENT, content);

    // decrypt file
    AnsibleVault.decrypt(testFile);
    content = FileUtils.readFileToString(testFile, StandardCharsets.UTF_8);
    assertEquals(TEST_CONTENT, content);
}

From source file:io.stallion.plugins.javascript.JavaToJsHelpers.java

public String readFileToString(String file) {
    if (!sandboxed) {
        try {/*from  www  . j av a2  s .  c om*/
            return FileUtils.readFileToString(new File(file), "UTF-8");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    throw new UsageException("You cannot read a file from a sandboxed plugin.");
}