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) throws IOException 

Source Link

Document

Reads the contents of a file line by line to a List of Strings using the default encoding for the VM.

Usage

From source file:de.tudarmstadt.ukp.dkpro.tc.ml.uima.TcAnnotatorSequence.java

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

    try {/*from   w  w  w  .  j av  a  2s  .c  o  m*/
        mlAdapter = (TCMachineLearningAdapter) Class
                .forName(FileUtils.readFileToString(new File(tcModelLocation, MODEL_META))).newInstance();
    } catch (InstantiationException e) {
        throw new ResourceInitializationException(e);
    } catch (IllegalAccessException e) {
        throw new ResourceInitializationException(e);
    } catch (ClassNotFoundException e) {
        throw new ResourceInitializationException(e);
    } catch (IOException e) {
        throw new ResourceInitializationException(e);
    }

    parameters = new ArrayList<>();
    try {
        for (String parameter : FileUtils.readLines(new File(tcModelLocation, MODEL_PARAMETERS))) {
            if (!parameter.startsWith("#")) {
                String[] parts = parameter.split("=");
                parameters.add(parts[0]);
                parameters.add(parts[1]);
            }
        }
    } catch (Exception e) {
        throw new ResourceInitializationException(e);
    }
    featureExtractors = new ArrayList<>();
    try {
        for (String featureExtractor : FileUtils
                .readLines(new File(tcModelLocation, MODEL_FEATURE_EXTRACTORS))) {
            featureExtractors.add(featureExtractor);
        }
    } catch (IOException e) {
        throw new ResourceInitializationException(e);
    }

    // featureExtractors = new ArrayList<>();
    // try {
    // for (String featureExtractor : FileUtils.readLines(new File(tcModelLocation,
    // "features.txt"))) {
    // featureExtractors.add(
    // (FeatureExtractorResource_ImplBase) Class.forName(featureExtractor).newInstance()
    // );
    // }
    // } catch (InstantiationException e) {
    // throw new ResourceInitializationException(e);
    // } catch (IllegalAccessException e) {
    // throw new ResourceInitializationException(e);
    // } catch (ClassNotFoundException e) {
    // throw new ResourceInitializationException(e);
    // } catch (IOException e) {
    // throw new ResourceInitializationException(e);
    // }
}

From source file:android.databinding.tool.reflection.java.JavaAnalyzer.java

private static String loadAndroidHome() {
    Map<String, String> env = System.getenv();
    for (Map.Entry<String, String> entry : env.entrySet()) {
        L.d("%s %s", entry.getKey(), entry.getValue());
    }/*from  w  w w  .j a v a  2  s  . c  om*/
    if (env.containsKey("ANDROID_HOME")) {
        return env.get("ANDROID_HOME");
    }
    // check for local.properties file
    File folder = new File(".").getAbsoluteFile();
    while (folder != null && folder.exists()) {
        File f = new File(folder, "local.properties");
        if (f.exists() && f.canRead()) {
            try {
                for (String line : FileUtils.readLines(f)) {
                    List<String> keyValue = Splitter.on('=').splitToList(line);
                    if (keyValue.size() == 2) {
                        String key = keyValue.get(0).trim();
                        if (key.equals("sdk.dir")) {
                            return keyValue.get(1).trim();
                        }
                    }
                }
            } catch (IOException ignored) {
            }
        }
        folder = folder.getParentFile();
    }

    return null;
}

From source file:edu.isi.wings.portal.classes.StorageHandler.java

