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

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

Introduction

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

Prototype

public static List readLines(File file, String encoding) throws IOException 

Source Link

Document

Reads the contents of a file line by line to a List of Strings.

Usage

From source file:eu.annocultor.data.filters.PatternBasedFilter.java

void load(File file, List<String> patterns) throws IOException {
    for (Object o : FileUtils.readLines(file, "UTF-8")) {
        String line = o.toString();
        if (line.isEmpty() || line.startsWith("#")) {
            continue;
        }/*w  ww .ja v  a  2 s .c  o m*/
        patterns.add(line);
    }
    if (patterns.isEmpty()) {
        log.warn("Did not loaded much filters from " + file);
    }
}

From source file:com.lynn.controller.FileAction.java

@RequestMapping("/read.do")
@ResponseBody//w ww . j a  v a  2  s. c  o m
public String read() throws IOException {
    File file = initFile();
    //FileUtils ?
    String content = FileUtils.readFileToString(file, encoding);
    List<String> listStrs = FileUtils.readLines(file, encoding);
    //??

    return content;
}

From source file:com.xeiam.datasets.hjabirdsong.bootstrap.DownloadWavFiles.java

private void go() throws IOException {

    String baseURL = "http://web.engr.oregonstate.edu/~briggsf/kdd2012datasets/hja_birdsong/src_wavs/";

    List<String> wavNameLines = FileUtils.readLines(new File("./raw/id2filename.txt"), "UTF-8");

    for (int i = 0; i < wavNameLines.size(); i++) {

        String wavNameLine = wavNameLines.get(i);
        String wavFileName = wavNameLine.substring(wavNameLine.indexOf(",") + 1, wavNameLine.length()) + ".wav";
        System.out.println("downloading: " + wavFileName);
        URL url = new URL(baseURL + wavFileName);
        File wavDir = new File("./raw/wav/" + wavFileName);
        org.apache.commons.io.FileUtils.copyURLToFile(url, wavDir, 5000, 10000);
    }//w ww .j a  va  2s  .  c  o  m

}

From source file:com.javacreed.examples.sc.part3.MembersServiceImpl.java

@Override
@Cacheable("members")
public Member getMemberWithId(final int id) {
    System.out.printf("Retrieving the member with id: [%d] from file: %s%n", id, dataFile.getAbsolutePath());
    try {//from  w  w  w. ja  v  a2s  .c o  m
        for (final String line : FileUtils.readLines(dataFile, "UTF-8")) {
            final String[] parts = line.split(",");
            final int memberId = Integer.parseInt(parts[0]);
            if (memberId == id) {
                return new Member(memberId, parts[1]);
            }
        }
    } catch (final Exception e) {
        throw new RuntimeException("Failed to load members", e);
    }

    return null;
}

From source file:com.yoncabt.abys.core.util.EBRConf.java

private void reload() {
    // 2 defa almasn buras
    synchronized (reloadLock) {
        File confFile = getConfFile();
        Map<String, String> tmp = new HashMap<>();
        if (confFile.exists() && confFile.isFile() && confFile.canRead()) {
            lastModified = confFile.lastModified();
            try {
                List<String> lines = FileUtils.readLines(confFile, "utf-8");
                for (String line : lines) {
                    if (StringUtils.isBlank(line) || line.trim().charAt(0) == '#') {
                        continue;
                    }/*from  w  w  w. j a v  a  2  s. co  m*/
                    String[] kv = line.split("=", 2);
                    if (kv.length != 2) {
                        continue;
                    }
                    final String key = kv[0].trim();
                    final String value = kv[1].trim();
                    tmp.put(key, value);
                }
            } catch (IOException ex) {
                Logger.getLogger(EBRConf.class.getName()).log(Level.SEVERE, null, ex);
            }
        } else {
            //System.out.println(confFile.getAbsolutePath() + ": OKUNMUYOR");
        }
        reconnectToDb(tmp);
        map = tmp;
        for (Map.Entry<String, String> entrySet : map.entrySet()) {
            String key = entrySet.getKey();
            String value = entrySet.getValue();
            if (key.startsWith("system.")) {
                System.setProperty(key.substring("system.".length()), value);
            }
        }
    }
}

From source file:BuildIntegrationTest.java

