I/O----复制文本文件

时间:2023-03-08 22:13:47
I/O----复制文本文件

文件 “我的青春谁做主.txt” 位于 D 盘根目录下,要求将此文件的内容复制到 C:/myPrime.txt 中。

package io.day03;

 import java.io.FileInputStream;

 import java.io.FileOutputStream;

 import java.io.InputStream;

 import java.io.OutputStream;

public class Test2 {

   public static void main(String[] args) throws Exception {

  InputStream is =new FileInputStream("d:/我的青春谁做主.txt");

  OutputStream os =new FileOutputStream("c:/myPrime.txt");

   byte[] bs =new byte[1024];

  while(true){

    int length =is.read(bs);

  //从下表0开始,把length长度写到文件中    os.write(bs, 0, length);

    if(length<1024){

    break;

  }

    }

    is.close();

    os.close();

}

}

注意:a.  boolean:表示是否向文件末尾追加,如果是true,表示追加,falase表示不追加(也就是覆 盖),默认值为false
         b. 创建FileOutputStream 实例时, 如果相应的文件并不存在,则会自动创建一个空的文件