Java Code Examples for org.apache.commons.io.FileUtils#readLines()

The following examples show how to use org.apache.commons.io.FileUtils#readLines() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RestTemplateTest.java    From sofa-tracer with Apache License 2.0 6 votes vote down vote up
public void testGetFromEntity() throws IOException, InterruptedException {
    RestTemplate restTemplate = SofaTracerRestTemplateBuilder.buildRestTemplate();
    ResponseEntity<Map> forEntity = restTemplate.getForEntity(urlHttpPrefix, Map.class);
    Assert.assertTrue(forEntity.getBody().containsKey("name"));
    Thread.sleep(1000);
    //wait for async output
    List<String> contents = FileUtils.readLines(new File(
        logDirectoryPath + File.separator
                + RestTemplateLogEnum.REST_TEMPLATE_DIGEST.getDefaultLogName()));
    assertTrue(contents.size() == 1);

    //stat log
    List<String> statContents = FileUtils.readLines(new File(
        logDirectoryPath + File.separator
                + RestTemplateLogEnum.REST_TEMPLATE_STAT.getDefaultLogName()));
    assertTrue(statContents.size() == 1);
}
 
Example 2
Source File: HyperParameterScopeConfigReader.java    From laozhongyi with MIT License 6 votes vote down vote up
public static List<HyperParameterScopeItem> read(final String configFilePath) {
    final File file = new File(configFilePath);
    List<String> lines;
    try {
        lines = FileUtils.readLines(file, Charsets.UTF_8);
    } catch (final IOException e) {
        throw new IllegalStateException(e);
    }
    final List<HyperParameterScopeItem> result = Lists.newArrayList();
    for (final String line : lines) {
        final String[] segments = StringUtils.split(line, ',');
        final List<String> values = Lists.newArrayList();
        for (int i = 1; i < segments.length; ++i) {
            values.add(segments[i]);
        }
        final HyperParameterScopeItem item = new HyperParameterScopeItem(segments[0], values);
        result.add(item);
    }
    return result;
}
 
Example 3
Source File: JATEUtil.java    From jate with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * load ACL RD-TEC documents from raw text corpus
 *
 * @param rawTxtFile raw text file
 * @return JATEDocument
 */
