intput/output 文件的复制练习

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

//逐字节读取,复制

public class io1 {
    public static void main (String []args) throws IOException{
        //读文件
        FileInputStream fis=new FileInputStream("D:\\Desktop\\文件复制练习\\1.jpg");
         //1.要读取的对象(文件内的内容)
         FileOutputStream fos=new FileOutputStream("D:\\Desktop\\文件复制练习\\3.jpg");
         //单独进行写入,要确定写什么,写到什么地方
         //要写入的对象(写入哪个文件)
         
         
         
         //确定读取方式:1.逐字节的读取,2.整个读取,
         int a = fis.read();
         //     读取每一个字节
                         
        while( a!= -1){//读取的数据与-1比较            
            fos.write(a);
        // 循环内先进行输出,后再次进行读取             
         a=fis.read();     //注意死循环 ,出现死循环因为没有a=
         }  
         fis.close();//输入流
         fos.flush();//
         fos.close();//关闭输出流
         }
    }
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;





public class io2 {
    public static void main (String []args) throws IOException{
        //读文件
        FileInputStream fis=new FileInputStream("D:\\Desktop\\文件复制练习\\1.jpg");
         //1.要读取的对象(文件内的内容)
         FileOutputStream fos=new FileOutputStream("D:\\Desktop\\文件复制练习\\4.jpg");
         //单独进行写入,要确定写什么,写到什么地方
         //要写入的对象(写入哪个文件)
                           
         //确定读取方式:1.逐字节的读取,2.整个读取,
         
         
         //整个读取                                                 
          File f=new File ("D:\\Desktop\\文件复制练习\\1.jpg");    
           int c=(int)f.length();
           
           //获取输入文件的长度               
           byte[]b=new byte [c];
           //将文件字节长度。放入同样长度的数组中
           
        int a =  fis.read(b,0,b.length);    //读取数据   
        
           fos.write(b, 0, b.length);
         fis.close();//输入流
         fos.flush();//
         fos.close();//关闭输出流
         }
    }

 

posted @ 2017-06-30 16:15  冰逸101  阅读(349)  评论(0编辑  收藏  举报