List of usage examples for org.apache.commons.io FileUtils writeStringToFile
public static void writeStringToFile(File file, String data, String encoding) throws IOException
From source file:edu.illinois.cs.cogcomp.wikifier.utils.freebase.FreebaseSearch.java
public List<FreebaseAnswer> lookup(String query) throws Exception { // First, try the cache. String checksum = QueryMQL.getMD5Checksum(query); if (IOUtils.exists(cacheLocation + "/" + checksum + ".cached")) { // System.out.println("Found!"); return parseJson( FileUtils.readFileToString(new File(cacheLocation + "/" + checksum + ".cached"), "UTF-8")); } else {/*from w w w .ja v a 2s . c o m*/ System.out.println("Caching"); String tmp = getQueryResponse(query); FileUtils.writeStringToFile(new File(cacheLocation + "/" + checksum + ".cached"), tmp, "UTF-8"); return parseJson(tmp); } }
From source file:jp.igapyon.selecrawler.SeleCrawlerWebContentNormalizer.java
public void processFile(final File file) throws IOException { String contents = FileUtils.readFileToString(file, "UTF-8"); contents = SimpleHtmlNormalizerUtil.normalizeHtml(contents); final File fileNormalized = new File(file.getParentFile(), file.getName() + SeleCrawlerConstants.EXT_SC_NORMAL); FileUtils.writeStringToFile(fileNormalized, contents, "UTF-8"); }
From source file:it.incipict.tool.profiles.ProfilesToolTest.java
@Test public void scanAllSurveys() { try {/*from w w w. j a v a 2 s . c om*/ for (File profileTypeKey : FileUtils.listFilesAndDirs(new File(TEST_RESOURCES), FalseFileFilter.FALSE, TrueFileFilter.TRUE)) { if (!profileTypeKey.getName().equals((new File(TEST_RESOURCES)).getName())) { StringBuilder report = new StringBuilder(); for (File survey : FileUtils.listFiles(new File(profileTypeKey + File.separator), extension, false)) { report.append("Analyzing " + survey.getAbsolutePath() + "\n"); Map<ProfileType, Double> checkProfile = profileTool.checkProfiles(survey); ProfileType bestProfile = ProfilesToolUtils.getBestProfile(checkProfile); Assert.assertEquals(profileTypeKey.getName(), bestProfile.getType()); report.append(ProfilesToolTestUtils.printDistancesToString(checkProfile) + "\n\n"); } FileUtils.writeStringToFile(new File(profileTypeKey + File.separator + "result.txt"), report.toString(), Charset.forName("ISO-8859-1")); } } Assert.assertTrue(true); } catch (ProfilesToolException | IOException e) { logger.error("error: ", e); Assert.assertTrue(false); } }
From source file:de.tudarmstadt.ukp.experiments.dip.wp1.documents.Step7CollectMTurkResults.java
public static void main(String[] args) throws Exception { // input dir - list of xml query containers // /home/user-ukp/research/data/dip/wp1-documents/step4-boiler-plate/ File inputDir = new File(args[0] + "/"); // MTurk result file // output dir File outputDir = new File(args[2]); if (!outputDir.exists()) { outputDir.mkdirs();// w w w .j a va 2s .c o m } // Folder with success files File mturkSuccessDir = new File(args[1]); Collection<File> files = FileUtils.listFiles(mturkSuccessDir, new String[] { "result" }, false); if (files.isEmpty()) { throw new IllegalArgumentException("Input folder is empty. " + mturkSuccessDir); } HashMap<String, List<MTurkAnnotation>> mturkAnnotations = new HashMap<>(); // parsing all CSV files for (File mturkCSVResultFile : files) { System.out.println("Parsing " + mturkCSVResultFile.getName()); MTurkOutputReader outputReader = new MTurkOutputReader( new HashSet<>(Arrays.asList("annotation", "workerid")), mturkCSVResultFile); // for fixing broken data input: for each hit, collect all sentence IDs Map<String, SortedSet<String>> hitSentences = new HashMap<>(); // first iteration: collect the sentences for (Map<String, String> record : outputReader) { String hitID = record.get("hitid"); if (!hitSentences.containsKey(hitID)) { hitSentences.put(hitID, new TreeSet<>()); } String relevantSentences = record.get("Answer.relevant_sentences"); String irrelevantSentences = record.get("Answer.irrelevant_sentences"); if (relevantSentences != null) { hitSentences.get(hitID).addAll(Arrays.asList(relevantSentences.split(","))); } if (irrelevantSentences != null) { hitSentences.get(hitID).addAll(Arrays.asList(irrelevantSentences.split(","))); } } // and now second iteration for (Map<String, String> record : outputReader) { String hitID = record.get("hitid"); String annotatorID = record.get("workerid"); String acceptTime = record.get("assignmentaccepttime"); String submitTime = record.get("assignmentsubmittime"); String relevantSentences = record.get("Answer.relevant_sentences"); String irrelevantSentences = record.get("Answer.irrelevant_sentences"); String reject = record.get("reject"); String filename[]; String comment; String clueWeb; String[] relevant = {}; String[] irrelevant = {}; filename = record.get("annotation").split("_"); String fileXml = filename[0]; clueWeb = filename[1].trim(); comment = record.get("Answer.comment"); if (relevantSentences != null) { relevant = relevantSentences.split(","); } if (irrelevantSentences != null) { irrelevant = irrelevantSentences.split(","); } // sanitizing data: if both relevant and irrelevant are empty, that's a bug // we're gonna look up all sentences from this HIT and treat this assignment // as if there were only irrelevant ones if (relevant.length == 0 && irrelevant.length == 0) { SortedSet<String> strings = hitSentences.get(hitID); irrelevant = new String[strings.size()]; strings.toArray(irrelevant); } if (reject != null) { System.out.println(" HIT " + hitID + " annotated by " + annotatorID + " was rejected "); } else { /* // relevant sentences is a comma-delimited string, // this regular expression is rather strange // it must contain digits, it might be that there is only one space or a comma or some other char // digits are the sentence ids. if relevant sentences do not contain digits then it is wrong if (relevantSentences.matches("^\\D*$") && irrelevantSentences.matches("^\\D*$")) { try { throw new IllegalStateException( "No annotations found for HIT " + hitID + " in " + fileXml + " for document " + clueWeb); } catch (IllegalStateException ex) { ex.printStackTrace(); } } */ MTurkAnnotation mturkAnnotation; try { mturkAnnotation = new MTurkAnnotation(hitID, annotatorID, acceptTime, submitTime, comment, clueWeb, relevant, irrelevant); } catch (IllegalArgumentException ex) { throw new IllegalArgumentException("Record: " + record, ex); } List<MTurkAnnotation> listOfAnnotations = mturkAnnotations.get(fileXml); if (listOfAnnotations == null) { listOfAnnotations = new ArrayList<>(); } listOfAnnotations.add(mturkAnnotation); mturkAnnotations.put(fileXml, listOfAnnotations); } } // parser.close(); } // Debugging: output number of HITs of a query System.out.println("Accepted HITs for a query:"); for (Map.Entry e : mturkAnnotations.entrySet()) { ArrayList<MTurkAnnotation> a = (ArrayList<MTurkAnnotation>) e.getValue(); System.out.println(e.getKey() + " " + a.size()); } for (File f : FileUtils.listFiles(inputDir, new String[] { "xml" }, false)) { QueryResultContainer queryResultContainer = QueryResultContainer .fromXML(FileUtils.readFileToString(f, "utf-8")); String fileName = f.getName(); List<MTurkAnnotation> listOfAnnotations = mturkAnnotations.get(fileName); if (listOfAnnotations == null || listOfAnnotations.isEmpty()) { throw new IllegalStateException("No annotations for " + f.getName()); } for (QueryResultContainer.SingleRankedResult rankedResults : queryResultContainer.rankedResults) { for (MTurkAnnotation mtAnnotation : listOfAnnotations) { String clueWeb = mtAnnotation.clueWeb; if (rankedResults.clueWebID.equals(clueWeb)) { List<QueryResultContainer.MTurkRelevanceVote> mTurkRelevanceVotes = rankedResults.mTurkRelevanceVotes; QueryResultContainer.MTurkRelevanceVote relevanceVote = new QueryResultContainer.MTurkRelevanceVote(); String annotatorID = mtAnnotation.annotatorID; String hitID = mtAnnotation.hitID; String acceptTime = mtAnnotation.acceptTime; String submitTime = mtAnnotation.submitTime; String comment = mtAnnotation.comment; String[] relevant = mtAnnotation.relevant; String[] irrelevant = mtAnnotation.irrelevant; relevanceVote.turkID = annotatorID.trim(); relevanceVote.hitID = hitID.trim(); relevanceVote.acceptTime = acceptTime.trim(); relevanceVote.submitTime = submitTime.trim(); relevanceVote.comment = comment != null ? comment.trim() : null; if (relevant.length == 0 && irrelevant.length == 0) { try { throw new IllegalStateException("the length of the annotations is 0" + rankedResults.clueWebID + " for HIT " + relevanceVote.hitID); } catch (IllegalStateException e) { e.printStackTrace(); } } for (String r : relevant) { String sentenceId = r.trim(); if (!sentenceId.isEmpty() && sentenceId.matches("\\d+")) { QueryResultContainer.SingleSentenceRelevanceVote singleSentenceVote = new QueryResultContainer.SingleSentenceRelevanceVote(); singleSentenceVote.sentenceID = sentenceId; singleSentenceVote.relevant = "true"; relevanceVote.singleSentenceRelevanceVotes.add(singleSentenceVote); } } for (String r : irrelevant) { String sentenceId = r.trim(); if (!sentenceId.isEmpty() && sentenceId.matches("\\d+")) { QueryResultContainer.SingleSentenceRelevanceVote singleSentenceVote = new QueryResultContainer.SingleSentenceRelevanceVote(); singleSentenceVote.sentenceID = sentenceId; singleSentenceVote.relevant = "false"; relevanceVote.singleSentenceRelevanceVotes.add(singleSentenceVote); } } mTurkRelevanceVotes.add(relevanceVote); } } } File outputFile = new File(outputDir, f.getName()); FileUtils.writeStringToFile(outputFile, queryResultContainer.toXML(), "utf-8"); System.out.println("Finished " + outputFile); } }
From source file:koper.demo.dataevent.listener.OrderListener.java
private void writeFile(File file) { try {/*from ww w .ja v a 2 s . c om*/ long currTs = System.currentTimeMillis(); long period = currTs - lastTs.getAndSet(currTs); long sumReceive = sumReceiveTime.getAndSet(0); long sumConsume = sumConsumeTime.getAndSet(0); String record = String.format( "TimeStamp = %d, Count = %d, MillSeconds = %d, TPS = %.2f, AvgReceive = %d, AvgConsume = %d\n", currTs, msgCount.get(), period, WRITE_PERIOD / ((double) period / 1000), sumReceive / WRITE_PERIOD, sumConsume / WRITE_PERIOD); FileUtils.writeStringToFile(file, record, true); } catch (Exception e) { e.printStackTrace(); } }
From source file:com.gargoylesoftware.htmlunit.libraries.Sarissa0993Test.java
/** * @throws Exception if an error occurs/* w ww . j a va2s. c om*/ */ @Before public void init() throws Exception { try { getBrowserVersion(); } catch (final Exception e) { return; } startWebServer("src/test/resources/libraries/sarissa/" + getVersion()); if (Page_ == null) { final WebClient client = getWebClient(); final String url = "http://localhost:" + PORT + "/test/testsarissa.html"; Page_ = client.getPage(url); Page_.<HtmlButton>getFirstByXPath("//button").click(); // dump the result page if (System.getProperty(PROPERTY_GENERATE_TESTPAGES) != null) { final File tmpDir = new File(System.getProperty("java.io.tmpdir")); final File f = new File(tmpDir, "sarissa0993_result.html"); FileUtils.writeStringToFile(f, Page_.asXml(), "UTF-8"); LOG.info("Test result written to: " + f.getAbsolutePath()); } } }
From source file:com.thoughtworks.go.server.service.support.toggle.FeatureToggleRepositoryTest.java
@Test public void shouldNotFailWhenContentOfAvailableTogglesFileIsInvalid() throws Exception { setupAvailableToggleFileAs(TEST_AVAILABLE_TOGGLES_PATH); FileUtils.writeStringToFile(availableTogglesFile(), "SOME-INVALID-CONTENT", UTF_8); FeatureToggleRepository repository = new FeatureToggleRepository(environment); assertThat(repository.availableToggles(), is(new FeatureToggles())); }
From source file:com.lucid.solr.sidecar.SidecarIndexReaderFactoryTest.java
private CoreContainer init() throws Exception { if (solrHomeDirectory.exists()) { FileUtils.deleteDirectory(solrHomeDirectory); }//from w w w. j a va 2s.co m assertTrue("Failed to mkdirs workDir", solrHomeDirectory.mkdirs()); copyMinConf(new File(solrHomeDirectory, "source")); File target = new File(solrHomeDirectory, "target"); copyMinConf(target); // modify the target to add SidecarIndexReaderFactory File targetSolrConf = new File(target, "conf" + File.separator + "solrconfig.xml"); String xml = FileUtils.readFileToString(targetSolrConf, "UTF-8"); xml = xml.replaceFirst("<!-- EDIT_SIDECAR_START", ""); xml = xml.replaceFirst("EDIT_SIDECAR_END -->", ""); FileUtils.writeStringToFile(targetSolrConf, xml, "UTF-8"); File solrXml = new File(solrHomeDirectory, "solr.xml"); FileUtils.write(solrXml, SOLR_XML, IOUtils.CHARSET_UTF_8.toString()); final CoreContainer cores = new CoreContainer(solrHomeDirectory.getAbsolutePath()); cores.load(); //cores.setPersistent(false); return cores; }
From source file:com.thoughtworks.go.helper.SvnRemoteRepository.java
private void enableAuthentication() throws IOException { File confFile = new File(repo.projectRepositoryRoot(), "conf/svnserve.conf"); String passwd = "[general]\n" + "anon-access = none\n" + "auth-access = read\n" + "auth-access = write\n" + "password-db = passwd\n"; FileUtils.writeStringToFile(confFile, passwd, UTF_8); }
From source file:net.osten.watermap.batch.FetchSanGJob.java
/** * Fetch the San Gorgonio files and save in output directory. * * @see javax.batch.api.Batchlet#process() * @return FAILED if the files cannot be downloaded or can't be written to disk; COMPLETED otherwise *//*from w w w. ja v a 2 s .c o m*/ @Override public String process() throws Exception { System.out.println("config=" + config); System.out.println("context=" + context); outputDir = new File(config.getString(WatermapConfig.OUTPUT_DIR)); // curl -o $od/datafile.txt http://www.howlingduck.com/triterra/gorgonio/datafile.txt if (!outputDir.isDirectory()) { log.log(Level.WARNING, "Output directory [{0}] is not a directory.", outputDir); return BatchStatus.FAILED.toString(); } else if (!outputDir.canWrite()) { log.log(Level.WARNING, "Output directory [{0}] is not writable.", outputDir); return BatchStatus.FAILED.toString(); } for (String url : URLS) { log.log(Level.FINE, "Fetching PCT to {0}", new Object[] { url }); String response = Request.Get(context.getProperties().getProperty(url)).execute().returnContent() .asString(); File outputFile = new File(outputDir.getAbsolutePath() + File.separator + "datafile.txt"); FileUtils.writeStringToFile(outputFile, response, Charsets.UTF_8); } return BatchStatus.COMPLETED.toString(); }