• Java基础 使用转换流进行文件的复制 / RandomAccessFile 类进行文件的复制


    笔记:

     **使用转换流进行文件的复制
      文本文件---字节流FileInputStream--> [InputStreamReader] -----字符流BufferedReader------>程序 * ------字符流BufferedWriter----->[OutputStreamWriter]-----字节流-FileOutputStream--->[ 输出目的/ 文件] * 编码: 字符串-->字节数组 * 解码: 字节数组-->字符串
    /** RandomAccessFile :支持随机访问
    * 1.既可以充当一个输入流,也可以充当一个输出流 ; read(byte b[])/write()
    * 2.支持从文件的任意位置的开头进行读取和写入 ;
    * raf.seek(long pos) //设置读指针的地址;
    * raf.writeBytes(String s);//将一个字符串 写入文件,
    * writeInt(int v);//写入一个int数据
    * 3.支持从任意位置的读取和写入(插入);
    */

    代码:

    实验4: 使用转换流  进行文件的复制
       @Test //实验4: 使用转换流  进行文件的复制
        public void testTrans() throws IOException {
    
            //①声明②将地址加载进字符流,将字符流加载进缓冲流③read/write④close缓冲流)
        // 解码
            File src=new File("D:\SZS文件夹\IO\hello.txt");
            FileInputStream fis= new FileInputStream(src);
            InputStreamReader isr=new InputStreamReader(fis,"GBK");
            BufferedReader br=new BufferedReader(isr);
        //编码
            File dest=new File("D:\SZS文件夹\IO\转换流复制的hello.txt");
            FileOutputStream fos = new FileOutputStream(dest);
            OutputStreamWriter osw=new OutputStreamWriter(fos,"GBK");
            BufferedWriter bw=new BufferedWriter(osw);
    
            char[] b=new char[1024];
            int len;
            while((len=br.read(b))!= -1) {
                bw.write(b,0,len);
            }
            bw.flush();
            br.close();
            bw.close();
        }
    }

    使用 RandomAccessFile 类 进行进行文件的复制测试的代码:

    
    
    import org.junit.Test;
    import java.io.File;
    import java.io.IOException;
    import java.io.RandomAccessFile;
    public class TestRandomAccessFile {
        //进行文件的读/写
        @Test              //使用RandomAccessFile 进行文件的复制
        public void test1() throws IOException {
            File src= new File("D:\SZS文件夹\IO\hello.txt");
            File dest= new File("D:\SZS文件夹\IO\RandomAccessFile_hello.txt");
            RandomAccessFile raf1=new RandomAccessFile(src,"r");
            RandomAccessFile raf2=new RandomAccessFile(dest,"rw");
    
            byte[] b = new byte[20];
            int len=0;
           while((len=raf1.read(b)) !=-1){
               raf2.write(b,0,len);
           }
           raf1.close();
           raf2.close();
        }
    
        @Test  //使用RandomAccessFile 的seek() 设置读指针的位置 ,下面的栗子相当于忽略了前10个字符
        public void test2() throws IOException{
            File src= new File("D:\SZS文件夹\IO\hello.txt");
            File dest= new File("D:\SZS文件夹\IO\RandomAccessFile2_hello.txt");
            RandomAccessFile raf1=new RandomAccessFile(src,"r");
            RandomAccessFile raf2=new RandomAccessFile(dest,"rw");
            raf1.seek(10);  //设置读指针的位置
            byte[] b = new byte[20];
            int len=0;
            while((len=raf1.read(b)) !=-1){
                raf2.write(b,0,len);
            }
            raf1.close();
            raf2.close();
        }
    }
  • 相关阅读:
    校招 存储相关研发工程师 收人要求
    Ubuntu配置
    个人感想
    Linux入门 文本编辑器
    linux入门 一些常见命令
    linux入门 配置网络
    面试逻辑题 5L和3L的杯子 8L的水 倒出4L
    Java并发编程的艺术读后总结
    URL重写
    常见的四种排名函数的用法----转载
  • 原文地址:https://www.cnblogs.com/zhazhaacmer/p/9806326.html
Copyright © 2020-2023  润新知