java.nio.file.Files.readAllBytes()方法的使用及代码示例

x33g5p2x  于2022-01-17 转载在 其他  
字(8.6k)|赞(0)|评价(0)|浏览(846)

本文整理了Java中java.nio.file.Files.readAllBytes()方法的一些代码示例,展示了Files.readAllBytes()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Files.readAllBytes()方法的具体详情如下:
包路径:java.nio.file.Files
类名称:Files
方法名:readAllBytes

Files.readAllBytes介绍

暂无

代码示例

代码示例来源:origin: stackoverflow.com

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);

代码示例来源:origin: springside/springside4

/**
 * 读取文件到byte[].
 * 
 * @see {@link Files#readAllBytes}
 */
public static byte[] toByteArray(final File file) throws IOException {
  return Files.readAllBytes(file.toPath());
}

代码示例来源:origin: GoogleContainerTools/jib

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);
 }
}

代码示例来源:origin: org.apache.commons/commons-lang3

/**
 * 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\\\":\\\"\\\"}]"));
}

代码示例来源:origin: apache/incubator-druid

@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));
}

代码示例来源:origin: spullara/mustache.java

@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);
}

代码示例来源:origin: pxb1988/dex2jar

@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)));
          }
        }
      }
    }
  }
});

代码示例来源:origin: checkstyle/checkstyle

@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);
      }
    });
}

代码示例来源:origin: neo4j/neo4j

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 );
}

代码示例来源:origin: checkstyle/checkstyle

@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);
}

代码示例来源:origin: apache/flink

@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()));
}

代码示例来源:origin: spullara/mustache.java

@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);
}

代码示例来源:origin: confluentinc/ksql

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()));
 }
}

代码示例来源:origin: spullara/mustache.java

@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);
}

代码示例来源:origin: apache/pulsar

/**
 * 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)));
}

代码示例来源:origin: apache/flink

public static String readFile(File file, String charsetName) throws IOException {
  byte[] bytes = Files.readAllBytes(file.toPath());
  return new String(bytes, charsetName);
}

代码示例来源:origin: apache/hive

@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));
}

代码示例来源:origin: hibernate/hibernate-orm

@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 ) );
}

代码示例来源:origin: spullara/mustache.java

@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);
}

代码示例来源:origin: lets-blade/blade

public ByteBody(File file) {
  try {
    this.file = file;
    this.byteBuf = Unpooled.copiedBuffer(Files.readAllBytes(Paths.get(file.toURI())));
  } catch (IOException e) {
    e.printStackTrace();
  }
}

相关文章

微信公众号

最新文章

更多