public static String unzipFile(File f, String todirname, String toDirectory) {
    File todir = new File(toDirectory);
    if (!todir.exists())
        todir.mkdirs();// www .  java 2s .  c  om

    try {
        // Check if the zip file contains only one directory
        ZipFile zfile = new ZipFile(f);
        String topDir = null;
        boolean isOneDir = true;
        for (Enumeration<? extends ZipEntry> e = zfile.entries(); e.hasMoreElements();) {
            ZipEntry ze = e.nextElement();
            String name = ze.getName().replaceAll("/.+$", "");
            name = name.replaceAll("/$", "");
            // OSX Zips carry an extra __MACOSX directory. Ignore it
            if (name.equals("__MACOSX"))
                continue;

            if (topDir == null)
                topDir = name;
            else if (!topDir.equals(name)) {
                isOneDir = false;
                break;
            }
        }
        zfile.close();

        // Delete existing directory (if any)
        FileUtils.deleteDirectory(new File(toDirectory + File.separator + todirname));

        // Unzip file(s) into toDirectory/todirname
        ZipInputStream zis = new ZipInputStream(new FileInputStream(f));
        ZipEntry ze = zis.getNextEntry();
        while (ze != null) {
            String fileName = ze.getName();
            // OSX Zips carry an extra __MACOSX directory. Ignore it
            if (fileName.startsWith("__MACOSX")) {
                ze = zis.getNextEntry();
                continue;
            }

            // Get relative file path translated to 'todirname'
            if (isOneDir)
                fileName = fileName.replaceFirst(topDir, todirname);
            else
                fileName = todirname + File.separator + fileName;

            // Create directories
            File newFile = new File(toDirectory + File.separator + fileName);
            if (ze.isDirectory())
                newFile.mkdirs();
            else
                newFile.getParentFile().mkdirs();

            try {
                // Copy file
                FileOutputStream fos = new FileOutputStream(newFile);
                IOUtils.copy(zis, fos);
                fos.close();

                String mime = new Tika().detect(newFile);
                if (mime.equals("application/x-sh") || mime.startsWith("text/"))
                    FileUtils.writeLines(newFile, FileUtils.readLines(newFile));

                // Set all files as executable for now
                newFile.setExecutable(true);
            } catch (FileNotFoundException fe) {
                // Silently ignore
                //fe.printStackTrace();
            }
            ze = zis.getNextEntry();
        }
        zis.closeEntry();
        zis.close();

        return toDirectory + File.separator + todirname;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:com.linkedin.pinot.index.persist.AvroDataPublisherTest.java

@Test
public void TestReadPartialAvro() throws Exception {
    final String filePath = TestUtils
            .getFileFromResourceUrl(getClass().getClassLoader().getResource(AVRO_DATA));
    final String jsonPath = TestUtils
            .getFileFromResourceUrl(getClass().getClassLoader().getResource(JSON_DATA));

    final List<String> projectedColumns = new ArrayList<String>();
    projectedColumns.add("column3");
    projectedColumns.add("column2");

    Schema schema = new Schema.SchemaBuilder().addSingleValueDimension("column3", DataType.STRING)
            .addSingleValueDimension("column2", DataType.STRING).build();
    final SegmentGeneratorConfig config = new SegmentGeneratorConfig(schema);

    config.setFormat(FileFormat.AVRO);//from   w w  w . ja v a 2  s.c o m
    config.setInputFilePath(filePath);

    config.setSegmentVersion(SegmentVersion.v1);

    final AvroRecordReader avroDataPublisher = new AvroRecordReader(
            FieldExtractorFactory.getPlainFieldExtractor(config), config.getInputFilePath());
    avroDataPublisher.next();
    int cnt = 0;
    for (final String line : FileUtils.readLines(new File(jsonPath))) {

        final JSONObject obj = new JSONObject(line);
        if (avroDataPublisher.hasNext()) {
            final GenericRow recordRow = avroDataPublisher.next();
            // System.out.println(recordRow);
            Assert.assertEquals(recordRow.getFieldNames().length, 2);
            for (final String column : recordRow.getFieldNames()) {
                final String valueFromJson = obj.get(column).toString();
                final String valueFromAvro = recordRow.getValue(column).toString();
                if (cnt > 1) {
                    Assert.assertEquals(valueFromAvro, valueFromJson);
                }
            }
        }
        cnt++;
    }
    Assert.assertEquals(10001, cnt);
}

From source file:android.databinding.compilationTest.BaseCompilationTest.java

/**
 * Extracts the text in the given location from the the at the given application path.
 *
 * @param pathInApp The path, relative to the root of the application under test
 * @param location  The location to extract
 * @return The string that is contained in the given location
 * @throws IOException If file is invalid.
 *///from  w w  w .ja va  2 s.  c  om
protected String extract(String pathInApp, Location location) throws IOException {
    File file = new File(testFolder, pathInApp);
    assertTrue(file.exists());
    StringBuilder result = new StringBuilder();
    List<String> lines = FileUtils.readLines(file);
    for (int i = location.startLine; i <= location.endLine; i++) {
        if (i > location.startLine) {
            result.append("\n");
        }
        String line = lines.get(i);
        int start = 0;
        if (i == location.startLine) {
            start = location.startOffset;
        }
        int end = line.length() - 1; // inclusive
        if (i == location.endLine) {
            end = location.endOffset;
        }
        result.append(line.substring(start, end + 1));
    }
    return result.toString();
}

From source file:com.gargoylesoftware.htmlunit.CodeStyleTest.java

private void process(final File dir) throws IOException {
    final File[] files = dir.listFiles();
    if (files != null) {
        for (final File file : files) {
            if (file.isDirectory() && !".svn".equals(file.getName())) {
                process(file);//  w  w  w.  j  ava2s  .  c  om
            } else {
                final String relativePath = file.getAbsolutePath()
                        .substring(new File(".").getAbsolutePath().length() - 1);
                svnProperties(file, relativePath);
                if (file.getName().endsWith(".java")) {
                    final List<String> lines = FileUtils.readLines(file);
                    openingCurlyBracket(lines, relativePath);
                    year(lines, relativePath);
                    javaDocFirstLine(lines, relativePath);
                    methodFirstLine(lines, relativePath);
                    methodLastLine(lines, relativePath);
                    lineBetweenMethods(lines, relativePath);
                    runWith(lines, relativePath);
                    vs85aspx(lines, relativePath);
                    deprecated(lines, relativePath);
                    staticJSMethod(lines, relativePath);
                    singleAlert(lines, relativePath);
                    staticLoggers(lines, relativePath);
                    loggingEnabled(lines, relativePath);
                    browserVersion_isIE(lines, relativePath);
                    alerts(lines, relativePath);
                }
            }
        }
    }
}

From source file:de.tudarmstadt.ukp.dkpro.core.mallet.topicmodel.io.MalletTopicProportionsWriterTest.java

@Test
public void test() throws UIMAException, IOException {
    File targetFile = new File("target/topics.txt");
    targetFile.deleteOnExit();/* w  w w. jav  a  2  s .  com*/

    int expectedLines = 2;
    String expectedLine0Regex = "dummy1.txt\t(0\\.[0-9]{4},){9}0\\.[0-9]{4}";
    String expectedLine1Regex = "dummy2.txt\t(0\\.[0-9]{4},){9}0\\.[0-9]{4}";

    CollectionReaderDescription reader = createReaderDescription(TextReader.class,
            TextReader.PARAM_SOURCE_LOCATION, CAS_DIR, TextReader.PARAM_PATTERNS, CAS_FILE_PATTERN,
            TextReader.PARAM_LANGUAGE, LANGUAGE);
    AnalysisEngineDescription segmenter = createEngineDescription(BreakIteratorSegmenter.class);

    AnalysisEngineDescription inferencer = createEngineDescription(MalletTopicModelInferencer.class,
            MalletTopicModelInferencer.PARAM_MODEL_LOCATION, MODEL_FILE,
            MalletTopicModelInferencer.PARAM_USE_LEMMA, USE_LEMMAS);

    AnalysisEngineDescription writer = createEngineDescription(MalletTopicProportionsWriter.class,
            MalletTopicProportionsWriter.PARAM_TARGET_LOCATION, targetFile);

    SimplePipeline.runPipeline(reader, segmenter, inferencer, writer);
    List<String> lines = FileUtils.readLines(targetFile);
    assertTrue(lines.get(0).matches(expectedLine0Regex));
    assertTrue(lines.get(1).matches(expectedLine1Regex));
    assertEquals(expectedLines, lines.size());
}

From source file:eu.hydrologis.jgrass.featureeditor.xml.annotatedguis.AComboBoxGui.java

public Control makeGui(Composite parent) {
    combo = new Combo(parent, SWT.DROP_DOWN | SWT.READ_ONLY);
    combo.setLayoutData(aComboBox.constraints);

    if (cacheMap.size() > 10) {
        // remove first
        Set<String> keySet = cacheMap.keySet();
        Iterator<String> iterator = keySet.iterator();
        String key = iterator.next();
        cacheMap.remove(key);/*from   ww  w  . ja v  a2  s.co m*/
    }

    labelList = new ArrayList<String>();
    valuesList = new ArrayList<String>();

    List<String> itemList = aComboBox.item;
    if (itemList.size() == 1 && itemList.get(0).startsWith("file:")) {
        /*
         * case in which a file was linked in the form.
         */
        // need to read file
        String fileDef = itemList.get(0);
        String[][] lines = cacheMap.get(fileDef);
        if (lines == null) {
            /*
             * if we didn't cache the read formdata already
             */
            String[] split = fileDef.split(";");

            IMap activeMap = ApplicationGIS.getActiveMap();
            ILayer selectedLayer = activeMap.getEditManager().getSelectedLayer();
            ID id = selectedLayer.getGeoResource().getID();
            File parentFolder = new File(".");
            if (id.isFile()) {
                File tmpParent = id.toFile().getParentFile();
                parentFolder = tmpParent;
            }
            String fileName = split[0].replaceFirst("file:", "");
            int guiNameColumn = -1;
            try {
                guiNameColumn = Integer.parseInt(split[1]);
            } catch (Exception e) {
            }
            int attributeValueColumn = -1;
            try {
                attributeValueColumn = Integer.parseInt(split[2]);
            } catch (Exception e) {
            }
            String sep = split[3];

            File itemsFile = new File(parentFolder, fileName);
            if (itemsFile.exists()) {
                List readLines = new ArrayList();
                try {
                    readLines = FileUtils.readLines(itemsFile);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                lines = new String[readLines.size()][2];
                int index = 0;
                for (Object line : readLines) {
                    if (line instanceof String) {
                        String lineStr = (String) line;
                        String[] lineSplit = lineStr.split(sep);

                        if (guiNameColumn >= 0) {
                            String gnC = lineSplit[guiNameColumn].trim();
                            lines[index][0] = gnC;
                            labelList.add(gnC);
                        }
                        String svC = lineSplit[attributeValueColumn].trim();
                        lines[index][1] = svC;
                        valuesList.add(svC);
                        index++;
                    }
                }
                cacheMap.put(fileDef, lines);
            }
        } else {
            /*
             * use cached formdata
             */
            for (String[] items : lines) {
                if (items[0] != null) {
                    labelList.add(items[0]);
                } else {
                    labelList.add(items[1]);
                }
                valuesList.add(items[1]);
            }
        }
    } else {
        /*
         * the formdata are not filebound
         */
        for (String item : itemList) {
            if (item.indexOf(',') != -1) {
                String[] split = item.split(","); //$NON-NLS-1$
                labelList.add(split[0].trim());
                valuesList.add(split[1].trim());
            } else {
                labelList.add(item);
                valuesList.add(item);
            }
        }
    }

    String[] listArray = (String[]) labelList.toArray(new String[labelList.size()]);
    combo.setItems(listArray);
    if (aComboBox.defaultText != null) {
        int index = 0;
        for (int i = 0; i < valuesList.size(); i++) {
            String value = valuesList.get(i);
            if (value.equals(aComboBox.defaultText)) {
                index = i;
                break;
            }
        }
        combo.select(index);
    }
    combo.addSelectionListener(this);
    return combo;
}

From source file:de.tudarmstadt.ukp.dkpro.core.mallet.topicmodel.io.MalletTopicsProportionsSortedWriterTest.java

@Test
public void test() throws UIMAException, IOException {
    File targetFile = new File("target/topics.txt");
    targetFile.deleteOnExit();/*  w  w  w  .  j  a  va 2 s.  c om*/

    int expectedLines = 2;
    int nTopicsOutput = 3;
    String expectedLine0Regex = "dummy1.txt(\t[0-9]+:0\\.[0-9]{4}){" + nTopicsOutput + "}";
    String expectedLine1Regex = "dummy2.txt(\t[0-9]+:0\\.[0-9]{4}){" + nTopicsOutput + "}";

    CollectionReaderDescription reader = createReaderDescription(TextReader.class,
            TextReader.PARAM_SOURCE_LOCATION, CAS_DIR, TextReader.PARAM_PATTERNS, CAS_FILE_PATTERN,
            TextReader.PARAM_LANGUAGE, LANGUAGE);
    AnalysisEngineDescription segmenter = createEngineDescription(BreakIteratorSegmenter.class);

    AnalysisEngineDescription inferencer = createEngineDescription(MalletTopicModelInferencer.class,
            MalletTopicModelInferencer.PARAM_MODEL_LOCATION, MODEL_FILE,
            MalletTopicModelInferencer.PARAM_USE_LEMMA, USE_LEMMAS);

    AnalysisEngineDescription writer = createEngineDescription(MalletTopicsProportionsSortedWriter.class,
            MalletTopicsProportionsSortedWriter.PARAM_TARGET_LOCATION, targetFile,
            MalletTopicsProportionsSortedWriter.PARAM_N_TOPICS, nTopicsOutput);

    SimplePipeline.runPipeline(reader, segmenter, inferencer, writer);
    List<String> lines = FileUtils.readLines(targetFile);
    assertTrue(lines.get(0).matches(expectedLine0Regex));
    assertTrue(lines.get(1).matches(expectedLine1Regex));
    assertEquals(expectedLines, lines.size());

    /* assert first field */
    lines.stream().map(line -> line.split("\t")).forEach(fields -> assertTrue(fields[0].startsWith("dummy")));
}

From source file:com.github.ipaas.ideploy.agent.handler.RollbackCodeHandlerTest.java

/**
 * , USING_VERSION,? ROLLBACKTO_VERSION 
 * // ww  w  . j  a  va2  s. c  o m
 * @throws Exception
 */
@Test
public void rollbackCodeTest() throws Exception {
    String deployRoot = "/www/app" + APP_NAME;

    File usingfile = new File(deployRoot);

    RollbackCodeHandler rollback = new RollbackCodeHandler();

    String localBkPath = "/www/appbk" + APP_NAME + "/firstVer0";
    Map<String, Object> cantRollBackParams = new HashMap<String, Object>();
    cantRollBackParams.put("localBkPath", localBkPath);
    cantRollBackParams.put("deployPath", deployRoot);
    try {
        rollback.execute(null, null, cantRollBackParams, null); // :
        // ,?,?!
        fail("Created fraction 1/0! That's undefined!");
    } catch (Exception e) {
        assertEquals("?,?!", e.getMessage());
    }
    File localBkFile = new File(localBkPath);
    if (!localBkFile.exists()) {
        localBkFile.mkdirs();
    }
    rollback.execute(null, null, cantRollBackParams, null);

    Integer hostStatus4New = 1;
    String savePath = "/www/apptemp/" + USING_VERSION;
    Map<String, Object> firstRollbackParams = new HashMap<String, Object>();
    firstRollbackParams.put("usingCodeVerPath", TEST_GROUP + TAGS + "/" + USING_VERSION);
    firstRollbackParams.put("hostStatus", hostStatus4New);
    firstRollbackParams.put("savePath", savePath);
    firstRollbackParams.put("deployPath", deployRoot);
    rollback.execute(null, null, firstRollbackParams, null); // ,,?

    DownloadCodeHandler downLoadHandler = new DownloadCodeHandler();
    Integer notUpdateAll = 2;
    Integer hostStatus4Old = 2;
    Map<String, Object> secondDownLoadParams = new HashMap<String, Object>();
    secondDownLoadParams.put("doingCodeVerPath", TEST_GROUP + TAGS + "/" + ROLLBACKTO_VERSION);
    secondDownLoadParams.put("usingCodeVerPath", TEST_GROUP + TAGS + "/" + USING_VERSION);
    secondDownLoadParams.put("hostStatus", hostStatus4Old);
    secondDownLoadParams.put("savePath", "/www/apptemp/rollback_test_vaild");// ??
    secondDownLoadParams.put("updateAll", notUpdateAll);
    downLoadHandler.execute(null, null, secondDownLoadParams, null);
    File updateFile = new File("/www/apptemp/rollback_test_vaild/update.txt");
    List<String> updateList = FileUtils.readLines(updateFile);// ??,?

    String savePath2 = "/www/apptemp/" + ROLLBACKTO_VERSION;
    Map<String, Object> secondRollbackParams = new HashMap<String, Object>();
    secondRollbackParams.put("usingCodeVerPath", TEST_GROUP + TAGS + "/" + ROLLBACKTO_VERSION);
    secondRollbackParams.put("doingCodeVerPath", TEST_GROUP + TAGS + "/" + USING_VERSION);
    secondRollbackParams.put("hostStatus", hostStatus4Old);
    secondRollbackParams.put("savePath", savePath2);
    secondRollbackParams.put("deployPath", deployRoot);
    rollback.execute(null, null, secondRollbackParams, null); // ,

    // ?
    for (String str : updateList) {
        if (str.startsWith("+")) {
            // 
            Assert.assertTrue(new File(deployRoot + StringUtils.removeStart(str, "+").trim()).exists());
        } else if (str.startsWith("-")) {
            // 
            String f = deployRoot + StringUtils.removeStart(str, "-").trim();
            Assert.assertFalse(new File(f).exists());
        }
    }

    FileUtils.cleanDirectory(usingfile);
    FileUtils.forceDelete(localBkFile);
    FileUtils.cleanDirectory(new File("/www/apptemp"));
}