FileReader && FileWriter && FileInputStream && FileOutputStream

FileReader && FileWriter && FileInputStream && FileOutputStream,第1张

FileReader && FileWriter && FileInputStream && FileOutputStream

目录

一、读入数据的基本 *** 作

将day09下的hello.txt文件内容读入程序中,并输出到控制台

1.步骤:

2.完整代码 

3.异常的处理:为了保证流资源一定可以执行关闭 *** 作,需要使用try-catch-finally处理 

注意

二、对read() *** 作升级:使用read的重载方法

⚪步骤

 1.File类的实例化

2.FileReader流的实例化

3.读入 *** 作

4.资源的关闭

三、FileWriter写出数据的 *** 作

 ————从内存中写出数据到硬盘的文件里

(一)步骤

1.提供File类的对象,指明写出到的文件

2.提供FileWriter的对象,用于数据输出

3.写出的 *** 作

4.流资源的关闭

完整代码

⚪结果:

 (二)说明

⚪输出 *** 作对应的File可以不存在,并不会报异常

四、使用FileReader和FileWriter实现文本文件的复制

五、图片文件的处理

1.字符流能否处理图片文件

2.需要用到字节流来处理图片文件

六、FileInputStream和FileOutputStream的使用

(一)使用字节流FileInputStream处理文本文件

⚪结论

(二)使用字节流处理非文本文件——实现对图片的复制

 七、指定路径下文件的复制的方法

1.方法

2.视频的复制

 耗费的时间:

运行结果 


一、读入数据的基本 *** 作 将day09下的hello.txt文件内容读入程序中,并输出到控制台 1.步骤:

①.实例化File类的对象,指明要 *** 作的文件

②.提供具体的流

③.数据的读入

read():返回读入的一个字符。如果达到文件末尾,返回-1

        int data = fr.read();
        while (data != -1){
            System.out.println((char)data);
            data = fr.read();
        }

④.流的关闭 *** 作

 

2.完整代码 
public class FileReaderWriterTest {
    public static void main(String[] args) {
        File file = new File("hello.txt"); //相较于当前工程
        System.out.println(file.getAbsoluteFile());  //E:BaiduNetdiskDownloadzzzzzzzzhello.txt

        File file1 = new File("day09\hello.txt");
        System.out.println(file1.getAbsoluteFile());
    }
    @Test
    public void test1() throws IOException {
        //1.实例化File类的对象,指明要 *** 作的文件
        File file = new File("hello.txt");//相较于当前module下的相对路径
        //2.提供具体的流
        FileReader fr = new FileReader(file);
        //3.数据的读入
        //read():返回读入的一个字符。如果达到文件末尾,返回-1
        int data = fr.read();
        while (data != -1){
            System.out.println((char)data);
            data = fr.read();
        }
        //4.流的关闭 *** 作
        fr.close();
    }
}

 

