Java I/O流 随笔拷贝输入fileInputStream输出fileOutputStream流

利用java输入流输入流拷贝图片(mac系统)

package com.hspedu.file.outputstream_;

import java.io.*;

public class FileCopy {

    public static void main(String[] args) {
            //完成文件拷贝
        //思路分析
        //1.创建文件的输入流,将文件读入到程序
        //2.创建文件的输出流,将读取到的文件数据,写入到指定的文件
        String srcFilePath = "/Users/leijinquan/Desktop/java/photo.png";
        String destFilePatch = "/Users/leijinquan/Desktop/java/copy/photo.png";
        FileInputStream fileInputStream = null;
        FileOutputStream fileOutputStream = null;

        try {
            fileInputStream = new FileInputStream(srcFilePath);
             fileOutputStream = new FileOutputStream(destFilePatch);
             //定义一个字节数组
            byte [] buf = new byte[1024];
            int readLen = 0;
            while ((readLen = fileInputStream.read(buf)) != -1){
                //读取到后就写入到文件 通过fileOutputStream
                //即边读边写
                fileOutputStream.write(buf,0,readLen);//一定要有这个方法
            }
            System.out.println("拷贝成功");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //关闭输入流输出流,释放资源
            if (fileInputStream != null){
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fileOutputStream!=null){
                try {
                    fileOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}

 演示java输入流

package com.hspedu.file.inputstream_;

import org.junit.jupiter.api.Test;

import java.io.FileInputStream;
import java.io.IOException;

//演示FileInputStream的使用
//单个字节读取,效率低
//使用readbyte读取
public class FileInputStream_ {
    public static void main(String[] args) {
    }

    public void readFile01() {
        String filePath = "/Users/leijinquan/Desktop/java/hello.txt";
        int readData = 0;
        FileInputStream fileInputStream = null;
        try {
            //创建FilInputStream对象,用于读取文件
             fileInputStream = new FileInputStream(filePath);
            //从该输入读取一个字节的数据,如果没有输入可用,此方法将被阻止
            //如返回-1,表示读取完毕
            while ((readData = fileInputStream.read()) != -1) {
                System.out.print((char)readData);//转成char显示
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //关闭文件流,释放资源
            try {
                fileInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


    @Test
    public void readFile02() {
        String filePath = "/Users/leijinquan/Desktop/java/hello.txt";
        //字节数组
        byte[] buf = new byte[8];//一次读8个字节
        int readLen = 0;
        FileInputStream fileInputStream = null;
        try {
            //创建FilInputStream对象,用于读取文件
            fileInputStream = new FileInputStream(filePath);
            //从该输入流读取最多b.length字节的数据到字节数组,此方法将阻塞,直到某些输入可用
            //如返回-1,表示读取完毕
            //如果读取正常,返回实际读取的字节数
            while ((readLen =(fileInputStream.read(buf))) != -1) {
                System.out.print(new String(buf,0,readLen));
                //显示这样就知道了String newStr = new String(array,0,index);的含义:
                // 就是将数组array中 索引值从0开始 长度为index 的部分截取出来然后建立一个新的字符串newStr
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //关闭文件流,释放资源
            try {
                fileInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }






}

演示java输出流

package com.hspedu.file.outputstream_;

import org.junit.jupiter.api.Test;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

public class FileOutputStream01 {
    public static void main(String[] args) {


    }

    @Test
    //演示使用FileOutputStream将数据写到文件中
    //如果该文件不存在,则创建该文件
    public void writeFile() {
        //先创建 FileOutputStream对象
        String filePath = "/Users/leijinquan/Desktop/java/a.txt";
        FileOutputStream fileOutputStream = null;
        try {
            //得到 FileOutputStream
            //1.new FileOutputStream(filePath)创建方式,写入内容是,会覆盖原来的内容
            //2.new FileOutputStream(filePath,true);创建方式,当写入内容,是追加到文件后面
         fileOutputStream = new FileOutputStream(filePath,true);
            //写入一个字节
//            fileOutputStream.write('H');//char ---> int
           String str = "age";
           //str.getBytes()可以把字符串转成字节数组
            //fileOutputStream.write(str.getBytes());
           fileOutputStream.write(str.getBytes(),0,str.length());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                fileOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值