栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

JavaSE-IO流--IO原理、流的分类、FileReader、FileWriter

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

JavaSE-IO流--IO原理、流的分类、FileReader、FileWriter

IO原理及流的分类
  • 1、IO原理及流的分类
    • 1.1、Java IO原理
    • 1.2、流的分类
    • 1.3、IO流体系
  • 2、数据读入FileReader
    • 2.1、FileReader读入数据的基本操作:使用read()方法
    • 2.2、FileReader使用read(char[] cbuf)读入数据
  • 3、写出数据FileWriter
  • 4、FileReader和FileWriter实现文本文件的复制
  • 5、FileInputStream、FileOutputStream实现非文本文件的复制
    • 5.1、图片的复制
    • 5.2、视频的复制
  • 6、字节型缓冲流的使用BufferedInputStream 和BufferedOutputStream
    • 6.1、复制文本
    • 6.2、复制视频
  • 7、字节型缓冲流的使用BufferedReader和BufferedWriter
  • 8、总结

1、IO原理及流的分类 1.1、Java IO原理


1.2、流的分类

1.3、IO流体系

2、数据读入FileReader 2.1、FileReader读入数据的基本操作:使用read()方法
  • IDEA中main方法中文件路径是相对于当前工程
  • IDEA中单元测试中的该文件路径是相对于当前module
package com.io.test;

import org.junit.Test;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;


public class FileReaderWriterTest {