3.异常的处理:为了保证流资源一定可以执行关闭 *** 作,需要使用try-catch-finally处理 
    @Test
    public void test1() {
        FileReader fr = null;
        try {
            //1.实例化File类的对象,指明要 *** 作的文件
            File file = new File("hello.txt");//相较于当前module下的相对路径
            //2.提供具体的流
            fr = new FileReader(file);
            //3.数据的读入
            //read():返回读入的一个字符。如果达到文件末尾,返回-1
            int data = fr.read();
            while (data != -1) {
                System.out.println((char) data);
                data = fr.read();
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //4.流的关闭 *** 作
            try {
                fr.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
注意

读入的对象一定要存在,否则就会报错:FileNotFoundException

二、对read() *** 作升级:使用read的重载方法 ⚪步骤  1.File类的实例化

2.FileReader流的实例化

3.读入 *** 作

 

read(char[ ] cbuf):返回每次读入cbuf数组中的字符个数。如果达到文件末尾,返回-1

            char[] cbuf = new char[5];
            int read;
            while((read = fr.read(cbuf)) != -1){
                //方式1
//                //错误写法
                for (int i = 0; i < cbuf.length; i++) {
                    System.out.print(cbuf[i]); //helloworld123ld
                }
//                //正确写法
//                for (int i = 0; i < read; i++) {
//                    System.out.print(cbuf[i]);
//                }
                //方式2
                //错误写法,对应方式1的错误写法
//                String str = new String(cbuf);
//                System.out.print(str);  //helloworld123ld
                //正确写法
                String str = new String(cbuf,0,read);
                System.out.print(str);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(fr != null){
                try {
                    fr.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
4.资源的关闭

三、FileWriter写出数据的 *** 作  ————从内存中写出数据到硬盘的文件里 (一)步骤 1.提供File类的对象,指明写出到的文件 2.提供FileWriter的对象,用于数据输出

 

3.写出的 *** 作
        fw.write("I have a dream!n");   // n用于换行
        fw.write("you need to have a dream!");
4.流资源的关闭
        //4.流资源的关闭
        fw.close();
完整代码
    @Test
    public void testFileWriter() throws IOException {
        //1.提供File类的对象,指明写出到的文件
        File file = new File("hello.txt");
        //2.提供FileWriter的对象,用于数据输出
        FileWriter fw = new FileWriter(file);
        //3.写出的 *** 作
        fw.write("I have a dream!n");   // n用于换行
        fw.write("you need to have a dream!");
        //4.流资源的关闭
        fw.close();
    }
⚪结果:

 (二)说明 ⚪输出 *** 作对应的File可以不存在,并不会报异常

如果不存在:在输出的过程中,会自动创建此文件;

如果存在:

     ①如果流使用的构造器是:FileWriter(file,false) /  FileWriter(file):对原有文件覆盖

     ②如果流使用的构造器是:FileWriter(file,true):不会对原有文件覆盖,而是在原有文件基础上追加内容

四、使用FileReader和FileWriter实现文本文件的复制
    @Test
    public void testFileWriterFileWriter() {
        FileReader fr = null;
        FileWriter fw = null;
        try {
            //1.创建File的对象,指明读入和写出的文件
            File srcFile = new File("hello.txt");
            File destFile = new File("hello2.txt");
            //2.创建输入流和输出流的对象
            fr = new FileReader(srcFile);
            fw = new FileWriter(destFile);

            //3.数据的读入和写出 *** 作
            char[] cbuf = new char[5];
            int len;//记录每次读入到cbuf数组中的字符个数
            while ((len = fr.read(cbuf)) != -1){
                //每次写出len个字符
                fw.write(cbuf,0,len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //4.关闭流资源
            try {
                if (fw != null)
                    fw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (fr != null)
                    fr.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
五、图片文件的处理 1.字符流能否处理图片文件

不能

2.需要用到字节流来处理图片文件

FileInputStream  FileOutputStream

六、FileInputStream和FileOutputStream的使用

使用字节流FileInputStream处理文本文件可能出现乱码

(一)使用字节流FileInputStream处理文本文件
    @Test
    public void test1() {
        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.print(str);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(fis != null){
                //4.关闭资源
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
⚪结论

1.对于文本文件(.txt,.java,.c,.cpp),使用字符流进行处理

2.对于非文本文件(.jpg,.mp3,.mp4,.avi,.doc,.ppt等),使用字节流进行处理

(二)使用字节流处理非文本文件——实现对图片的复制
    @Test
    public void test2() {
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            File srcFile = new File("1515050201.jpg");
            File destFile = new File("1515050201(2).jpg");
            fis = new FileInputStream(srcFile);
            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();
                }
            }
        }
    }

复制成功!!! 

 七、指定路径下文件的复制的方法 1.方法
    public void copyFile(String srcPath,String destPath){
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            File srcFile = new File(srcPath);
            File destFile = new File(destPath);
            fis = new FileInputStream(srcFile);
            fos = new FileOutputStream(destFile);
            //复制的过程
            byte[] buffer = new byte[1024];  //2^10
            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();
                }
            }
        }
    }
2.视频的复制
    @Test
    public void testCopyFile(){
        long start = System.currentTimeMillis();
        String srcPath = "C:\Users\Lenovo\Desktop\软件2005班张兰.mp4";
        String destPath = "C:\Users\Lenovo\Desktop\软件2005班张兰02.mp4";
        copyFile(srcPath,destPath);
        long end = System.currentTimeMillis();

        System.out.println("复制 *** 作花费的时间为:" + (end-start));
    }
 耗费的时间:

运行结果 

 

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5708338.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存