List of usage examples for java.nio.file Files readAllBytes
public static byte[] readAllBytes(Path path) throws IOException
From source file:ch.unibas.charmmtools.gui.step1.mdAssistant.CHARMM_GUI_InputAssistant.java
private File convertWithBabel(File xyz) { String parent = FilenameUtils.getFullPath(xyz.getAbsolutePath()); String basename = FilenameUtils.getBaseName(xyz.getAbsolutePath()); File pdbOut = new File(parent + basename + ".pdb"); BabelConverterAPI babelc = new BabelConverterAPI("xyz", "pdb"); babelc.convert(xyz, pdbOut);/*ww w.j a va 2 s.c o m*/ if (pdbOut.exists()) { try { logger.info("Content of pdb file obtained from babel : \n" + Files.readAllBytes(Paths.get(pdbOut.getAbsolutePath()))); } catch (IOException ex) { } } return pdbOut; }
From source file:com.clust4j.data.BufferedMatrixReader.java
static byte[] fileToBytes(final File file) throws IOException { return Files.readAllBytes(file.toPath()); }
From source file:com.exalttech.trex.util.files.FileManager.java
public static String getFileContent(File fileToRead) throws IOException { byte[] encoded = Files.readAllBytes(Paths.get(fileToRead.getPath())); return new String(encoded); }
From source file:edu.odu.cs.cs350.yellow1.mutationgeneration.JavaFile.java
/** * Open the file and load its contents to this class * /*w w w . j a va2s . c o m*/ * @param path * path the absolute path or relative path * @param provides * encoding the file encoding method * @throws IOException */ public void readFile(String path, Charset encoding) throws IOException { byte[] encoded = Files.readAllBytes(Paths.get(path)); this.fileContents = new String(encoded, encoding); this.filePath = path; }
From source file:org.springsource.restbucks.payment.web.PaymentProcessIntegrationTest.java
/** * Creates a new {@link Order} by looking up the orders link from the source and posting the content of * {@code orders.json} to it. Verifies we get receive a {@code 201 Created} and a {@code Location} header. Follows the * location header to retrieve the {@link Order} just created. * //from w ww . java 2 s. c o m * @param source * @return * @throws Exception */ private MockHttpServletResponse createNewOrder(MockHttpServletResponse source) throws Exception { String content = source.getContentAsString(); Link ordersLink = getDiscovererFor(source).findLinkWithRel(ORDERS_REL, content); ClassPathResource resource = new ClassPathResource("order.json"); byte[] data = Files.readAllBytes(resource.getFile().toPath()); MockHttpServletResponse result = mvc .perform(post(ordersLink.expand().getHref()).contentType(MediaType.APPLICATION_JSON).content(data)). // andExpect(status().isCreated()). // andExpect(header().string("Location", is(notNullValue()))). // andReturn().getResponse(); return mvc.perform(get(result.getHeader("Location"))).andReturn().getResponse(); }
From source file:bds.clemson.nfv.ProviderLoader.java
@SuppressWarnings("unchecked") private void configure(Properties properties) throws CloudException, InternalException, ConfigurationException, IllegalAccessException, InstantiationException { // First, read the basic configuration data from system properties String cname = properties.getProperty("DSN_PROVIDER_CLASS"); String endpoint = properties.getProperty("DSN_ENDPOINT"); String regionId = properties.getProperty("DSN_REGION"); String cloudName = properties.getProperty("DSN_CLOUD_NAME", "Unkown"); String providerName = properties.getProperty("DSN_PROVIDER_NAME", "Unknown"); String account = properties.getProperty("DSN_ACCOUNT"); // Use that information to register the cloud Cloud cloud;/* w ww . j av a 2 s . co m*/ try { cloud = Cloud.register(providerName, cloudName, endpoint, (Class<? extends CloudProvider>) Class.forName(cname)); } catch (ClassNotFoundException e) { throw new ConfigurationException("Unrecognized provider name '" + cname + "', is the jar included?"); } // Find what additional fields are necessary to connect to the cloud ContextRequirements requirements = cloud.buildProvider().getContextRequirements(); List<ContextRequirements.Field> fields = requirements.getConfigurableValues(); // Load the values for the required fields from the system properties ProviderContext.Value[] values = new ProviderContext.Value[fields.size()]; int i = 0; for (ContextRequirements.Field f : fields) { // System.out.print("Loading '" + f.name + "' from "); if (f.type.equals(ContextRequirements.FieldType.KEYPAIR)) { // System.out.println("'DSN_" + f.name + "_SHARED' and 'DSN_" + f.name + "_SECRET'"); String sharedProperty = "DSN_" + f.name + "_SHARED"; String secretProperty = "DSN_" + f.name + "_SECRET"; String shared = properties.getProperty(sharedProperty); String secret = properties.getProperty(secretProperty); if (shared == null || secret == null) throw new ConfigurationException( "Both " + sharedProperty + " and " + secretProperty + " properties are required."); // special case for p12 certificate (as in Google Cloud), from // https://groups.google.com/forum/#!topic/dasein-cloud/vAFgSZR09y0 if (f.name.equals("p12Certificate")) { byte[][] bytes = new byte[2][]; try { bytes[0] = Files.readAllBytes(Paths.get(shared)); } catch (IOException e) { throw new ConfigurationException(e.getMessage()); } bytes[1] = secret.getBytes(); values[i] = new Value<byte[][]>(f.name, bytes); } else { try { values[i] = ProviderContext.Value.parseValue(f, shared, secret); } catch (UnsupportedEncodingException e) { throw new ConfigurationException(e.getMessage()); } } } else { // System.out.println("'DSN_" + f.name + "'"); String propertyName = "DSN_" + f.name; String value = properties.getProperty(propertyName); if (value == null && f.required) throw new ConfigurationException("The " + propertyName + " property is required."); try { values[i] = ProviderContext.Value.parseValue(f, value); } catch (UnsupportedEncodingException e) { throw new ConfigurationException(e.getMessage()); } } i++; } ProviderContext ctx = cloud.createContext(account, regionId, values); configuredProvider = ctx.connect(); }
From source file:org.pathwaycommons.pcviz.service.PathwayCommonsGraphService.java
private String readFile(String path, Charset encoding) throws IOException { byte[] encoded = Files.readAllBytes(Paths.get(path)); return new String(encoded, encoding); }
From source file:org.wso2.devicemgt.raspberry.agent.SidhdhiQuery.java
/** * Read content from a given file and return as a string * @param path//www.ja va2 s . com * @param encoding * @return */ static String readFile(String path, Charset encoding) { byte[] encoded = new byte[0]; try { encoded = Files.readAllBytes(Paths.get(path)); } catch (IOException e) { log.error("Error reading Sidhdhi query from file."); } return new String(encoded, encoding); }
From source file:com.puppycrawl.tools.checkstyle.internal.XdocsPagesTest.java
@Test public void testAllChecksPresentOnAvailableChecksPage() throws Exception { final String availableChecks = new String(Files.readAllBytes(AVAILABLE_CHECKS_PATH), UTF_8); CheckUtil.getSimpleNames(CheckUtil.getCheckstyleChecks()).forEach(checkName -> { if (!isPresent(availableChecks, checkName)) { Assert.fail(checkName + " is not correctly listed on Available Checks page" + " - add it to " + AVAILABLE_CHECKS_PATH); }/*from w ww.ja v a 2 s.com*/ }); }
From source file:com.example.dlp.Inspect.java
private static void inspectFile(String filePath, Likelihood minLikelihood, int maxFindings, List<InfoType> infoTypes, boolean includeQuote) { // [START dlp_inspect_file] // Instantiates a client try (DlpServiceClient dlpServiceClient = DlpServiceClient.create()) { // The path to a local file to inspect. Can be a text, JPG, or PNG file. // fileName = 'path/to/image.png'; // The minimum likelihood required before returning a match // minLikelihood = LIKELIHOOD_UNSPECIFIED; // The maximum number of findings to report (0 = server maximum) // maxFindings = 0; // The infoTypes of information to match // infoTypes = ['US_MALE_NAME', 'US_FEMALE_NAME']; // Whether to include the matching string // includeQuote = true; Path path = Paths.get(filePath); // detect file mime type, default to application/octet-stream String mimeType = URLConnection.guessContentTypeFromName(filePath); if (mimeType == null) { mimeType = MimetypesFileTypeMap.getDefaultFileTypeMap().getContentType(filePath); }//from w w w. j a va 2 s .c om if (mimeType == null) { mimeType = "application/octet-stream"; } byte[] data = Files.readAllBytes(path); ContentItem contentItem = ContentItem.newBuilder().setType(mimeType).setData(ByteString.copyFrom(data)) .build(); InspectConfig inspectConfig = InspectConfig.newBuilder().addAllInfoTypes(infoTypes) .setMinLikelihood(minLikelihood).setMaxFindings(maxFindings).setIncludeQuote(includeQuote) .build(); InspectContentRequest request = InspectContentRequest.newBuilder().setInspectConfig(inspectConfig) .addItems(contentItem).build(); InspectContentResponse response = dlpServiceClient.inspectContent(request); for (InspectResult result : response.getResultsList()) { if (result.getFindingsCount() > 0) { System.out.println("Findings: "); for (Finding finding : result.getFindingsList()) { if (includeQuote) { System.out.print("Quote: " + finding.getQuote()); } System.out.print("\tInfo type: " + finding.getInfoType().getName()); System.out.println("\tLikelihood: " + finding.getLikelihood()); } } else { System.out.println("No findings."); } } } catch (Exception e) { e.printStackTrace(); System.out.println("Error in inspectFile: " + e.getMessage()); } // [END dlp_inspect_file] }