@Before
public void setup() throws IOException {

    //Copy Plugin source files to temp directory to be checked
    FileUtils.copyDirectory(new File("src/test/resources"), testProjectDir.getRoot());

    //Add Plugin classpath to the test runner
    File testClassPath = new File("build/createClasspathManifest/plugin-classpath.txt");

    if (!testClassPath.exists()) {
        throw new IllegalStateException(
                "Did not find plugin classpath resource, run `testClasses` build task.");
    }/* w ww .ja  v  a  2s.  c  om*/

    List<String> classPathStrings = FileUtils.readLines(testClassPath, null);

    classPaths = new ArrayList<>();

    for (String classPath : classPathStrings) {
        classPaths.add(new File(classPath));
    }

    System.out.println(">>>>>>>>>>> Build Integration Test Started");
}

From source file:com.ppcxy.cyfm.showcase.demos.utilities.io.IODemo.java

@Test
public void workWithFileContent() {
    File file = new File("woop.txt");
    File destFile = new File("bar.txt");
    try {//www. j  a va 2  s  .  c o m
        // text -> file, Collection<String>, byte[] ->file
        FileUtils.writeStringToFile(file, "Hey sailor!\nHaha\n", "UTF-8");

        // inputstream -> file
        InputStream source = IOUtils.toInputStream("Hej", "UTF-8");
        FileUtils.copyInputStreamToFile(source, file);

        // ///////////////////////////
        // file -> outputstream
        System.out.println("copy File to outputstream:");
        FileUtils.copyFile(file, System.out);

        // file -> file
        FileUtils.copyFile(file, destFile);

        // file -> string
        System.out.println("File to String:");
        System.out.println(FileUtils.readFileToString(file, "UTF-8"));

        // file -> list<string>
        System.out.println("File to List<String>:");
        List<String> lines = FileUtils.readLines(file, "UTF-8");
        for (String string : lines) {
            System.out.println(string);
        }
    } catch (IOException e) {
        Exceptions.unchecked(e);
    }
}

From source file:com.kurento.kmf.test.services.Recorder.java

public static float getPesqMos(String audio, int sampleRate) {
    float pesqmos = 0;

    try {/*w w  w.  ja v a 2s .c  om*/
        String pesq = KurentoServicesTestHelper.getTestFilesPath() + "/bin/pesq/PESQ";
        String origWav = "";
        if (audio.startsWith(HTTP_TEST_FILES)) {
            origWav = KurentoServicesTestHelper.getTestFilesPath() + audio.replace(HTTP_TEST_FILES, "");
        } else {
            // Download URL
            origWav = KurentoMediaServerManager.getWorkspace() + "/downloaded.wav";
            URL url = new URL(audio);
            ReadableByteChannel rbc = Channels.newChannel(url.openStream());
            FileOutputStream fos = new FileOutputStream(origWav);
            fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
            fos.close();
        }

        Shell.runAndWait(pesq, "+" + sampleRate, origWav, RECORDED_WAV);
        List<String> lines = FileUtils.readLines(new File(PESQ_RESULTS), "utf-8");
        pesqmos = Float.parseFloat(lines.get(1).split("\t")[2].trim());
        log.info("PESQMOS " + pesqmos);

        Shell.runAndWait("rm", PESQ_RESULTS);

    } catch (IOException e) {
        log.error("Exception recording local audio", e);
    }

    return pesqmos;
}

From source file:de.tudarmstadt.ukp.experiments.argumentation.convincingness.features.SpellCheckingFeature.java

@Override
public void initialize(UimaContext context) throws ResourceInitializationException {
    super.initialize(context);

    try {/*w  w w. j a v a2 s  .  com*/
        vocabulary = new TreeSet<String>();

        File wordFile = new File("/usr/share/dict/words");
        if (!wordFile.exists()) {
            throw new IOException("File not found " + wordFile);
        }

        // load vocabulary
        vocabulary.addAll(FileUtils.readLines(wordFile, "utf-8"));

        getLogger().info("Loaded " + vocabulary.size() + " words");
    } catch (IOException e) {
        throw new ResourceInitializationException(e);
    }

}

From source file:com.mohawk.webcrawler.ScriptCompiler.java

/**
 * Takes a script file and creates a queue of string token and verb objects
 *
 * @param filename the full file name of the script
 * @return/*from   w ww.j a  va 2  s  .  c om*/
 * @throws IOException
 * @throws Exception
 */
public ScriptExecutable compile(File file) throws IOException, CompilationException {

    List<String> lines = FileUtils.readLines(file, "UTF-8");
    StringBuffer fileContents = new StringBuffer();

    CommentRemover comment = new CommentRemover();

    for (String line : lines)
        fileContents.append(comment.remove(line)).append(' ');

    System.out.printf("fileContents %s", fileContents);

    // reorganize the tokens into scopes
    return compile0(fileContents.toString());
}