java中复制文本文件

来源:互联网 发布:拜耳杀蟑胶饵 知乎 编辑:程序博客网 时间:2024/05/16 15:10
package jbit.io;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;public class InputAndOutputFile {public static void main(String[] args) {FileInputStream fis=null;FileOutputStream fos=null;try {//1、创建输入流对,负责读取D:/ 我的青春谁做主.txt文件fis = new FileInputStream("D:/我的青春谁做主.txt");//2、创建输出流对象fos = new FileOutputStream("C:/myFile/myPrime.txt",true);//3、创建中转站数组,存放每次读取的内容byte[] words=new byte[1024];//4、通过循环实现文件读取while((fis.read())!=-1){fis.read(words);fos.write(words, 0, words.length);}System.out.println("复制完成,请查看文件!");} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}finally{//5、关闭流        try {        if(fos!=null)        fos.close();        if(fis!=null)        fis.close();} catch (IOException e) {e.printStackTrace();}}}}

0 0
原创粉丝点击