    public static void main(String[] args) {
        File file = new File("Hello.txt");//main方法中该文件路径是相对于当前工程
        System.out.println(file.getAbsolutePath());//获取Hello.txt文件的绝对路径:E:workspacesIDEAJavaSEHello.txt

        File file1 = new File("IO\Hello.txt");
        System.out.println(file1.getAbsolutePath());
    }

     
    @Test
    public void test(){
        FileReader fileReader = null;
        try {
            //1、实例化File对象,指明要操作的文件
            File file = new File("Hello.txt");//单元测试中的该文件路径是相对于当前module
            //System.out.println(file.getAbsolutePath());//控制台输出为:E:workspacesIDEAJavaSEIOHello.txt

            //2、提供具体的流
            fileReader = new FileReader(file);

            //3、数据的读入
            //Read():返回读入的一个字符。如果达到文件末尾,返回-1
            int data = fileReader.read();
            while (data != -1){
                System.out.print((char)data);
                data = fileReader.read();
            }
        }catch (IOException e){
            e.printStackTrace();
        }finally{
            try {
                //4、关闭流
                if(fileReader != null){
                    fileReader.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

2.2、FileReader使用read(char[] cbuf)读入数据
  • read(char[] cbuf):返回每次读入到数组中的字符个数,
public class FileTest{
//对read()操作升级:使用read的重载方法
    @Test
    public void testFileReader(){
        FileReader fr = null;
        try {
            //1、File类的实例化
            File file = new File("Hello.txt");
            //2、FileReader流的实例化
            fr = new FileReader(file);
            //3、读入的操作
            //read(char[] cbuf):返回每次读入cbuf数组中字符的个数,如果达到文件末尾,返回-1
            char[] cbuf = new char[5];
            int len;
            while((len = fr.read(cbuf)) != -1){
                //方式一
                for(int i = 0; i < len; i++){
                    System.out.print(cbuf[i]);
                }
                //方式二
//                String str = new String(cbuf,0,len);
//                System.out.print(str);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally{
            //4、关闭流
            if(fr !=null){
                try {
                    fr.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
 }

3、写出数据FileWriter
public class FileWriter {
    @Test
    public void test() throws IOException {
        //1、提供File类的对象,指明写出到的文件
        File file = new File("writer.txt");

        //2、提供FileWriter的对象,用于数据的写出
        java.io.FileWriter fileWriter = new java.io.FileWriter(file,true);

        //3、写出的操作
        fileWriter.write("I have a dream!n");
        fileWriter.write("You need to have a dream!n");

        //4、流资源的关闭
        fileWriter.close();

    }
}

4、FileReader和FileWriter实现文本文件的复制
 //FileReader和FileWriter实现文本文件的复制
    @Test
    public void test01()  {
        FileReader fr = null;
        FileWriter fw = null;
        try{
            //1、提供File类的对象,指明读入和写出的文件
            File srcFile = new File("hello.txt");
            File destFile = new File("hello1.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){
                fw.write(cbuf,0,len);
            }
        }catch (IOException e){
            e.printStackTrace();
        }finally {
            //4、流资源的关闭
            if(fw!=null){
                //4、流资源的关闭
                try {
                    fw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }finally {
                    if(fr!=null){
                        try {
                            fr.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
    }
5、FileInputStream、FileOutputStream实现非文本文件的复制
  • 对于文本文件(.txt .java .c .cpp)使用字符流处理
  • 对于非文本文件(.jpg .mp3 .mp4 .avi .doc .ppt)使用字符流处理
5.1、图片的复制
public class FileInputOuputStream {
    //实现图片(非文本文件)的的复制
    @Test
    public void test01()  {
        FileInputStream fr = null;
        FileOutputStream fw = null;
        try{
            //1、提供File类的对象,指明读入和写出的文件
            File srcFile = new File("bird.png");
            File destFile = new File("bird1.png");

            //2、提供输入流和输出流的对象
            fr = new FileInputStream(srcFile);
            fw  =new FileOutputStream(destFile);

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

5.2、视频的复制
public class FileInputOuputStream {
	public void copyFile(String srcFile,String destFile)  {
	        FileInputStream fis = null;
	        FileOutputStream fos = null;
	        try{
	            //2、提供输入流和输出流的对象
	            fis = new FileInputStream(srcFile);
	            fos  =new FileOutputStream(destFile);
	
	            //3、数据的读入和写出的操作
	            byte[] cbuf = new byte[1024];
	            int len;//记录每次读入到cbuf数组中字符的个数
	            while ((len = fis.read(cbuf)) != -1){
	                fos.write(cbuf,0,len);
	            }
	        }catch (IOException e){
	            e.printStackTrace();
	        }finally {
	            //4、流资源的关闭
	            if(fos!=null){
	                //4、流资源的关闭
	                try {
	                    fos.close();
	                } catch (IOException e) {
	                    e.printStackTrace();
	                }finally {
	                    if(fis!=null){
	                        try {
	                            fis.close();
	                        } catch (IOException e) {
	                            e.printStackTrace();
	                        }
	                    }
	                }
	            }
	        }
	    }
	
	    @Test
	    public void test(){
	        String srcFile = "C:\Users\Lenovo\Desktop\01-视频.mp3";
	        String destFile = "C:\Users\Lenovo\Desktop\02-视频.mp3";
	        long start = System.currentTimeMillis();
	        copyFile(srcFile, destFile);
	        long end = System.currentTimeMillis();
	        System.out.println("复制视频耗时:" + (end-start) );
	    }
	}
6、字节型缓冲流的使用BufferedInputStream 和BufferedOutputStream 6.1、复制文本
 public class BufferedTest {
    @Test
    public void test(){
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try {
            //1、造文件
            File srcFile = new File("bird.png");
            File destFile = new File("bird3.png");
            //2、造流
            //2.1、造节点流
            FileInputStream fis = new FileInputStream(srcFile);
            FileOutputStream fos = new FileOutputStream(destFile);
            //2.2、造缓冲流
            bis = new BufferedInputStream(fis);
            bos = new BufferedOutputStream(fos);
            //3、复制的细节:读取,写入
            byte[] buffer = new byte[5];
            int len;
            while((len = bis.read(buffer)) != -1){
                fos.write(buffer,0,len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {//说明:关闭外层流的同时,内层流也会自动的进行关闭。关于内层流的关闭。我们可以省略
            if(bos != null){
                //4、资源关闭
                //要求:先关闭外层的流、再关闭内层的流
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(bis != null){
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
 }
6.2、复制视频
 public class BufferedTest {
  public void copyFile(String srcFile,String destFile)  {
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try{
            //2、提供输入流和输出流的对象
            fis = new FileInputStream(srcFile);
            fos  =new FileOutputStream(destFile);

            //3、数据的读入和写出的操作
            byte[] cbuf = new byte[1024];
            int len;//记录每次读入到cbuf数组中字符的个数
            while ((len = fis.read(cbuf)) != -1){
                fos.write(cbuf,0,len);
            }
        }catch (IOException e){
            e.printStackTrace();
        }finally {
            //4、流资源的关闭
            if(fos!=null){
                //4、流资源的关闭
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }finally {
                    if(fis!=null){
                        try {
                            fis.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
    }

		//测试方法
    @Test
    public void test(){
        String srcFile = "C:\Users\Lenovo\Desktop\01-视频.mp3";
        String destFile = "C:\Users\Lenovo\Desktop\02-视频.mp3";
        long start = System.currentTimeMillis();
        copyFile(srcFile, destFile);
        long end = System.currentTimeMillis();
        System.out.println("复制视频耗时:" + (end-start) );
    }
 }
7、字节型缓冲流的使用BufferedReader和BufferedWriter
@Test
    public void testBufferedReaderBufferWriter(){
        BufferedReader br = null;
        BufferedWriter bw = null;
        try {
            //创建文件和相应的流
            br = new BufferedReader(new FileReader(new File("hello.txt")));
            bw = new BufferedWriter(new FileWriter(new File("hello.txt")));
            //读写操作
            //方式一:使用char[]数组
//            char[] cbuff = new char[5];
//            int len;
//            while((len = br.read(cbuff)) != -1){
//                bw.write(cbuff,0,len);
//            }
            //方式二:使用String[]
            String data;
            while((data=br.readLine()) !=null){
                //方法一:
                bw.write(data);//data中不包含换行符
                //方法二:
                bw.write(data);//data中不包含换行符
                bw.newLine();//提供换行操作

            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {//关闭资源
            if(bw != null){
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
           if(br != null){
               try {
                   bw.close();
               } catch (IOException e) {
                   e.printStackTrace();
               }
           }
        }
    }
8、总结
 二、流的体系结构
 抽象基类          节点流(文件流)                                 缓冲流(处理流的一种)
 InputStream    FileInputStream (read(byte[] buffer)          BufferedInputStream (read(byte[] buffer)
 OutputStream   FileOutStream (write(byte[] buffer,0,len))    BufferedOutputStream (write(byte[] buffer,0,len))
 Reader         FileReader (read(byte[] buffer)               BufferedReader (read(byte[] buffer /readLine())
 Writer         FileWriter (write(byte[] buffer,0,len))       BufferedWriter (write(byte[] buffer,0,len))
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/996454.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号