io流

来源:互联网 发布:人民银行金融数据 编辑:程序博客网 时间:2024/06/06 20:18

package com.java1;

 

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

 

import org.junit.Test;

 

public class TestFile {

  @Test

  public void test1(){

    FileInputStream fis = null;

    FileOutputStream fos = null;

    try {

      fis = newFileInputStream("hello.txt");

      fos = newFileOutputStream("hello2.txt",true);

 

核心代码注意FileOutputStream后面的true的意思是继续接着源文件读入进去也会换行

     

      byte[] b =newbyte[2];

      int len;

      while((len =fis.read(b)) != -1){

                fos.write(b,0,len);

      }

    } catch (IOException e) {

      e.printStackTrace();

    }finally{

      if(fos !=null){

        try {

          fos.close();

        } catch (IOException e) {

          e.printStackTrace();

        }

      }

      if(fis !=null){

       

        try {

          fis.close();

        } catch (IOException e) {

          e.printStackTrace();

        }

      }

    }

  }

}

 

原创粉丝点击