zoukankan      html  css  js  c++  java
  • Java之字节流操作-复制文件

     1 package test_demo.fileoper;
     2 
     3 import java.io.FileInputStream;
     4 import java.io.FileOutputStream;
     5 import java.io.IOException;
     6 
     7 /*
     8 * 字节流操作,复制文件
     9 * 输入流:从文件中读取数据,存放在字节数组中
    10 * 输出流:将字节数组中存放的数据输出到文件中
    11 * 注意关闭输入输出流
    12 * */
    13 public class ByteInOutSteamOper {
    14     public static void main(String args[]) {
    15         FileInputStream fis = null; //输入流
    16         FileOutputStream fos = null;    //输出流
    17         try {
    18             fis = new FileInputStream("C:\testdata\filedir\a.txt");
    19             fos = new FileOutputStream("C:\testdata\filedir\b.txt");
    20             byte bs[] = new byte[1024]; //定义一个字节数值存放中转数据
    21             int i = 0;  //存放读取的byte[]长度
    22             //通过输入流读取数据,经过字节数值中转,由输出流输出到文件
    23             while ((i = fis.read(bs)) != -1) {
    24                 fos.write(bs, 0, i);
    25             }
    26         } catch (IOException e) {
    27             e.printStackTrace();
    28         } finally {
    29             try {
    30                 //关闭输入输出流
    31                 fos.close();
    32                 fis.close();
    33             } catch (IOException e) {
    34                 e.printStackTrace();
    35             }
    36         }
    37     }
    38 }
  • 相关阅读:
    vs2005 配置winpcap
    qt 解决中文乱码问题
    [翻译] QT正则表达式
    使用QSetting 读写ini文件
    [转]GNOME快捷键
    华为面试题之大整数相加
    qt 程序windows 上发布
    win7英文版中文乱码问题
    CURL命令 Alex
    Sendfile机制 Alex
  • 原文地址:https://www.cnblogs.com/gongxr/p/7992398.html
Copyright © 2011-2022 走看看