Example usage for org.apache.commons.io IOUtils readLines

List of usage examples for org.apache.commons.io IOUtils readLines

Introduction

In this page you can find the example usage for org.apache.commons.io IOUtils readLines.

Prototype

public static List readLines(Reader input) throws IOException 

Source Link

Document

Get the contents of a Reader as a list of Strings, one entry per line.

Usage

From source file:org.apache.bigtop.itest.datafu.PigTests.java

/**
 * Gets the lines from a given file.//from  w  w  w  .jav a  2 s .c  om
 * 
 * @param relativeFilePath The path relative to the datafu-tests project.
 * @return The lines from the file
 * @throws IOException
 */
protected String[] getLinesFromFile(String relativeFilePath) throws IOException {
    // assume that the working directory is the datafu-tests project
    File file = new File(System.getProperty("user.dir"), relativeFilePath).getAbsoluteFile();
    BufferedInputStream content = new BufferedInputStream(new FileInputStream(file));
    Object[] lines = IOUtils.readLines(content).toArray();
    String[] result = new String[lines.length];
    for (int i = 0; i < lines.length; i++) {
        result[i] = (String) lines[i];
    }
    return result;
}

From source file:org.apache.chemistry.shell.Main.java

private void runInBatchMode() throws IOException {
    Console.setDefault(new Console());
    Console.getDefault().setPrintTimeStamps(enableTime);
    Console.getDefault().start(app);
    List<String> cmds;
    if (command == null) {
        cmds = IOUtils.readLines(System.in);
    } else {/*from   w  w  w  .j  a  v a  2s  .c om*/
        cmds = IOUtils.readLines(new FileInputStream(new File(command)));
    }
    for (String cmd : cmds) {
        // Ignore empty lines / comments
        if (cmd.length() == 0 || cmd.startsWith("#")) {
            continue;
        }
        Console.getDefault().println("Running: " + cmd);
        try {
            Console.getDefault().runCommand(cmd);
        } catch (ExitException e) {
            Console.getDefault().println("Bye.");
            return;
        } catch (Exception e) {
            Console.getDefault().error(e.getMessage());
            if (testMode) {
                e.printStackTrace();
                Console.getDefault().println("Exiting on error.");
                System.exit(1);
                return;
            }
        }
    }
    Console.getDefault().println("Done.");
}

From source file:org.apache.click.util.ClickUtils.java

/**
 * Deploys required files (from a file list) for a control that repsects a specific convention.
 * <p/>//from w  w  w. java2s  .  c  o  m
 * <b>Convention:</b>
 * <p/>
 * There's a descriptor file generated by the <code>tools/standalone/dev-tasks/ListFilesTask</code>.
 * The files to deploy are all in a subdirectory placed in the same directory with the control.
 * See documentation for more details. <p/>
 *
 * <b>Usage:</b><p/>
 * In your Control simply use the code below, and everything should work automatically.
 * <pre class="prettyprint">
 * public void onDeploy(ServletContext servletContext) {
 *    ClickUtils.deployFileList(servletContext, HeavyControl.class, "click");
 * } </pre>
 *
 * @param servletContext the web applications servlet context
 * @param controlClass the class of the Control that has files for deployment
 * @param targetDir target directory where to deploy the files to. In most cases this
 * is only the reserved directory <code>click</code>
 */
public static void deployFileList(ServletContext servletContext, Class<? extends Control> controlClass,
        String targetDir) {

    String packageName = ClassUtils.getPackageName(controlClass);
    packageName = StringUtils.replaceChars(packageName, '.', '/');
    packageName = "/" + packageName;
    String controlName = ClassUtils.getShortClassName(controlClass);

    ConfigService configService = getConfigService(servletContext);
    LogService logService = configService.getLogService();
    String descriptorFile = packageName + "/" + controlName + ".files";
    logService.debug("Use deployment descriptor file:" + descriptorFile);

    try {
        InputStream is = getResourceAsStream(descriptorFile, ClickUtils.class);
        List fileList = IOUtils.readLines(is);
        if (fileList == null || fileList.isEmpty()) {
            logService.info("there are no files to deploy for control " + controlClass.getName());
            return;
        }

        // a target dir list is required cause the ClickUtils.deployFile() is too inflexible to autodetect
        // required subdirectories.
        List<String> targetDirList = new ArrayList<String>(fileList.size());
        for (int i = 0; i < fileList.size(); i++) {
            String filePath = (String) fileList.get(i);
            String destination = "";
            int index = filePath.lastIndexOf('/');
            if (index != -1) {
                destination = filePath.substring(0, index + 1);
            }
            targetDirList.add(i, targetDir + "/" + destination);
            fileList.set(i, packageName + "/" + filePath);
        }

        for (int i = 0; i < fileList.size(); i++) {
            String source = (String) fileList.get(i);
            String targetDirName = targetDirList.get(i);
            ClickUtils.deployFile(servletContext, source, targetDirName);
        }

    } catch (IOException e) {
        String msg = "error occurred getting resource " + descriptorFile + ", error " + e;
        logService.warn(msg);
    }
}

From source file:org.apache.coheigea.bigdata.storm.WordSpout.java

public WordSpout(String filePath) throws Exception {
    java.io.File inputFile = new java.io.File(filePath);
    words = IOUtils.readLines(new java.io.FileInputStream(inputFile));
}

From source file:org.apache.coheigea.bigdata.storm.WordSpout.java

public WordSpout(URI filePath) throws Exception {
    java.io.File inputFile = new java.io.File(filePath);
    words = IOUtils.readLines(new java.io.FileInputStream(inputFile));
}

From source file:org.apache.giraph.debugger.gui.ServerUtils.java

