19.使用字节流 写 读 复制 文件

    1.效果

091016_qwev_3015807.png

2.代码

package cn.ma.fileinputstream;

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

/**
 * @ClassName: Instream
 * @Company: cn.ma.com
 * @Description:  使用字节流  fileInputStream 读取本地文件   这里的内容必须使用数字或者英文字母   否则会乱码  原因是使用字节流对象
 * @author ss 
 * @date 2017年7月13日 下午11:34:59
 */

public class Instream {
        @SuppressWarnings("resource")
        public static void main(String[] args) throws IOException {
            
            
            //1.E:/IO/a.txt文件下  写入内容
            FileOutputStream fos = new FileOutputStream("E:/IO/a.txt",true);
            String s="abcdefjhiuakiosaioioasioasiooi";//当文本中有中文的时候就会导致读入的数据乱码    原因是将内容转换成为char类型了
            byte[] b = s.getBytes();
            fos.write(b);
            fos.close();
            
            //2.读取E:/IO/a.txt  中的内容
            //2.1 创建输入字节流 对象
             FileInputStream fis = new  FileInputStream("E:/IO/a.txt");
             //2.2 读取并输出到控制台
             while(fis.read() !=-1){
                 System.out.print((char)(fis.read()));
             }
             fis.close();
             
             /**
              * 3.将刚刚生成的a.text 复制到  E:\test\b.docx 下
              *     3.1    读取 文件
              *         3.1.1   创建字节流输入对象  FileInputStream  封装路径
              *         3.1.2   依字节 读取  文件   ,注意判断条件
              *         3.1.2   关流
              *     3.2    将文件写到  目的地
              *         3.2.1   创建 自己输出流  FileOutputStream 并封装路径
              *         3.2.2 将文件写到 目的路径下
              *         3.2.3 关闭流
              *         
              */
             //3.1  读文件
             FileInputStream fis2 = new  FileInputStream("E:/IO/a.txt");
             
            // 3.2 封装目的地   不需要程序执行前 已经存在 b.txt文件
                FileOutputStream fos2 = new FileOutputStream("E:/test/b.txt");
                 int by = 0;
                     while((by= fis2.read()) !=-1){
                         fos2.write(by);
                     }
             // 3.3 关闭资源   后开先关
                     fos2.close();
                     fis2.close();
                     
        }
}
4.注意事项

复制文件是完全可以复制中文内容的   因为不是将它们转化为字节了,就不会出现乱码了

刚刚是因为 先去生产

4.1复制中文的效果

093721_gqEh_3015807.png

4.2代码

093820_Juqr_3015807.png

/**
         * 4.字节流复制带有中文的文件 到目的路径下
         */
                     FileInputStream fis4=null;
                     FileOutputStream fos4 =null;
        try {
             fis4 = new FileInputStream("E:/IO/c.txt");//这个是准备好的   带有中文的文件
            fos4 = new FileOutputStream("E:/test/d.txt");
            int i=0;
            while((i= fis4.read())  !=-1){
                fos4.write(i);
            }
            System.out.println("复制文件完成");
            
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            fis4.close();
            fos4.close();
        }

 

转载于:https://my.oschina.net/springMVCAndspring/blog/1406221

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值