IO流练习

目录

 

1 正则表达式中的字符集

2 日期、日历中常用方法

3 BigInteger、BigDecimal基础用法

4 文件

5 IO流(字节流重要)

6 回顾:序列化

7 随机文件访问流


1 正则表达式中的字符集

正则表达式回顾:https://blog.csdn.net/qq_43711904/article/details/96114479

校验电话号码:^[1][345789][0-9]{9}$
校验IP地址:
    192.168.1.1 规则:一共四组,每一组都是0-255。分为以25开头或2开头或1开头的情况。
    ^((25[0-5]|2[0-4]\\d|[1]{1}\\d{1}\\d{1}|[1-9]{1}\\d{1}|\\d{1})($|(?!\\.$)\\.)){4}$
校验邮箱:^[a-z0-9A-Z]+[- | a-z0-9A-Z . _]+@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-z]{2,}$

2 日期、日历中常用方法

//日期
//        DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss E");
//        
//        System.out.println("系统时间:" + new Date());
//        System.out.println("系统时间毫秒数:" + System.currentTimeMillis());
//        System.out.println("格式化日期:" + df.format(new Date()));
//        System.out.println("指定日期转换:" + new SimpleDateFormat("yyyy-MM-dd").parse("1998-3-4"));
//        System.out.println("一个常规的数字转换实例:" + NumberFormat.getInstance().format(123456));
//        System.out.println("一个货币格式数字转换实例:" + NumberFormat.getCurrencyInstance().format(123456));
        
        //日历
//        Calendar c = Calendar.getInstance();
//        c.setTime(new Date()); //设置时间参照点
//        System.out.println("DAY_OF_YEAR:" + c.get(Calendar.DAY_OF_YEAR));
//        System.out.println("DAY_OF_MONTH:" + c.get(Calendar.DAY_OF_MONTH));
//        c.set(Calendar.DAY_OF_MONTH, 17); //设置日期为本月的第17天
//        System.out.println("DAY_OF_WEEK:" + c.get(Calendar.DAY_OF_WEEK));
//        c.add(Calendar.DAY_OF_MONTH, 5); //设置日期为今天的后5天
//        System.out.println("DAY_OF_MONTH:" + c.get(Calendar.DAY_OF_MONTH));

3 BigInteger、BigDecimal基础用法

import java.math.BigDecimal;
import java.math.BigInteger;

public class Practise_01 {

    public static void main(String[] args) {

        //加法
        double d1 = 0.01, d2 = 0.05;
        BigDecimal d11 = BigDecimal.valueOf(d1);
        BigDecimal d22 = BigDecimal.valueOf(d2);
        System.out.println(d11.add(d22));
        
        //100的阶乘
        BigInteger sum = new BigInteger("1");
        for(int i = 1; i <= 100; i++) {
            BigInteger ii = BigInteger.valueOf(i);
            sum = sum.multiply(ii);
        }
        System.out.println(sum);
    }

}

4 文件

文件操作回顾:https://blog.csdn.net/qq_43711904/article/details/96427231
File类代表与平台无关的文件和目录。
File能新建、删除、重命名文件和目录,但File不能访问文件内容本身。如果需要访问文件内容本身,则需要使用输入/输出流。

5 IO流(字节流重要)

字节流与字符流回顾:https://blog.csdn.net/qq_43711904/article/details/96435532

//递归实现输出指定目录下的所有子文件
import java.io.File;

public class PrintIO {
    public static void main(String[] args) {
        print(new File("E:\\practise"),1);
    }        
    /**
     * 打印文件目录结构
     * @param file 文件
     * @param m 判断是第几层文件,决定了输出几个间隔符
     */
    public static void print(File file,    int m) {
        if(file.isDirectory()) {
            File[] fileLists = file.listFiles();
            //第一层的两个子文件
            for(int i = 0; i < fileLists.length; i++) {
                //第一层的第一个子文件前输出一个间隔符
                for(int j = 0;j < m; j++){
                    System.out.print("|---");
                }
                //第一层的第一个子文件名称
                System.out.println(fileLists[i].getName());
                //第一层的第一个子文件下的子文件
                if(fileLists[i].isDirectory()) {
                    print(fileLists[i],m + 1); //继续依次判断
                }
            }
        } else {
            System.out.println("这是一个文件!");
        }
    }
}

6 回顾:序列化