/**
 * @param jobId id of the job, whose jar path will be returned.
 * @return a url wrapped inside an array for convenience.
 *//*from  w w  w.ja  va2s .  c o m*/
public static URL[] getCachedJobJarPath(String jobId) {
    // read the jar signature file under the TRACE_ROOT/jobId/
    Path jarSignaturePath = new Path(DebuggerUtils.getTraceFileRoot(jobId) + "/" + "jar.signature");
    try {
        FileSystem fs = getFileSystem();
        try (FSDataInputStream jarSignatureInput = fs.open(jarSignaturePath)) {
            List<String> lines = IOUtils.readLines(jarSignatureInput);
            if (lines.size() > 0) {
                String jarSignature = lines.get(0);
                // check if jar is already in JARCACHE_LOCAL
                File localFile = new File(DebuggerUtils.JARCACHE_LOCAL + "/" + jarSignature + ".jar");
                if (!localFile.exists()) {
                    // otherwise, download from HDFS
                    Path hdfsPath = new Path(
                            fs.getUri().resolve(DebuggerUtils.JARCACHE_HDFS + "/" + jarSignature + ".jar"));
                    Logger.getLogger(ServerUtils.class)
                            .info("Copying from HDFS: " + hdfsPath + " to " + localFile);
                    localFile.getParentFile().mkdirs();
                    fs.copyToLocalFile(hdfsPath, new Path(localFile.toURI()));
                }
                return new URL[] { localFile.toURI().toURL() };
            }
        }
    } catch (IOException e) {
        // gracefully ignore if we failed to read the jar.signature
        LOG.warn("An IOException is thrown but will be ignored: " + e.toString());
    }
    return new URL[0];
}

From source file:org.apache.gobblin.aws.CloudInitScriptBuilderTest.java

/**
 * loads the given file into a string, ignoring the comments, but considering "#!/bin/bash"
 * @param file file to read//from www.j  av  a2s. com
 * @return file content as a string
 * @throws IOException
 */
private String loadFile(String file) throws IOException {
    StringBuilder sb = new StringBuilder();

    List<String> lines = IOUtils.readLines(new InputStreamReader(
            GobblinAWSClusterLauncherTest.class.getClassLoader().getResourceAsStream(file), "UTF-8"));

    for (String line : lines) {
        if (line.equals(CloudInitScriptBuilder.BASH)
                || (!line.startsWith("#") && !Strings.isNullOrEmpty(line))) {
            sb.append(line).append("\n");
        }
    }
    return sb.toString();
}

From source file:org.apache.gobblin.data.management.copy.recovery.RecoveryHelperTest.java

@Test
public void testPersistFile() throws Exception {

    String content = "contents";

    File stagingDir = Files.createTempDir();
    stagingDir.deleteOnExit();// w ww  . java 2s.c o m

    File file = new File(stagingDir, "file");
    OutputStream os = new FileOutputStream(file);
    IOUtils.write(content, os);
    os.close();

    Assert.assertEquals(stagingDir.listFiles().length, 1);

    State state = new State();
    state.setProp(RecoveryHelper.PERSIST_DIR_KEY, this.tmpDir.getAbsolutePath());
    state.setProp(ConfigurationKeys.DATA_PUBLISHER_FINAL_DIR, "/publisher");

    File recoveryDir = new File(RecoveryHelper.getPersistDir(state).get().toUri().getPath());

    FileSystem fs = FileSystem.getLocal(new Configuration());

    CopyableFile copyableFile = CopyableFile.builder(fs, new FileStatus(0, false, 0, 0, 0, new Path("/file")),
            new Path("/dataset"), CopyConfiguration.builder(fs, state.getProperties())
                    .preserve(PreserveAttributes.fromMnemonicString("")).build())
            .build();

    CopySource.setWorkUnitGuid(state, Guid.fromHasGuid(copyableFile));

    RecoveryHelper recoveryHelper = new RecoveryHelper(FileSystem.getLocal(new Configuration()), state);

    recoveryHelper.persistFile(state, copyableFile, new Path(file.getAbsolutePath()));

    Assert.assertEquals(stagingDir.listFiles().length, 0);
    Assert.assertEquals(recoveryDir.listFiles().length, 1);

    File fileInRecovery = recoveryDir.listFiles()[0].listFiles()[0];
    Assert.assertEquals(IOUtils.readLines(new FileInputStream(fileInRecovery)).get(0), content);

    Optional<FileStatus> fileToRecover = recoveryHelper.findPersistedFile(state, copyableFile,
            Predicates.<FileStatus>alwaysTrue());
    Assert.assertTrue(fileToRecover.isPresent());
    Assert.assertEquals(fileToRecover.get().getPath().toUri().getPath(), fileInRecovery.getAbsolutePath());

    fileToRecover = recoveryHelper.findPersistedFile(state, copyableFile, Predicates.<FileStatus>alwaysFalse());
    Assert.assertFalse(fileToRecover.isPresent());

}

From source file:org.apache.hadoop.security.KDiag.java

/**
 * Dump any file to standard out./*w w  w  .j ava 2s.c  o m*/
 * @param file file to dump
 * @throws IOException IO problems
 */
private void dump(File file) throws IOException {
    try (FileInputStream in = new FileInputStream(file)) {
        for (String line : IOUtils.readLines(in)) {
            println(line);
        }
    }
}

From source file:org.apache.hadoop.security.KerberosDiags.java

/**
 * Dump any file to standard out; add a trailing newline
 * @param file file to dump//from  ww  w  .j a va  2  s.  c  o m
 * @throws IOException IO problems
 */
public void dump(File file) throws IOException {
    try (FileInputStream in = new FileInputStream(file)) {
        for (String line : IOUtils.readLines(in)) {
            println("%s", line);
        }
    }
    println("");
}