Refine search
import java.nio.file.Files; import java.nio.file.Paths; import java.nio.file.Path; Path path = Paths.get("path/to/file"); byte[] data = Files.readAllBytes(path);
/** * 读取文件到byte[]. * * @see {@link Files#readAllBytes} */ public static byte[] toByteArray(final File file) throws IOException { return Files.readAllBytes(file.toPath()); }
public static void main(String[] args) throws URISyntaxException, IOException { Path worldFile = Paths.get(Resources.getResource("world").toURI()); String world = new String(Files.readAllBytes(worldFile), StandardCharsets.UTF_8); System.out.println("Hello " + world); } }
/** * Tests https://issues.apache.org/jira/browse/LANG-708 * * @throws IOException * if an I/O error occurs */ @Test public void testLang708() throws IOException { final byte[] inputBytes = Files.readAllBytes(Paths.get("src/test/resources/lang-708-input.txt")); final String input = new String(inputBytes, StandardCharsets.UTF_8); final String escaped = StringEscapeUtils.escapeEcmaScript(input); // just the end: assertTrue(escaped, escaped.endsWith("}]")); // a little more: assertTrue(escaped, escaped.endsWith("\"valueCode\\\":\\\"\\\"}]")); }
@Test public void testChunkedCopy() throws Exception { File f = tempFolder.newFile(); byte[] bytes = new byte[]{(byte) 0x8, (byte) 0x9}; ByteArrayInputStream bis = new ByteArrayInputStream(bytes); NativeIO.chunkedCopy(bis, f); byte[] data = Files.readAllBytes(f.toPath()); Assert.assertTrue(Arrays.equals(bytes, data)); }
@Test public void testToJson4() throws IOException { DefaultMustacheFactory dmf = new DefaultMustacheFactory(); Mustache compile = dmf.compile("fdbcli2.mustache"); Path file = getPath("src/test/resources/fdbcli3.txt"); String txt = new String(Files.readAllBytes(file), "UTF-8"); System.out.println("Input text:["); System.out.print(txt); System.out.println("]"); Node invert = compile.invert(txt); output(invert); }
@Override public void visitFile(Path file, String relative) throws IOException { if (file.getFileName().toString().endsWith(".class")) { ClassReader cr = new ClassReader(Files.readAllBytes(file)); ClassNode cn = new ClassNode(); cr.accept(new CheckClassAdapter(cn, false), ClassReader.SKIP_DEBUG | ClassReader.EXPAND_FRAMES | ClassReader.SKIP_FRAMES); for (MethodNode method : cn.methods) { BasicVerifier verifier = new BasicVerifier(); Analyzer<BasicValue> a = new Analyzer<>(verifier); try { a.analyze(cn.name, method); } catch (Exception ex) { System.err.println("Error verify method " + cr.getClassName() + "." + method.name + " " + method.desc); if (detail) { ex.printStackTrace(System.err); printAnalyzerResult(method, a, new PrintWriter(new OutputStreamWriter(System.err, StandardCharsets.UTF_8))); } } } } } });
@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); } }); }
private void corruptJar( URL jar ) throws IOException, URISyntaxException { File jarFile = new File( jar.toURI() ).getCanonicalFile(); long fileLength = jarFile.length(); byte[] bytes = Files.readAllBytes( Paths.get( jar.toURI() ) ); for ( long i = fileLength / 2; i < fileLength; i++ ) { bytes[(int) i] = 0; } Files.write( jarFile.toPath(), bytes ); }
@Test public void testPrintAst() throws Exception { final FileText text = new FileText( new File(getPath("InputAstTreeStringPrinterComments.java")).getAbsoluteFile(), System.getProperty("file.encoding", StandardCharsets.UTF_8.name())); final String actual = AstTreeStringPrinter.printAst(text, JavaParser.Options.WITHOUT_COMMENTS); final String expected = new String(Files.readAllBytes(Paths.get( getPath("ExpectedAstTreeStringPrinter.txt"))), StandardCharsets.UTF_8); Assert.assertEquals("Print AST output is invalid", expected, actual); }
@Test public void testExtractContainedLibraries() throws Exception { String s = "testExtractContainedLibraries"; byte[] nestedJarContent = s.getBytes(ConfigConstants.DEFAULT_CHARSET); File fakeJar = temporaryFolder.newFile("test.jar"); try (ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(fakeJar))) { ZipEntry entry = new ZipEntry("lib/internalTest.jar"); zos.putNextEntry(entry); zos.write(nestedJarContent); zos.closeEntry(); } final List<File> files = PackagedProgram.extractContainedLibraries(fakeJar.toURI().toURL()); Assert.assertEquals(1, files.size()); Assert.assertArrayEquals(nestedJarContent, Files.readAllBytes(files.iterator().next().toPath())); }
@Test public void testParser() throws IOException { DefaultMustacheFactory dmf = new DefaultMustacheFactory(); Mustache compile = dmf.compile("fdbcli.mustache"); Path file = getPath("src/test/resources/fdbcli.txt"); String txt = new String(Files.readAllBytes(file), "UTF-8"); Node invert = compile.invert(txt); System.out.println(invert); }
public static void main(final String[] args) throws Exception { final Path path = Paths.get(args[0]); final byte[] data = Files.readAllBytes(path); final byte[] encoded = Base64.getEncoder().encode(data); final String s = new String(encoded, StandardCharsets.UTF_8); System.out.println(String.format("Base64 encode content:%s" + "=======================%s" + "%s%s" + "=======================", lineSeparator(), lineSeparator(), s, lineSeparator())); } }
@Test public void testToJson5() throws IOException { DefaultMustacheFactory dmf = new DefaultMustacheFactory(); Mustache compile = dmf.compile("fdbcli3.mustache"); Path file = getPath("src/test/resources/fdbcli.txt"); String txt = new String(Files.readAllBytes(file), "UTF-8"); Node invert = compile.invert(txt); output(invert); }
/** * Read a service definition as a String */ public String getServiceDefinition(String serviceName) throws IOException { String serviceDefPath = narWorkingDirectory + "/META-INF/services/" + serviceName; return new String(Files.readAllBytes(Paths.get(serviceDefPath))); }
public static String readFile(File file, String charsetName) throws IOException { byte[] bytes = Files.readAllBytes(file.toPath()); return new String(bytes, charsetName); }
@Test public void testBeelinePasswordMask() throws Exception { TestBeeline bl = new TestBeeline(); File errFile = File.createTempFile("test", "tmp"); bl.setErrorStream(new PrintStream(new FileOutputStream(errFile))); String args[] = new String[] { "-u", "url", "-n", "name", "-p", "password", "-d", "driver", "--autoCommit=true", "--verbose", "--truncateTable" }; bl.initArgs(args); bl.close(); String errContents = new String(Files.readAllBytes(Paths.get(errFile.toString()))); Assert.assertTrue(errContents.contains(BeeLine.PASSWD_MASK)); }
@Test @TestForIssue(jiraKey = "HHH-12132") public void testTargetScriptIsCreatedBooleanTypeDefault() throws Exception { this.rebuildSessionFactory(); String fileContent = new String( Files.readAllBytes( output.toPath() ) ); Pattern fileContentPattern = Pattern.compile( "create( (column|row))? table test_entity \\(field varchar.+, b boolean.+, c varchar.+, lob clob" ); Matcher fileContentMatcher = fileContentPattern.matcher( fileContent.toLowerCase() ); assertThat( "Script file : " + fileContent.toLowerCase(), fileContentMatcher.find(), is( true ) ); }
@Test public void testToJson() throws IOException { DefaultMustacheFactory dmf = new DefaultMustacheFactory(); Mustache compile = dmf.compile("fdbcli.mustache"); Path file = getPath("src/test/resources/fdbcli.txt"); String txt = new String(Files.readAllBytes(file), "UTF-8"); Node invert = compile.invert(txt); output(invert); }
public ByteBody(File file) { try { this.file = file; this.byteBuf = Unpooled.copiedBuffer(Files.readAllBytes(Paths.get(file.toURI()))); } catch (IOException e) { e.printStackTrace(); } }