public static JATEDocument loadACLRDTECDocumentFromRaw(File rawTxtFile) {
    JATEDocument jateDocument = new JATEDocument(rawTxtFile.toURI());
    jateDocument.setId(rawTxtFile.getName());

    StringBuilder rawTextBuffer = new StringBuilder();
    try {
        List<String> lines = FileUtils.readLines(rawTxtFile, Charset.forName("utf8"));
        if (lines.size() > 0) {
            lines.forEach(rawTextBuffer::append);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

    jateDocument.setContent(rawTextBuffer.toString());
    return jateDocument;
}
 
Example 4
Source File: ServletUtil-TEMPLATE.java    From steam with GNU Affero General Public License v3.0 6 votes vote down vote up
public static synchronized void loadModels(File servletPath) {
  if (modelNames == null) {
    try {
      modelNames = FileUtils.readLines(new File(servletPath, "modelnames.txt"));
      logger.info("modelNames size {}", modelNames.size());
      models = new HashMap<String, EasyPredictModelWrapper>();
      EasyPredictModelWrapper mod = null;
      for (String m : modelNames) {
        if (m.endsWith(".java"))
          mod = addPojoModel(m.replace(".java", ""));
        else if (m.endsWith(".zip"))
          mod = addMojoModel(m.replace(".zip", ""), servletPath);
        if (mod == null)
          throw new Exception("Failed to load model "  + m);
        if (model == null)
          model = mod;
      }
      logger.info("added {} models", models.size());
    }
    catch (Exception e) {
      logger.error("can't load model using modelnames.txt", e);
    }
  }
  else
    logger.debug("models already loaded");
}
 
Example 5
Source File: HttpClientTracerTest.java    From sofa-tracer with Apache License 2.0 6 votes vote down vote up
private void testHttpClientGet(int expectedLength) throws Exception {
    String httpGetUrl = urlHttpPrefix;
    String path = "/httpclient";
    String responseStr = new HttpClientInstance(10 * 1000).executeGet(httpGetUrl + path);
    assertFalse(StringUtils.isBlank(responseStr));

    TestUtil.waitForAsyncLog();

    //wait for async output
    List<String> contents = FileUtils
        .readLines(customFileLog(HttpClientLogEnum.HTTP_CLIENT_DIGEST.getDefaultLogName()));
    assertTrue(contents.size() == expectedLength);

    // stat log print cycle: 1s
    Thread.sleep(1000);

    //stat log
    List<String> statContents = FileUtils
        .readLines(customFileLog(HttpClientLogEnum.HTTP_CLIENT_STAT.getDefaultLogName()));
    assertTrue(statContents.size() == expectedLength);
}
 
Example 6
Source File: NonJsonTest.java    From sofa-tracer with Apache License 2.0 6 votes vote down vote up
private void testHttpClientHead(int expectedLength) throws Exception {
    String httpHeadUrl = urlHttpPrefix;
    String path = "/httpclient";
    String responseStr = new HttpClientInstance(10 * 1000).executeHead(httpHeadUrl + path);
    assertTrue(StringUtils.isBlank(responseStr));

    TestUtil.waitForAsyncLog();

    //wait for async output
    List<String> contents = FileUtils
        .readLines(customFileLog(HttpClientLogEnum.HTTP_CLIENT_DIGEST.getDefaultLogName()));
    assertTrue(contents.size() == expectedLength);

    // stat log print cycle: 1s
    Thread.sleep(1000);

    //stat log
    List<String> statContents = FileUtils
        .readLines(customFileLog(HttpClientLogEnum.HTTP_CLIENT_STAT.getDefaultLogName()));
    assertTrue(statContents.size() == expectedLength);
}
 
Example 7
Source File: RocksDbHdfsState.java    From jstorm with Apache License 2.0 5 votes vote down vote up
@Override
public void restore(String checkpointBackupDir) {
    LOG.info("Start restore from remote: {}", checkpointBackupDir);

    if (rocksDb != null)
        rocksDb.dispose();
    initLocalRocksDbDir();
    // Restore db files from hdfs to local disk
    try {
        if (checkpointBackupDir != null) {
            // Get dir of sst files
            int index = checkpointBackupDir.lastIndexOf("checkpoint");
            String remoteDbBackupDir = checkpointBackupDir.substring(0, index);

            // copy sstFile.list, CURRENT, MANIFEST to local disk for the specified batch
            Collection<String> files = hdfsCache.listFile(checkpointBackupDir, false);
            LOG.debug("Restore checkpoint files: {}", files);
            for (String fileName : files)
                hdfsCache.copyToLocal(checkpointBackupDir + "/" + fileName, rocksDbDir);

            // copy all rocksDB sst files to local disk
            String sstFileList = rocksDbDir + "/" + SST_FILE_LIST;
            File file = new File(sstFileList);
            List<String> sstFiles = FileUtils.readLines(file);
            LOG.debug("Restore sst files: {}", sstFiles);
            for (String sstFile : sstFiles) {
                hdfsCache.copyToLocal(remoteDbBackupDir + "/" + sstFile, rocksDbDir);
            }
            FileUtils.deleteQuietly(file);
        }
        initRocksDb();
    } catch (IOException e) {
        LOG.error("Failed to restore checkpoint", e);
        throw new RuntimeException(e.getMessage());
    }
}
 
Example 8
Source File: TestCaseCreator.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * The entry point.
 *
 * @param args the arguments
 * @throws IOException if an error occurs
 */
public static void main(final String[] args) throws IOException {
    if (args.length == 0) {
        System.out.println("HTML file location is not provided");
        return;
    }

    final File file = new File(args[0]);
    if (!file.exists()) {
        System.out.println("File does not exist " + file.getAbsolutePath());
    }

    System.out.println("        /**");
    System.out.println("         * @throws Exception if an error occurs");
    System.out.println("         */");
    System.out.println("        @Test");
    System.out.println("        @Alerts()");
    System.out.println("        public void test() throws Exception {");

    final List<String> lines = FileUtils.readLines(file, ISO_8859_1);
    for (int i = 0; i < lines.size(); i++) {
        final String line = lines.get(i);
        if (i == 0) {
            System.out.println("            final String html = \"" + line.replace("\"", "\\\"") + "\\n\"");
        }
        else {
            System.out.print("                + \"" + line.replace("\"", "\\\"") + "\\n\"");
            if (i == lines.size() - 1) {
                System.out.print(";");
            }
            System.out.println();
        }
    }
    System.out.println("            loadPageWithAlerts2(html);");
    System.out.println("        }");
}
 
Example 9
Source File: ATEResultLoader.java    From jate with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Deprecated
public static List<List<String>> loadJATE1(String jate1outputfile) throws IOException {
    List<String> lines = FileUtils.readLines(new File(jate1outputfile));
    List<List<String>> out = new ArrayList<>(lines.size());
    for(String l : lines){
        String terms = l.split("\t\t\t")[0];
        List<String> variants = new ArrayList<>();
        for(String t: terms.split("\\|")){
            variants.add(t.trim());
        }
        out.add(variants);
    }
    return out;
}
 
Example 10
Source File: SynchronizingSelfLogTest.java    From sofa-tracer with Apache License 2.0 5 votes vote down vote up
@Test
public void info() throws IOException {
    SynchronizingSelfLog.info("test info");
    SynchronizingSelfLog.flush();
    File log = customFileLog("sync.log");
    List<String> logs = FileUtils.readLines(log);
    assertTrue(logs.toString(), logs.get(0).contains("[INFO]"));
    assertTrue(logs.toString(), logs.get(0).contains("test info"));
}
 
Example 11
Source File: TaskUpdateWebpackTest.java    From flow with Apache License 2.0 5 votes vote down vote up
@Test
public void should_updateOnlyGeneratedWebpack() throws Exception {
    Assert.assertFalse("No webpack config file should be present.",
            webpackConfig.exists());
    Assert.assertFalse("No generated config file should be present.",
            webpackGenerated.exists());

    webpackUpdater.execute();
    Assert.assertTrue("webpack.config.js was not created.",
            webpackConfig.exists());
    Assert.assertTrue("webpack.generated.js was not created.",
            webpackGenerated.exists());
    assertWebpackConfigContent();

    // Add a custom line into webpack.config.js
    String customString = "custom element;";
    List<String> lines = FileUtils.readLines(webpackConfig, "UTF-8");
    for (int i = 0; i < lines.size(); i++) {
        if (lines.get(i).equals("module.exports = merge(flowDefaults,")) {
            lines.add(i + 1, customString);
            break;
        }
    }

    FileUtils.writeLines(webpackConfig, lines);

    TaskUpdateWebpack newUpdater = new TaskUpdateWebpack(frontendFolder,
            baseDir, new File(baseDir, "foo"), WEBPACK_CONFIG,
            WEBPACK_GENERATED, new File(baseDir, "bar"), false);
    newUpdater.execute();

    assertWebpackGeneratedConfigContent("bar", "foo");
    List<String> webpackContents = Files.lines(webpackConfig.toPath())
            .collect(Collectors.toList());

    Assert.assertTrue("Custom string has disappeared",
            webpackContents.contains(customString));

}
 
Example 12
Source File: DatabaseHelper.java    From smart-framework with Apache License 2.0 5 votes vote down vote up
/**
 * 初始化 SQL 脚本
 */
public static void initSQL(String sqlPath) {
    try {
        File sqlFile = new File(ClassUtil.getClassPath() + sqlPath);
        List<String> sqlList = FileUtils.readLines(sqlFile);
        for (String sql : sqlList) {
            update(sql);
        }
    } catch (Exception e) {
        logger.error("初始化 SQL 脚本出错!", e);
        throw new RuntimeException(e);
    }
}
 
Example 13
Source File: AutoWordBagInstance.java    From SnowGraph with Apache License 2.0 5 votes vote down vote up
private static void mark() throws IOException {
	List<String> lines = FileUtils.readLines(new File(semanticTitlePath));
	List<String> r = new ArrayList<>();
	for (String line : lines) {
		if (line.endsWith("T"))
			return;
		String[] eles = line.split("\\s+");
		if (Integer.parseInt(eles[1]) > 24)
			r.add(line + " T");
		else
			r.add(line);
	}
	FileUtils.writeLines(new File(semanticTitlePath), r);
}
 
Example 14
Source File: SelfLogTest.java    From sofa-tracer with Apache License 2.0 5 votes vote down vote up
/**
 * Method: error(String log, Throwable e)
 */
@Test
public void testErrorForLogE() throws Exception {
    SelfLog.error("Error info", new RuntimeException("RunTimeException"));

    TestUtil.waitForAsyncLog();

    List<String> logs = FileUtils.readLines(tracerSelfLog());
    assertTrue(!logs.isEmpty());
}
 
Example 15
Source File: FileUtil.java    From bbs with GNU Affero General Public License v3.0 5 votes vote down vote up
/**  
  * 读文件
  * @param path 路径
  * @param encoding 编码  UTF-8
  * @return 文本字符串
  */   
  public static List<String> readLines(File file,String encoding){ 
  	try {
	return FileUtils.readLines(file, encoding);
} catch (IOException e) {
	// TODO Auto-generated catch block
//	e.printStackTrace();
	if (logger.isErrorEnabled()) {
         logger.error("读文件",e);
     }
}
return null;
  }
 
Example 16
Source File: FileMetaDir.java    From jackrabbit-filevault with Apache License 2.0 5 votes vote down vote up
public String getRepositoryUrl() throws IOException {
    File mpFile = new File(dir, ADDRESS_FILE_NAME);
    if (!mpFile.canRead()) {
        return null;
    }
    List lines = FileUtils.readLines(mpFile, Constants.ENCODING);
    if (lines.isEmpty()) {
        throw new IOException(mpFile.getPath() + " is empty.");
    }
    return (String) lines.get(0);
}
 
Example 17
Source File: SpringMvcSofaTracerFilterTest.java    From sofa-tracer with Apache License 2.0 5 votes vote down vote up
/**
 * Test that exception information is not printed to tracer-self.log when an exception occurs during the execution of the business Filter chain
 * @throws Exception
 */
@Test
public void testFilterFail() throws Exception {

    checkFile();

    final SpringMvcSofaTracerFilter filter = new SpringMvcSofaTracerFilter();
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();

    FilterChain mockFilterChain = new FilterChain() {
        public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse)
                                                                                            throws IOException,
                                                                                            ServletException {
            throw new RuntimeException("Has got exception...");
        }

        public void init(FilterConfig theConfig) {
        }

        public void destroy() {
        }
    };

    try {
        filter.doFilter(request, response, mockFilterChain);
    } catch (Exception e) {
        String message = e.getMessage();
        Assert.assertTrue(message.contains("Has got exception..."));
    }
    Thread.sleep(500);
    //wait for async output
    File file = new File(logDirectoryPath + File.separator + "tracer-self.log");
    if (file.exists()) {
        List<String> result = FileUtils.readLines(new File(logDirectoryPath + File.separator
                                                           + "tracer-self.log"));
        Assert.assertTrue(result.size() == 1);
    }
    Assert.assertTrue(!file.exists());
}
 
Example 18
Source File: FeatureReaderImpl.java    From IridiumApplicationTesting with MIT License 5 votes vote down vote up
@Override
public boolean selectFile(@NotNull final File file, @NotNull final String app) {
	checkNotNull(file);
	checkArgument(StringUtils.isNotBlank(app));

	boolean isEnabled = true;
	boolean matchesFeatureGroup = false;

	try {
		final List<String> lines = FileUtils.readLines(file);

		for (final String line : lines) {
			if (Constants.COMMENT_LINE.matcher(line).find()) {
				final Matcher featureGroup = Constants.FEATURE_GROUP_HEADER.matcher(line);
				if (featureGroup.find()) {
					matchesFeatureGroup = matchesFeatureGroup(
						featureGroup.group("value"),
						app);
				}

				final Matcher enabled = Constants.ENABLED_HEADER.matcher(line);
				if (enabled.find()) {
					isEnabled = Boolean.parseBoolean(enabled.group("value"));
				}
			} else {
				/*
					We have found a line that is not a comment, so stop processing the file
				 */
				break;
			}
		}
	} catch (final IOException ex) {
		LOGGER.error("Could not read file.", ex);
	}

	return isEnabled && matchesFeatureGroup;
}
 
Example 19
Source File: PureStyleSQLApplicationTest.java    From attic-apex-malhar with Apache License 2.0 4 votes vote down vote up
@Test
public void test() throws Exception
{
  LocalMode lma = LocalMode.newInstance();
  Configuration conf = new Configuration(false);
  conf.addResource(this.getClass().getResourceAsStream("/META-INF/properties.xml"));
  conf.addResource(this.getClass().getResourceAsStream("/META-INF/properties-PureStyleSQLApplication.xml"));

  conf.set("broker", kafka.getBroker());
  conf.set("topic", testTopicData);
  conf.set("outputFolder", outputFolder);
  conf.set("destFileName", "out.tmp");

  PureStyleSQLApplication app = new PureStyleSQLApplication();

  lma.prepareDAG(app, conf);

  LocalMode.Controller lc = lma.getController();

  lc.runAsync();
  kafka.publish(testTopicData, Arrays.asList(
      "15/02/2016 10:15:00 +0000,1,paint1,11",
      "15/02/2016 10:16:00 +0000,2,paint2,12",
      "15/02/2016 10:17:00 +0000,3,paint3,13",
      "15/02/2016 10:18:00 +0000,4,paint4,14",
      "15/02/2016 10:19:00 +0000,5,paint5,15",
      "15/02/2016 10:10:00 +0000,6,abcde6,16"));

  Assert.assertTrue(waitTillFileIsPopulated(outputFolder, 40000));
  lc.shutdown();

  File file = new File(outputFolder);
  File file1 = new File(outputFolder + file.list()[0]);
  List<String> strings = FileUtils.readLines(file1);

  String[] actualLines = strings.toArray(new String[strings.size()]);

  String[] expectedLines = new String[]{
      "15/02/2016 10:18:00 +0000,15/02/2016 12:00:00 +0000,OILPAINT4",
      "",
      "15/02/2016 10:19:00 +0000,15/02/2016 12:00:00 +0000,OILPAINT5",
      ""};

  Assert.assertEquals(expectedLines.length, actualLines.length);
  for (int i = 0;i < expectedLines.length; i++) {
    Assert.assertEquals(expectedLines[i], actualLines[i]);
  }
}
 
Example 20
Source File: StatsProcess.java    From RAMPART with GNU General Public License v3.0 3 votes vote down vote up
private void createDistinctStatsFile(List<File> statsFiles, File outputFile) throws IOException {

        List<String> lines = new ArrayList<>();

        lines.add("filename\tmin\tmax\tmean\tvariance\tstddev");

        for(File file : statsFiles) {

            long min = Long.MAX_VALUE;
            long max = 0;
            long sum = 0;
            long sum2 = 0;

            List<String> statsLines = FileUtils.readLines(file);

            for(int i = 1; i < statsLines.size(); i++) {

                String line = statsLines.get(i);

                String[] parts = line.split("\t");

                long distinctVal = Long.parseLong(parts[2]);

                min = distinctVal < min ? distinctVal : min;
                max = distinctVal > max ? distinctVal : max;

                sum += distinctVal;
                sum2 += distinctVal * distinctVal;
            }

            int entries = statsLines.size() - 1;
            double mean = (double)sum / (double)entries;
            double var = ((sum*sum) - sum2)/(double)entries;
            double stddev = Math.sqrt(var);

            lines.add(file.getName() + "\t" + min + "\t" + max + "\t" + mean + "\t" + var + "\t" + stddev);
        }

        FileUtils.writeLines(outputFile, lines);
    }