对象序列化:https://blog.csdn.net/qq_43711904/article/details/96638740

7 随机文件访问流

RandomAccessFile类可以实现文件的跳跃式读取,即可以只读取文件中的部分内容,但是要求数据的保存位数确定,适合庞大文件的读取。该类中方法:public RandomAccessFile​(File file,String mode) throws FileNotFoundException。其中,文件处理模式:r(只读)、rw(读写)

  • 基础运用:
import java.io.*;
public class RandomAccessFileDemo {
    public static void main(String[] args) throws Exception {
        File file = new File("d:" + File.separator + "a.txt") ;
        RandomAccessFile raf = new RandomAccessFile(file, "rw") ;
        // 写数据,数组中数据现在是每行5+4=9字节;int占4个字节
        // String str[] = new String[] {"abcde","abc  ","ab   "} ; //位数一致
        // int num[] = new int[] {20, 30, 10} ;
        // for(int x = 0 ; x < str.length; x ++) {
        //     raf.write(str[x].getBytes()) ;
        //     raf.writeInt(num[x]) ;
        // }
        // raf.close();
        // 读数据
        {
            //读取“ab   ”,需要跳到18位(一个字节八位)
            raf.skipBytes(18) ; //向下跳
            byte[] data = new byte[5] ;
            int len = raf.read(data) ;
            System.out.println(new String(data, 0, len).trim() + "-" + raf.readInt());
        }
        {
            //读取“abc  ”
            raf.seek(9) ; //向回跳,回到第九位处
            byte[] data = new byte[5] ;
            int len = raf.read(data) ;
            System.out.println(new String(data, 0, len).trim() + "-" + raf.readInt());
        }
        {
            //读取“abcde”
            raf.seek(0) ; //向回跳,回到头
            byte[] data = new byte[5] ;
            int len = raf.read(data) ;
            System.out.println(new String(data, 0, len).trim() + "-" + raf.readInt());
        }
        raf.close() ;
    }
}
  • 拷贝大数据量文件

-------------------------------------------主线程分模块复制------------------------------------------

//多线程复制大文件
import java.io.File;

public class TestCopy {
    public static void main(String[] args) throws Exception {
        //开启3个线程分模块复制源文件内容
        copy("E:\\practise","D:\\practise",3);
    }
    /**
     * 将源文件路径下的文件复制到目标路径下
     * @param srcpath 源文件路径
     * @param distpath 目标文件路径
     * @param n 开启的线程数(文件划分的块数)
     * @throws Exception
     */
    public static void copy(String srcpath,String distpath,Integer n) throws Exception{
        if(n < 1) {
            return;
        }
        File file = new File(srcpath);
        long length = file.length(); //获取文件长度
        long len = length/n; //子线程处理的长度
        for(int i = 1; i < n; i++){
            Sub sub = new Sub(srcpath, distpath, (i-1)*len, i*len);
            sub.start();
        }
        //把剩余的文件内容分给最后一个线程执行
        Sub sub = new Sub(srcpath,distpath,(n-1)*len, file.length());
        sub.start();
    }
}
-------------------------------------------子类线程复制一个模块的内容------------------------------------------

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

//自定义子线程类
public class Sub extends Thread{
    private String srcpath; //源文件路径
    private String distpath; //目标文件路径
    private long startindex; //复制起始索引
    private long endindex; //复制结束索引
    public Sub(String srcpath,String distpath,long startindex,long endindex){
        this.srcpath = srcpath;
        this.distpath = distpath;
        this.startindex = startindex;
        this.endindex = endindex;
    }
    @Override
    public void run() {
        try {
            RandomAccessFile r1 = new RandomAccessFile(srcpath, "r"); //原文件只读
            RandomAccessFile r2 = new RandomAccessFile(distpath, "rw"); //目标文件可读可写
            r1.seek(startindex);
            r2.seek(startindex);
            long index = startindex; //标志读取的起始位置
            byte[] bytes = new byte[1024]; //读取内容到数组 
            int n; //read()-读到的每一“小组”的字节数,读到null时,返回-1
            while((n = r1.read(bytes)) != -1){
                index += n;
                r2.write(bytes,0,n); //将读的数组写入目的路径
                if(index >= endindex) { //直到读到结尾处
                    break;
                }
            }
            r1.close(); //关闭流
            r2.close();
        }catch(IOException e){
            e.printStackTrace();
        }
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值