List of usage examples for com.google.common.io Files toString
public static String toString(File file, Charset charset) throws IOException
From source file:co.mitro.keyczar.Util.java
public static String readFile(String path) { try {//from w ww .j ava 2 s.co m return Files.toString(new File(path), Charsets.UTF_8); } catch (IOException e) { throw new RuntimeException(e); } }
From source file:com.zyuc.es.netty.EchoClientHandler.java
public void sendMsg(Channel channel) { String str;/*w w w. j ava 2 s . c om*/ try { String path = EchoClientHandler.class.getResource("nettyTest").getPath(); System.out.println("---" + path); System.out.println("------" + Thread.currentThread().getContextClassLoader().getResource("")); str = Files.toString(new File(path), Charset.forName("UTF-8")); ChannelBuffer buffer = ChannelBuffers.dynamicBuffer(); buffer.writeBytes(str.getBytes("UTF-8")); channel.write(buffer).syncUninterruptibly(); } catch (IOException e) { System.out.println("EchoClientHandler.sendMsg() " + e.getMessage()); } }
From source file:controllers.GrokPatternsController.java
@BodyParser.Of(BodyParser.MultipartFormData.class) public Result upload() { String path = getRefererPath(); Http.MultipartFormData body = request().body().asMultipartFormData(); Http.MultipartFormData.FilePart patterns = body.getFile("patterns"); final String[] replaceParam = body.asFormUrlEncoded().get("replace"); boolean replace = replaceParam != null; if (patterns != null) { Collection<GrokPatternSummary> grokPatterns = Lists.newArrayList(); try {// w w w . ja v a 2 s .c o m File file = patterns.getFile(); String patternsContent = Files.toString(file, StandardCharsets.UTF_8); final LineReader lineReader = new LineReader(new StringReader(patternsContent)); Pattern pattern = Pattern.compile("^([A-z0-9_]+)\\s+(.*)$"); String line; while ((line = lineReader.readLine()) != null) { Matcher m = pattern.matcher(line); if (m.matches()) { final GrokPatternSummary grokPattern = new GrokPatternSummary(); grokPattern.name = m.group(1); grokPattern.pattern = m.group(2); grokPatterns.add(grokPattern); } } } catch (IOException e) { Logger.error("Could not parse uploaded file: " + e); flash("error", "The uploaded pattern file could not be parsed: does it have the right format?"); return redirect(path); } try { extractorService.bulkLoadGrokPatterns(grokPatterns, replace); flash("success", "Grok patterns added successfully."); } catch (APIException | IOException e) { flash("error", "There was an error adding the grok patterns, please check the file format."); } } else { flash("error", "You didn't upload any pattern file"); } return redirect(path); }
From source file:org.sonar.plugins.l10n.L10nHackyPropertiesUpdater.java
private static void fixSpacesAroundEqualsAndScrewUpEncoding(File localizedBundle) throws IOException { String lines = Files.toString(localizedBundle, Charset.forName("ISO-8859-1")); lines = lines.replaceAll(" = ", "="); lines = StringEscapeUtils.unescapeJava(lines); // Yeah, this is *really* weird, as properties files are by definition encoded in ISO-8859-1 // but seems like SQ l10n plugins are done this way :-/ Files.write(lines, localizedBundle, Charset.forName("UTF-8")); }
From source file:org.jclouds.virtualbox.config.HardcodeLocalhostAsNodeMetadataSupplier.java
static String readRsaIdentity() { String privateKey;//from w ww. ja v a2 s . c o m try { File keyFile = new File(System.getProperty("user.home") + "/.ssh/id_rsa"); privateKey = Files.toString(keyFile, Charsets.UTF_8); } catch (IOException e) { throw Throwables.propagate(e); } return privateKey; }
From source file:com.android.build.gradle.integration.common.fixture.TemporaryProjectModification.java
public void modifyFile(@NonNull String relativePath, @NonNull Function<String, String> modification) throws InitializationError { File file = mTestProject.file(relativePath); String currentContent = null; try {/* www . ja va2s.c o m*/ currentContent = Files.toString(file, Charsets.UTF_8); } catch (IOException e) { throw new InitializationError(e); } // We can modify multiple times, but we only want to store the original. if (!mFileContentToRestore.containsKey(relativePath)) { mFileContentToRestore.put(relativePath, currentContent); } String newContent = modification.apply(currentContent); if (newContent == null) { assertTrue("File should have been deleted", file.delete()); } else { try { Files.write(newContent, file, Charsets.UTF_8); } catch (IOException e) { throw new InitializationError(e); } } }
From source file:org.sonar.java.checks.FileHeaderCheck.java
public void visitFile(File file) { if (isRegularExpression) { String fileContent;/*from w ww. j a va 2 s. c o m*/ try { fileContent = Files.toString(file, charset); } catch (IOException e) { throw new AnalysisException(e); } checkRegularExpression(fileContent); } else { List<String> lines; try { lines = Files.readLines(file, charset); } catch (IOException e) { throw new AnalysisException(e); } if (!matches(expectedLines, lines)) { addIssueOnFile(MESSAGE); } } }
From source file:org.eclipse.scout.docs.publish.PublishHelpUtility.java
public static void publishEclipseHelp(File rootInFolder, File rootOutFolder, File listOfPagesFile) throws IOException { if (!rootInFolder.exists() || !rootInFolder.isDirectory()) { throw new IllegalStateException( "Folder rootInFolder '" + rootInFolder.getAbsolutePath() + "' not found."); }//from w w w. j a v a2s .c o m if (!listOfPagesFile.exists() || !listOfPagesFile.isFile()) { throw new IllegalStateException( "File listOfPagesFile '" + listOfPagesFile.getAbsolutePath() + "' not found."); } List<String> pages = Files.readLines(listOfPagesFile, Charsets.ISO_8859_1); List<File> inFiles = new ArrayList<File>(); for (String p : pages) { if (p != null && p.length() > 0) { File f = new File(rootInFolder, p); if (!f.exists() || !f.isFile()) { throw new IllegalStateException("File '" + f.getAbsolutePath() + "' not found."); } inFiles.add(f); } } OutlineItemEx root = new OutlineItemEx(null, 0, "id", 0, 0, "Eclipse Scout User Guide"); root.setFilePath(HTML_SUB_PATH + inFiles.get(0).getName()); //////////////////////////////////////////////////////////////////////////////////////////////////////////// //Check if all files from the "rootInFolder" are contained in the inFiles list List<File> filesInFolder = new ArrayList<File>(); filesInFolder.addAll(Arrays.asList(rootInFolder.listFiles(new FileFilter() { @Override public boolean accept(File f) { return f.getName().endsWith("html"); } }))); for (File f : filesInFolder) { if (!inFiles.contains(f)) { throw new IllegalStateException("File '" + f.getName() + "' is missing in the inFiles list"); } } //////////////////////////////////////////////////////////////////////////////////////////////////////////// File htmlFolder = new File(rootOutFolder, HTML_SUB_PATH); if (htmlFolder.exists()) { htmlFolder.delete(); } copyNavImg(htmlFolder); Map<Integer, OutlineItemEx> nodeMap = new HashMap<Integer, OutlineItemEx>(); putNode(nodeMap, root, root.getLevel()); for (int i = 0; i < inFiles.size(); i++) { File inFile = inFiles.get(i); File outFile = new File(htmlFolder, inFile.getName()); String html = Files.toString(inFile, Charsets.ISO_8859_1); String filePath = outFile.getAbsolutePath().substring(rootOutFolder.getAbsolutePath().length() + 1) .replaceAll("\\\\", "/"); Document doc = Jsoup.parse(html); doc.outputSettings().charset("ASCII"); if (i > 0) { //Next files are taken into account in the outline tree: computeOutlineNodes(nodeMap, doc, filePath); } //Create the navigation section: String nextHref = null; String nextTitle = null; String prevHref = null; String prevTitle = null; if (i < inFiles.size() - 1) { nextHref = inFiles.get(i + 1).getName(); nextTitle = readAndFindFirstHeader(inFiles.get(i + 1)); } if (i > 0 && inFiles.size() > 0) { prevHref = inFiles.get(i - 1).getName(); prevTitle = readAndFindFirstHeader(inFiles.get(i - 1)); } String baseUri = doc.baseUri(); String title = findFirstHeader(doc); Element tableTop = createNavigationTable(root, title, true, nextHref, prevHref, nextTitle, prevTitle, baseUri); doc.body().insertChildren(0, Collections.singleton(tableTop)); Element tableBottom = createNavigationTable(root, title, false, nextHref, prevHref, nextTitle, prevTitle, baseUri); insertBeforeId(doc.body(), "footer", tableBottom); //Move and Copy Images: PublishUtility.moveAndcopyImages(doc, inFile.getParentFile(), htmlFolder, IMAGES_SUB_PATH); //Move and Copy CSS: PublishUtility.moveAndcopyCss(doc, inFile.getParentFile(), htmlFolder, "css/"); //Fix Figure Links: PublishUtility.fixListingLink(doc); PublishUtility.fixFigureLink(doc); //Write as file: Files.createParentDirs(outFile); Files.write(doc.toString(), outFile, Charsets.ISO_8859_1); } MarkupToEclipseToc eclipseToc = new MarkupToEclipseToc() { @Override protected String computeFile(OutlineItem item) { if (item instanceof OutlineItemEx && ((OutlineItemEx) item).getFilePath() != null) { return ((OutlineItemEx) item).getFilePath(); } return super.computeFile(item); } }; eclipseToc.setBookTitle(root.getLabel()); eclipseToc.setHtmlFile(root.getFilePath()); String tocContent = eclipseToc.createToc(root); File outTocFile = new File(rootOutFolder, "toc.xml"); Files.write(tocContent, outTocFile, Charsets.ISO_8859_1); }
From source file:eu.redzoo.article.planetcassandra.reactive.CassandraDB.java
/** * executes a CQL file. CQL processing errors will be ignored * // ww w . j ava 2 s. c o m * @param cqlFile the CQL file name * @throws IOException if the file could not be found */ public void tryExecuteCqlFile(String cqlFile) { try { File file = new File(cqlFile); if (file.exists()) { tryExecuteCql(Files.toString(new File(cqlFile), Charsets.UTF_8)); } else { tryExecuteCql(Resources.toString(Resources.getResource(cqlFile), Charsets.UTF_8)); } } catch (IOException ioe) { throw new RuntimeException(cqlFile + " not found"); } }
From source file:org.sonar.cxx.checks.regex.FileHeaderCheck.java
@Override public void visitFile(AstNode astNode) { if (isRegularExpression) { String fileContent;//from ww w. ja v a 2 s . c om try { fileContent = Files.toString(getContext().getFile(), charset); } catch (IOException e) { throw new AnalysisException(e); } checkRegularExpression(fileContent); } else { List<String> lines; try { lines = Files.readLines(getContext().getFile(), charset); } catch (IOException e) { throw new IllegalStateException(e); } if (!matches(expectedLines, lines)) { getContext().createFileViolation(this, MESSAGE); } } }