FileInputStream和FileOutputStream

简介: FileInputStream和FileOutputStream

FileInputStream和FileOutputStream的使用

总结于尚硅谷学习视频

package com.day0315_2;
import org.junit.jupiter.api.Test;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/**
 * 测试FileInputStream和FileOutputStream的使用
 *
 * 结论:
 * 1.对于文本文件(.txt,.java,.c,.cpp),使用字符流处理
 * 2.对于非文本文件(.jpt,.mp3,.mp4,.avi,.ppt),使用字节流处理
 *
 *
 */
public class FileInputOutputStreamTest {
    //使用字节流处理文本文件是可能出现乱码的
    @Test
    public void testFileInputStream() {
        FileInputStream fis= null;
        try {
            //1.造文件
            File file=new File("hello.txt");
            //2.造流
            fis = new FileInputStream(file);
            //3.读数据
            byte[] buffer= new byte[5];
            int len;//记录
            while((len=fis.read(buffer))!=-1){
                String str=new String(buffer,0,len);
                System.out.println(str);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(fis!=null)
            //4.关闭资源
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
        }
    }
    /*
    实现对图片的复制
     */
    @Test
    public void testFileInputOutputStream(){
        FileInputStream fis= null;
        FileOutputStream fos= null;
        try {
            //
            File strFile = new File("图片.png");
            File destFile = new File("图片2.png");
            //
            fis = new FileInputStream(strFile);
            fos = new FileOutputStream(destFile);
            //复制的过程
            byte[] buffer=new byte[5];
            int len;
            while((len=fis.read(buffer))!=-1){
                fos.write(buffer,0,len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //
            if(fos!=null)
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            if(fis!=null)
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
        }
    }
    //指定路径下的文件的复制
    public void copyFile(String srcPath,String destPath){
        FileInputStream fis= null;
        FileOutputStream fos= null;
        try {
            //
            File strFile = new File(srcPath);
            File destFile = new File(destPath);
            //
            fis = new FileInputStream(strFile);
            fos = new FileOutputStream(destFile);
            //复制的过程
            byte[] buffer=new byte[1024];
            int len;
            while((len=fis.read(buffer))!=-1){
                fos.write(buffer,0,len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //
            if(fos!=null)
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            if(fis!=null)
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
        }
    }
    @Test
    public void testCopyFile(){
        long start =System.currentTimeMillis();
        String srcPath="hello.txt";
        String destPath="hello3.txt";
        copyFile(srcPath,destPath);
        long end =System.currentTimeMillis();
        System.out.println("复制操作花费时间为:"+(end-start));
    }
}
相关文章
|
5月前
|
存储 Java 调度
FileInputStream,FileOutputStream 和 FileReader ,FileWriter 类的基本使用【 File类+IO流知识回顾②】
这篇文章回顾了Java中FileInputStream、FileOutputStream、FileReader和FileWriter类的基本使用方法,包括读取和写入文件的操作,以及字符流和字节流的区别和应用场景。
FileInputStream,FileOutputStream 和 FileReader ,FileWriter 类的基本使用【 File类+IO流知识回顾②】
|
7月前
|
存储 Java
使用OutputStreamWriter写入数据
使用OutputStreamWriter写入数据
|
9月前
|
Java
FileInputStream和FileOutputStream
FileInputStream和FileOutputStream
62 0
File操作-FileReader(FileWriter)/BufferedReader(Writer)
File操作-FileReader(FileWriter)/BufferedReader(Writer)
66 0
|
存储 Java
FileInputStream 你了解多少
FileInputStream 你了解多少
|
消息中间件 前端开发 JavaScript
InputStream 和 OutputStream 基础
大家好,我是指北君。 PS:最近是跳槽的高峰期,我连日加班好多天,整理出了包含16000 多道面试题的面试宝典,并且指北君也会持续更新这份面试宝典中的题目,希望它能帮助大家找到自己心仪的工作!【文末有领取方式】
InputStream 和 OutputStream 基础
BufferedReader和BufferedWriter(七)
以前读取和写入文件内容时,用的Reader和Writer, 当调用 read() 或者 readLine() 方法时,效率是非常慢的。 如果能添加缓冲区,那么就非常好了。 BufferedReader 和 BufferedWriter ,就是在 Reader和 Writer 的基础上,添加了缓冲区, 即不仅具有Reader和Writer的各个功能,还能通过缓冲区进行相应的优化, 其实,这就是装饰器。
167 0
BufferedReader和BufferedWriter(七)
|
移动开发
字节流InputStream和OutputStream(二)上
字节流InputStream和OutputStream(二)
197 0
字节流InputStream和OutputStream(二)上