Commons-io jar包中的方法 功能流 以及 线程基础

Commons-io

   获取路径 扩展名.txt .png
  static String getExtension(String filename)

   获取文件的名字
  static String getName(String filename)

   判断是不是这个扩展名
  static boolean isExtension(String filename,String extension)

   复制文件夹
  static void copyDirectoryToDirectory(File src,File desc)

   复制文件
  static void copyFile(File src,File desc)

    写入字符串到文件
  static void writeStringToFile(File src,String date)

    按字符串读取文件
  static String readFileToString(File src)

    写入文件 可以选取用什么字节流写入
  static void write(String data, OutputStream output)

    读取文件到集合中以字符串形式
  static List<String> readLines(InputStream input)
 ```

学习方法:

1. 先找类 找到对应的类(先看后缀)
2. 看该类如何创建对象(获取对象 怎么调方法)
3. 看方法名 揣测 方法的用意
4. 测试看结果 然后整理在方法集中
# 功能流(合并流)
## SequenceInputStream
### 合并功能(可以把 多个文件读成(合并)成一个流) 

构造方法:

参数 是迭代器 是Vector特有的
该Vector要保存的是 InputStream 的子类
SequenceInputStream(Enumeration

// 将三个文件写成一个文件

public static void main(String[] args) throws IOException {


    File f1 = new File("文件一路径");
    File f2 = new File("文件二路径");
    File f3 = new File("文件三路径");
    // 创建集合 保存的是 FileInputStream
    Vector<FileInputStream> vector = new Vector<>();
    vector.add(new FileInputStream(f1));
    vector.add(new FileInputStream(f2));
    vector.add(new FileInputStream(f3));
    // 获取迭代器
    Enumeration<FileInputStream> elements = vector.elements();
    // 构建合并流
    SequenceInputStream sis = new SequenceInputStream(elements);
    // 写到一个文件
    FileOutputStream fos = new FileOutputStream("合并的文件路径");
    // 数组方式写入
    byte[] b = new byte[1024];
    int len = 0;
    while ((len = sis.read(b)) != -1) {
        fos.write(b, 0, len);
    }
    sis.close();
    fos.close();

}
1. 将图片切割成多个1M的文件
2. 然后将这些文件合并成一个图片
public static void fun1() throws FileNotFoundException, IOException {
    // 先读取文件
    File file = new File("要被分割的文件");
    FileInputStream fis = new FileInputStream(file);

    FileOutputStream fos = null;
    int len = 0;
    // 保存文件的名字
    int num = 1;
    byte[] b = new byte[1024 * 1024];
    while ((len = fis.read(b)) != -1) {
        // 写入多个文件 拼接文件的名字
        fos = new FileOutputStream(new File("分割的文件路径"+ num +".后缀"));
        fos.write(b, 0, len);
        fos.close();
        // 名字自增
        num++;
    }
    fis.close();
}



Vector<FileInputStream> vector = new Vector<>();
    for (int i = 1; i < 19; i++) {
        File file = new File("路径" + i + ".后缀");
        FileInputStream fis = new FileInputStream(file);
        vector.add(fis);
    }
    // 获取迭代器
    Enumeration<FileInputStream> elements = vector.elements();
    // 创建合并流
    SequenceInputStream sis = new SequenceInputStream(elements);
    // 写到新路径下
    FileOutputStream fos = new FileOutputStream("合并的文件路径");
    byte[] b = new byte[1024 * 1024];
    int len = 0;
    while ((len = sis.read(b)) != -1) {
        fos.write(b, 0, len);
    }
    sis.close();
    fos.close();

# 线程
### 进程: 一个正在运行的程序(独立)
#### 进程只有一个线程的叫单线程程序
#### 有多个线程的叫多线程程序
### 线程: 一个线程 相当于一个cpu的执行路径 独立的运行单元 

      单线程程序

public static void main(String[] args) {
    /*
     * 绝对安全 程序由上至下一次执行
     */
    // 添加
    add();
    // 删除
    remove();
    System.out.println("程序执行完毕");
}
public static void add() {
    for (int i = 0; i < 100; i++) {
        System.out.println(i);
    }
}
public static void remove() {
    System.out.println("删除");
}


## 创建一个线程并开启
###  1. 需要继承Thread类
###  2. 重写父类中的 run方法
####  线程默认名字: Thread - 0  Tread - 1 .....

public static void main(String[] args) {

    // 创建一个线程
    SubThread subThread = new SubThread("ww");
    // 直接调用run方法 相当于 就调用一个普通的成员方法
    // subThread.run();
    // 开启线程
    // 注意: 直接调用run方法不能开启线程 需要调用start方法开启线程
    subThread.start();
    for (int i = 0; i < 50; i++) {
        System.out.println("- main - " + i);
    }
    // 获取主线程的名字
    // 获取当前执行的线程对象 静态方法
    Thread currentThread = Thread.currentThread();
    String name = currentThread.getName();
    System.out.println(name);
}

// Thread(String name) 该方法在创建线程的同时给线程起名
class SubThread extends Thread{
// 构造方法
public SubThread() {
}
public SubThread(String string) {
super(string);
}

@Override
public void run() {
    for (int i = 0; i < 50; i++) {
        // 获取线程名的方法 getName
        System.out.println("---- run 方法 ----" + i);
    }
    System.out.println(Thread.currentThread().getName());
}

}

class NameThread extends Thread {

private String name;

public NameThread() {
    super();
}

public NameThread(String name, String string) {
    super(name);
    // 给自己类的name赋值
    // 建议: 不要使用name这个名字 避免麻烦
    this.name = string;
}

// 把set/get方法改名
public String getNewName() {
    return name;
}

public void setNewName(String name) {
    this.name = name;
}

// 父类中的已经有getName()方法 并且用final修饰 不能重写 所以报错
/*
  public String getName() { 
       return name; 
  }

  public void setName(String name) {
       this.name = name; 
  }
 */
@Override
public void run() {
    super.run();
}

}

## 接口创建线程

public static void main(String[] args) {
    RunnableImpl rImpl = new RunnableImpl();
    Thread thread = new Thread(rImpl);
    // 开启线程
    thread.start();
}

// 接口中 run方法是 抽象方法
class RunnableImpl implements Runnable {

@Override
public void run() {
    for (int i = 0; i < 50; i++) {
        System.out.println(Thread.currentThread().getName() + "--" + i);
    }
}

}

“`

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值