IO流(2)--定义小数组拷贝

来源:互联网 发布:系统重装软件哪个好 编辑:程序博客网 时间:2024/05/16 17:57

拷贝数据的方法–定义小数组

package com.fenqing.Stream;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;public class demo4_arrayCopy {    public static void main(String[] args) throws IOException {    //      simple();//未定义部分//      littleArray1();        FileInputStream fis=new FileInputStream("xxx.txt");        FileOutputStream fos=new FileOutputStream("yyy.txt");    byte[] arr=new byte[1024*8];    //一般情况下,定义成1024的整数倍    int len;    while((len=fis.read(arr))!=-1){ //如果不加arr,那么读取的就是字符的码表值,而不是字节个数         fos.write(arr,0,len);    }    fis.close();    fos.close();}public static void littleArray1() throws FileNotFoundException, IOException {    FileInputStream fis=new FileInputStream("xxx.txt");    FileOutputStream fos=new FileOutputStream("yyy.txt");    byte[] arr=new byte[2];    int len;    while((len=fis.read(arr))!=-1){         fos.write(arr,0,len);    }    fis.close();    fos.close();}public static void simple() throws FileNotFoundException, IOException {    FileInputStream fis=new FileInputStream("xxx.txt");    byte[] arr=new byte[2];    System.out.println("-----第一次-----");    int a=fis.read(arr);    //将文件上的字节读到字节数组中    System.out.println(a);  //读到的有效字节个数    for (byte b : arr) {    //第一次获取到文件上的a和b        System.out.println(b);    }    System.out.println("-----第二次-----");    int c=fis.read(arr);    System.out.println(c);    for (byte d : arr) {        System.out.println(d);    }    fis.close();}}
原创粉丝点击