集册 Java实例教程 将文件复制到输出流

将文件复制到输出流

欢马劈雪     最近更新时间:2020-01-02 10:19:05

457
将文件复制到输出流

import java.io.ByteArrayOutputStream;

import java.io.IOException;

import java.nio.file.FileSystems;

import java.nio.file.Files;//from n o w j a v a . c o m

import java.nio.file.Path;


public class Main {

  public static void main(String[] args) {

    Path sourceFile = FileSystems.getDefault().getPath(

        "C:/home/docs/users.txt");

    try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {

      Files.copy(sourceFile, outputStream);

      byte arr[] = outputStream.toByteArray();

      System.out.println("The contents of " + sourceFile.getFileName());
      /* 
       来自 
      *n o w    j a v a  . c o m*/

      for (byte data : arr) {

        
展开阅读全文