文件的拷贝

来源:互联网 发布:说明书排版用什么软件 编辑:程序博客网 时间:2024/06/05 07:47
步骤:
文件的拷贝
1.建立联系        File对象
2.选择流           输入流、输出流        InputStream、outputStream、FileInputStream、FileoutputStream
3.操作              byte[] test = new byte[20];
                       定义每次实际读的长度;
                       while语句循环读取完文件数据{
                       输出流.write();
                       输入流.flush();
                       }
4.释放资源 关闭流

public class MyCopy {       public static void main(String[] args){              /**               * 文件的拷贝               * 1.建立联系        File对象               * 2.选择流   输入流、输出流        InputStream、outputStream、FileInputStream、FileoutputStream               * 3.操作            byte[] test = new byte[20];               *                   定义每次实际读的长度;               *                   while语句循环读取完文件数据{               *                         输出流.write();               *                         输出流.flush();               *                   }               * 4.释放资源 关闭流               */              File src = new File("C:/Users/Administrator/Desktop/../MyDemo2/image1.jpg");              File dest = new File("C:/Users/Administrator/Desktop/android学习笔记/image1.jpg");              InputStream iS = null;              OutputStream oS = null;              try {                     iS = new FileInputStream(src);                     oS = new FileOutputStream(dest,true);                     byte[] test = new byte[20];                     int len =0;                     while(-1!=(len=iS.read(test))){                           oS.write(test, 0, len);                           oS.flush();                     }              } catch (FileNotFoundException e) {                     // TODO Auto-generated catch block                     e.printStackTrace();              } catch (IOException e) {                     // TODO Auto-generated catch block                     e.printStackTrace();              }finally {                     if(null!=iS){                           try {                                  iS.close();                                  oS.close();                           } catch (IOException e) {                                  // TODO Auto-generated catch block                                  e.printStackTrace();                           }                     }              }       }}


0 0
原创粉丝点击