Java IO_文件分割_面向过程核心版 尚学堂187

https://www.bilibili.com/video/BV1ct411n7oG?p=188 

Java IO_文件分割_面向过程核心版 尚学堂187_第1张图片

RandomAccessFile 类的实例支持读取和写入随机访问文件。

其构造器可以指定读写模式:

Java IO_文件分割_面向过程核心版 尚学堂187_第2张图片

它的第一个参数是文件的路径或文件的对象,不是放其它的流了,第二个参数mode: 

Java IO_文件分割_面向过程核心版 尚学堂187_第3张图片 可以有读取和读写,"rws"和"rwd"不常用。

他的操作方式和字节流是一样的。

其seek(搜寻)方法: 

 


测试RandomAccessFile 的 seek方法:

指定起始位置,读取剩余的所有内容。

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;

public class Test {

	public static void main(String[] args) throws IOException{
		RandomAccessFile raf = new RandomAccessFile(new File("abc.txt"), "r");
		//随机读取
		raf.seek(2);//从第2个位置开始
		//读取
		//操作(分段读取)
		byte[] flush = new byte[1024];//缓冲容器
		int len = -1;//接收长度
		while((len = raf.read(flush)) != -1) {
			System.out.println(new String(flush, 0, len));
		}
		
		//释放资源
		raf.close();
	}

}

Java IO_文件分割_面向过程核心版 尚学堂187_第4张图片

输出结果:

CDEFG


 读区一部分:

分块思想:起始、实际大小

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;

public class Test {

	public static void main(String[] args) throws IOException{
		RandomAccessFile raf = new RandomAccessFile(new File("abc.txt"), "r");
		//起始位置
		int beginPos = 2;
		//实际大小
		int actualSize = 3;
		//随机读取
		raf.seek(beginPos);//从第beginPos个位置开始
		//读取
		//操作(分段读取)
		byte[] flush = new byte[1024];//缓冲容器
		int len = -1;//接收长度
		while((len = raf.read(flush)) != -1) {
			if(actualSize > len) {//获取本次读取的所有内容
				System.out.println(new String(flush, 0, len));
				actualSize -= len;
			}
			else {
				System.out.println(new String(flush, 0, actualSize));
				break;
			}
		}
		
		//释放资源
		raf.close();
	}

}

Java IO_文件分割_面向过程核心版 尚学堂187_第5张图片

输出结果:

CDE


 Math.ceil:向上取整,比如3.5向上取整就是4.0

每块大小1024字节,分多少块:

import java.io.File;

public class Test {

	public static void main(String[] args) {
		//分多少块
		File src = new File("src/FileUtils.java");
		//总长度
		long len = src.length();
		//每块大小
		int blockSize = 1024;
		//多少块
		int size = (int)Math.ceil(len*1.0/blockSize);
		System.out.println(size);
	}

}

输出结果:

3


 分割:

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;

public class Test {

	public static void main(String[] args) throws IOException {
		//分多少块
		File src = new File("src/FileUtils.java");
		//总长度
		long len = src.length();
		//每块大小
		int blockSize = 1024;
		//多少块
		int size = (int)Math.ceil(len*1.0/blockSize);
		//起始位置和实际大小
		int beginPos = 0;
		int actualSize = (int)(blockSize>len?len:blockSize);
		for(int i=0;i" + beginPos + "-->" + actualSize);
			split(i, beginPos, actualSize);
		}

	}
	
	/**
	 * 指定第i块的起始位置和实际长度
	 * @param i
	 * @param beginPos
	 * @param actualSize
	 * @throws IOException
	 */
	public static void split(int i, int beginPos, int actualSize) throws IOException{
		RandomAccessFile raf = new RandomAccessFile(new File("src/FileUtils.java"), "r");
		//随机读取
		raf.seek(beginPos);//从第beginPos个位置开始
		//读取
		//操作(分段读取)
		byte[] flush = new byte[1024];//缓冲容器
		int len = -1;//接收长度
		while((len = raf.read(flush)) != -1) {
			if(actualSize > len) {//获取本次读取的所有内容
				System.out.println(new String(flush, 0, len));
				actualSize -= len;
			}
			else {
				System.out.println(new String(flush, 0, actualSize));
				break;
			}
		}
		
		//释放资源
		raf.close();
	}

}

输出结果:

0-->0-->1024
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.*;

public class FileUtils {

    public static void main(String[] args) {
        System.out.println("hello");
        try {
            InputStream is = new FileInputStream("p.png");
            OutputStream os = new FileOutputStream("p-copy2.png");
            copy(is, os);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 对接输入输出流
     * @param is
     * @param os
     */
    public static void copy(InputStream is, OutputStream os) {
        try(is;os){
            //3、操作(分段读取)
            byte[] flush = new byte[1024];//缓冲容器
            int len = -1;//接收长度
            while((len = is.read(flush)) != -1) {
                os.write(flush, 0, len);//分段写出
                os.flush();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    /**
1-->1024-->1024

     * 对接输入输出流
     * @param is
     * @param os
     */
    public static void copy2(String srcPath, String destPath) {
        //1、创建源
        File src = new File(srcPath);//源头
        File dest = new File(destPath);//目的地
        //2、选择流
        try(InputStream is = new FileInputStream(src);
                OutputStream os = new FileOutputStream(dest)){
            //3、操作(分段读取)
            byte[] flush = new byte[1024];//缓冲容器
            int len = -1;//接收长度
            while((len = is.read(flush)) != -1) {
                os.write(flush, 0, len);//分段写出
            }
            os.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    /**
     * 释放资源
     * @param is
     * @param os
     */
    public static void close(InputStream is, OutputStream os) {
        try {
            if(null != os) {
                os.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            if(null != is) {
                is.close();
            }
        } catch (IOException e) {

2-->2048-->235
            e.printStackTrace();
        }
    }
    
    /**
     * 释放资源
     * @param ios
     */
    public static void close(Closeable... ios) throws IOException {
        for (Closeable io : ios) {
            if(null != io) {
                io.close();
            }
        }
    }
}


 

你可能感兴趣的:(Java_尚学堂笔记,java)